f



when to use ; and when to use ,

Any statements like
 printf("abc");
 scanf("%d",&x);
 a=20;
can be replaced by
 printf("abc"), scanf("%d",&x), a=20;

So when should we use ; and when sould we use ,

0
ra.ravi.rav (128)
6/16/2007 5:26:36 AM
comp.lang.c 30656 articles. 5 followers. spinoza1111 (3246) is leader. Post Follow

13 Replies
2914 Views

Similar Articles

[PageSpeed] 48

In article <1181971596.941107.318810@o11g2000prd.googlegroups.com>,
Ravi  <ra.ravi.rav@gmail.com> wrote:
>Any statements like
> printf("abc");
> scanf("%d",&x);
> a=20;
>can be replaced by
> printf("abc"), scanf("%d",&x), a=20;
>
>So when should we use ; and when sould we use ,

Use ; when you want people to be able to understand your code.

Use , when you only want the compiler to be able to understand your code.


dave
(there are places where it's useful and reasonable to use ,, but you
won't be running into those for a while)

-- 
Dave Vandervies                        dj3vande@csclub.uwaterloo.ca
P.J. Plauger has a valid reason [...] although I would say that was
more to do with his customers being less than sensible.
                                      --Flash Gordon in comp.lang.c
0
dj3vande (669)
6/16/2007 5:43:09 AM
"Ravi" <ra.ravi.rav@gmail.com> wrote in message 
news:1181971596.941107.318810@o11g2000prd.googlegroups.com...
> Any statements like
> printf("abc");
> scanf("%d",&x);
> a=20;
> can be replaced by
> printf("abc"), scanf("%d",&x), a=20;
>
> So when should we use ; and when sould we use ,
>#
The convention is to make extremely light use of the comma operator and 
place each operation on a line of its own, made into a statement by 
separating with semicolons.
There is no particular reason for this, it is just the convention. In the 
early days it might have made things a little easier for the compiler 
because a comma-separated list is also an expression, but that hardly 
applies on a modern machine.

-- 
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

0
regniztar (3128)
6/16/2007 7:00:12 AM
Ravi said:

> Any statements like
>  printf("abc");
>  scanf("%d",&x);
>  a=20;
> can be replaced by
>  printf("abc"), scanf("%d",&x), a=20;
> 
> So when should we use ; and when sould we use ,

If in doubt, use a semicolon.

-- 
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
0
rjh (10791)
6/16/2007 8:44:58 AM
"Ravi" <ra.ravi.rav@gmail.com> ha scritto nel messaggio 
news:1181971596.941107.318810@o11g2000prd.googlegroups.com...
> Any statements like
> printf("abc");
> scanf("%d",&x);
> a=20;
> can be replaced by
> printf("abc"), scanf("%d",&x), a=20;
>
> So when should we use ; and when sould we use ,
>
It is considered bad style to use the comma operator when splitting
the statement would do it (e.g. always, except in one of the expressions of 
a loop, or in a macro[1]).

[1]Nothing forbids to use semicolons in macros, but

#define DO_THESE do_this(); do_that()
if (cond)
    DO_THESE;
else
    puts("cond is zero.");

expands to

if (cond)
    do_this(); do_that();
else
    puts("cond is zero.");

which is a syntax error. And if there were no else, the code would
compile correctly but do_that() would be always executed. Some
people would use
#define DO_THESE do { do_this(); do_that(); } while 0
but I prefer
#define DO_THESE (do_this(), do_that())
which unlike the former can be used as a controlling expression of
a loop, etc. 


0
please.ask (434)
6/16/2007 9:29:57 AM
On Jun 16, 10:26 am, Ravi <ra.ravi....@gmail.com> wrote:
> Any statements like
>  printf("abc");
>  scanf("%d",&x);
>  a=20;
> can be replaced by
>  printf("abc"), scanf("%d",&x), a=20;
>
> So when should we use ; and when sould we use ,

Use "," for writing all the statements in same line. Like :
    printf("Enter value of A : "), scanf("%d",&a);
Here it makes some logical sense.

Use ";" otherwise.

One frequent use of , is in case of for loops :
    for ( i=0, j=m; i<n; ++i, ++j )
        to_arr[j] = from_arr[i];
Here we copy all all elements from from_arr[] to some contiguous
positions of to_arr[] starting from m. We are using "," here to
facilitate writing the for loop. Of course it can be done in other
ways, but this looks logically consistent and elegant. And of course
should you differ with my opinion, you could follow other ways and use
";".

0
6/17/2007 7:05:33 AM
BiGYaN said:

<snip>
 
> Use "," for writing all the statements in same line.

Why?

> Like :
>     printf("Enter value of A : "), scanf("%d",&a);
> Here it makes some logical sense.

No, it doesn't. Firstly, you have no guarantee that the prompt will 
appear before the program will block for input. Secondly, you are not 
checking that scanf yielded the proper return value.

<snip>

-- 
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
0
rjh (10791)
6/17/2007 7:39:36 AM
Army1987 wrote:

> 
> "Ravi" <ra.ravi.rav@gmail.com> ha scritto nel messaggio
> news:1181971596.941107.318810@o11g2000prd.googlegroups.com...
>> Any statements like
>> printf("abc");
>> scanf("%d",&x);
>> a=20;
>> can be replaced by
>> printf("abc"), scanf("%d",&x), a=20;
>>
>> So when should we use ; and when sould we use ,
>>
> It is considered bad style 

.... by some but not all people ...

> to use the comma operator when splitting 
> the statement would do it (e.g. always, except in one of the expressions
> of a loop, or in a macro[1]).


-- 
Far-Fetched Hedgehog
"No-one here is exactly what he appears." G'kar, /Babylon 5/

0
eh (206)
6/17/2007 3:03:43 PM
"Richard Heathfield" <rjh@see.sig.invalid> ha scritto nel messaggio 
news:adadnfSH7tIKeenbnZ2dnUVZ8sfinZ2d@bt.com...
> BiGYaN said:
>
> <snip>
>
>> Use "," for writing all the statements in same line.
>
> Why?
That does make sense if you do stuff such as

if (something)
    free(p), p = NULL;

(I've been tempted to do that at times, but I've always resisted to
these temptations and written
if (something) {
    free(p); p = NULL;
} if indeed I felt that putting a newline between two intimately
related operations was evil -- but, still, most times I resist even
this temptation and write them in two lines.)


>> Like :
>>     printf("Enter value of A : "), scanf("%d",&a);
>> Here it makes some logical sense.
>
> No, it doesn't. Firstly, you have no guarantee that the prompt will
> appear before the program will block for input. Secondly, you are not
> checking that scanf yielded the proper return value.

But replacing the comma with a semicolon doesn't solve the
problem... :-)


