Path: chuka.playstation.co.uk!news From: pal Newsgroups: scee.yaroze.freetalk.english Subject: Re: ANSI C code Date: Thu, 12 Mar 1998 14:37:31 +0100 Organization: PlayStation Net Yaroze (SCEE) Lines: 72 Message-ID: <3507E51B.763DB0F2@hotmail.com> References: <6e5qdr$p7b1@emeka.playstation.co.uk> <6e693k$6nq4@chuka.playstation.co.uk> <3506CCE6.7301@127.0.0.1> Reply-To: palpalpalpal@hotmail.com NNTP-Posting-Host: ppp-121-214.marseille.club-internet.fr Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Mailer: Mozilla 4.04 [en] (Win95; I) > 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 +)...