Path: chuka.playstation.co.uk!news From: "JohnT" Newsgroups: scee.yaroze.freetalk.english Subject: Re: ANSI C code Date: Tue, 17 Mar 1998 15:54:49 -0000 Organization: PlayStation Net Yaroze (SCEE) Lines: 129 Message-ID: <6em62v$dud33@chuka.playstation.co.uk> References: <6e5qdr$p7b1@emeka.playstation.co.uk> <6e693k$6nq4@chuka.playstation.co.uk> <3506CCE6.7301@127.0.0.1> <3507E51B.763DB0F2@hotmail.com> NNTP-Posting-Host: 194.75.167.131 X-Newsreader: Microsoft Outlook Express 4.72.2106.4 X-MimeOLE: Produced By Microsoft MimeOLE V4.72.2106.4 Ok, I was only going by what I was taught. Here it is... The semicolon ; is used to end a statement. Therefore the following are correct... for(i=0; i<10; str[i++]=0); This uses a null statement because the for loop has no need for a statement but the ; is still needed or else the next non-looped statement will be looped. for(; i<10; i++).... This is valid because a for loop HAS to have three argument areas separated by ;. You could also use this for a continuous loop... for(;;).... This is because the 'arguments' of a for loop are statements and three are required for the syntax to be correct (the third is terminated by the close bracket like an ordinary argument for a function). The braces { and } are used to enclose a block of statements or a function's content. So the semicolon ; should not be used. Therefore the following is incorrect... for(i=0; i<10; i++) { str[i] = 0; }; I think that because the K&R specifications don't specifically say this is illegal it is accepted by most compilers simply because the compiler is treating the ; as a null statement following the block of code in much the same way as... for(i=0; i<10; i++) { str[i] = 0; } printf("Done\n"); Does this clear things up better? JohnT ------------------------------------------------ Contact me at http://wwp.mirabilis.com/9461567 Send e-mail to johnt@nettech.demon.co.uk Visit my web site at www.coco.net.uk/johnt ------------------------------------------------ pal wrote in message <3507E51B.763DB0F2@hotmail.com>... >> Without having my copy of K&R here, i can't say for sure, but my vote is for >> ';' being a legal expression. > >The null statement is mentionned in K&R (p18 - on the French version that is :)). >They say it's useful for making empty loops, eg : >for (n = 0 ; getchar() != EOF ; ++n) ; // counts characters > >I think it's useful too when your for doesn't need an initializer : >for (; a==b ;a++) {...}. > >Moreover, an "infinite" loop like this one is syntactically correct, too : >for(;;) { > if (...) return ; > } > >(you may prefer while(1)) > >And, of course, as Majik pointed out, a macro like the following may lead to a >null statement : >#ifdef DEBUG > #define debug(x) printf(x) >#else > #define debug(x) >#endif > > >> so you should always write macros like: >> >> #define DEBUG(x) do { if(debug) printDebug(x) } while(0) >> >> the do { } while(0) construct makes the compiler treat the entire block >> as a single statement, and protects you from the dreaded dangling else. >> it forces you to use the macro as a function, as well, which is good >> practice. > >Well, what do you think about that : > >#define DEBUG(x) { if(debug) printDebug(x) ;} > >You can write blocs whereever you can write simple statements. With an ANSI-C >compiler, that is... Personnally I often write such blocs in order to reduce >variables' visibility (well, I'm not sure this is good English, sorry), and gcc >doesn't complain about it (perhaps he knows he'd be deleted from my hd) : > >// Note : please be aware that optimisation is not my purpose here :). >{ >int i ; >for (something with i) {...} >} > >// later in the same function... >{ >int i ; >for (something else with i) {...} >} > >This might not look very useful, but have a look at this one : > >f() >{ >int i ; >(...lots of code where i isn't used...) >(last part of code, where i is used, far from its declaration) >} > >Haven't you ever dreamt of writing : >for (int i = 0 ; i < n ; i++) {...} // Java code >? > >Mmh... I think it's enough, I just needed to talk between two lines of code +)... > >