0
please.ask (434)
6/17/2007 8:46:44 PM
Army1987 said:

> 
> "Richard Heathfield" <rjh@see.sig.invalid> ha scritto nel messaggio
> news:adadnfSH7tIKeenbnZ2dnUVZ8sfinZ2d@bt.com...
>> BiGYaN said:
>>
>> <snip>
>>
>>> Use "," for writing all the statements in same line.
>>
>> Why?
> That does make sense if you do stuff such as
> 
> if (something)
>     free(p), p = NULL;

Why does that make sense?

<snip>

>>> Like :
>>>     printf("Enter value of A : "), scanf("%d",&a);
>>> Here it makes some logical sense.
>>
>> No, it doesn't. Firstly, you have no guarantee that the prompt will
>> appear before the program will block for input. Secondly, you are not
>> checking that scanf yielded the proper return value.
> 
> But replacing the comma with a semicolon doesn't solve the
> problem... :-)

Indeed, but it's a start. The first step in fixing code is to remove 
spurious "clever" stuff. That makes it easier to see what the real 
problems are.

-- 
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
0
rjh (10791)
6/18/2007 4:04:55 AM
"Richard Heathfield" <rjh@see.sig.invalid> ha scritto nel messaggio news:urCdnfENYq1VnuvbRVnytwA@bt.com...
> Army1987 said:
>
>>
>> "Richard Heathfield" <rjh@see.sig.invalid> ha scritto nel messaggio
>> news:adadnfSH7tIKeenbnZ2dnUVZ8sfinZ2d@bt.com...
>>> BiGYaN said:
>>>
>>> <snip>
>>>
>>>> Use "," for writing all the statements in same line.
>>>
>>> Why?
>> That does make sense if you do stuff such as
>>
>> if (something)
>>     free(p), p = NULL;
>
> Why does that make sense?

