Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Where to use braces (http://www.go4expert.com/forums/braces-t9377/)

usama_muneeb 16Mar2008 15:08

Where to use braces
I am learning C by the LPE Programming in C book by Stephen Kochan. There are some confusing things in the sample programs. For instance, somewhere, with the for statement, braces have been used like this:


for ( conditions ) {

While somewhere, they are not:


for ( conditions )

Where to use them and where not?

shabbir 16Mar2008 17:45

Re: Where to use braces
When you have one statement in the loops / conditional statement you do not need to use the braces

usama_muneeb 16Mar2008 19:17

Re: Where to use braces
Thanks, useful.

asadullah.ansari 17Mar2008 11:08

Re: Where to use braces
If one statement in the loops / conditional statement then it does not matter u are putiing braces or not.

But strong recommendation is that put braces like this

expression ;

In all excellent industries recommend this.
Use of it is that No Confusion( If big and too much complex single expression) and easy to see the values by debugging)


AgprsLink_mp = (AgprsMotLink *) ( InputDataSubLink) *Function(agprs_val,
(PcmLinkDataRes_m *)( nData),
(PacketFormet_m* ) (nLink) );

this is simple case, I did'nt remember, I saw manyplace too much complex sinlgle expression and it will be confusion. So better use braces for any loop with a single or more than one statements.

All times are GMT +5.5. The time now is 23:00.