Put a newline retaining the comma, and you will give the reader the
impression that p = NULL is always evaluated. Replace the comma
with a semicolon without adding a newline, and you'll give that
same impression to the compiler. :-)
(Obviously using braces instead wouldn't hurt, and would confuse
fewer people...) 


0
please.ask (434)
6/29/2007 9:27:09 AM
Army1987 said:
> "Richard Heathfield" <rjh@see.sig.invalid> ha scritto nel messaggio
> news:urCdnfENYq1VnuvbRVnytwA@bt.com...
>> Army1987 said:
>>> "Richard Heathfield" <rjh@see.sig.invalid> ha scritto nel messaggio
>>> news:adadnfSH7tIKeenbnZ2dnUVZ8sfinZ2d@bt.com...
>>>> BiGYaN said:
>>>>
>>>> <snip>
>>>>
>>>>> Use "," for writing all the statements in same line.
>>>>
>>>> Why?
>>> That does make sense if you do stuff such as
>>>
>>> if (something)
>>>     free(p), p = NULL;
>>
>> Why does that make sense?
> 
> Put a newline retaining the comma, and you will give the reader the
> impression that p = NULL is always evaluated. Replace the comma
> with a semicolon without adding a newline, and you'll give that
> same impression to the compiler. :-)
> (Obviously using braces instead wouldn't hurt, and would confuse
> fewer people...)

Right, which is why it makes sense to use braces. I still don't see why 
it makes sense to use the more obscure comma operator, when the meaning 
is made much clearer by braces.

-- 
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
0
rjh (10791)
6/29/2007 9:37:00 AM
Richard Heathfield wrote:

> Army1987 said:
>> "Richard Heathfield" <rjh@see.sig.invalid> ha scritto nel messaggio
>> news:urCdnfENYq1VnuvbRVnytwA@bt.com...
>>> Army1987 said:
>>>> "Richard Heathfield" <rjh@see.sig.invalid> ha scritto nel messaggio
>>>> news:adadnfSH7tIKeenbnZ2dnUVZ8sfinZ2d@bt.com...
>>>>> BiGYaN said:
>>>>>
>>>>> <snip>
>>>>>
>>>>>> Use "," for writing all the statements in same line.
>>>>>
>>>>> Why?
>>>> That does make sense if you do stuff such as
>>>>
>>>> if (something)
>>>>     free(p), p = NULL;
>>>
>>> Why does that make sense?
>> 
>> Put a newline retaining the comma, and you will give the reader the
>> impression that p = NULL is always evaluated. Replace the comma
>> with a semicolon without adding a newline, and you'll give that
>> same impression to the compiler. :-)
>> (Obviously using braces instead wouldn't hurt, and would confuse
>> fewer people...)
> 
> Right, which is why it makes sense to use braces. I still don't see why 
> it makes sense to use the more obscure comma operator, when the meaning 
> is made much clearer by braces.

One of the reasons the comma operator is "obscure" is that people
are advised to avoid it (in favour of semicolons and braces) because
it's "obscure".

Braces don't automatically make things clearer -- to some eyes, they
(when unnecessary) look like irrelevant clutter.

-- 
Chris, Dollin;

Hewlett-Packard Limited registered office:                Cain Road, Bracknell,
registered no: 690597 England                                    Berks RG12 1HN

0
chris.dollin (1683)
6/29/2007 9:50:14 AM
Ravi wrote:
> Any statements like
>  printf("abc");
>  scanf("%d",&x);
>  a=20;
> can be replaced by
>  printf("abc"), scanf("%d",&x), a=20;
> 
> So when should we use ; and when sould we use ,
> 
Personally I have only used the comma operator in cases such as those:

More than one variable in a for loop to change each time:
for (i=0,j=1;i<5;i++,j=j*2+j/2)
   a[i]=j;
(with some proper declarations of the variables used of course...)

A function doing something and then checking the result in some way
while (fn(&b),b!=1)
   //do something

This could of course be replaced with
fn(&b);
while (b!=1)
{
   //do something
   fn(&b);
}
But I think the previous form is more clear in some cases
0
qwerty_42 (51)
7/6/2007 9:23:45 AM
Reply: