By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,101 Members | 1,340 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,101 IT Pros & Developers. It's quick & easy.

when to use ; and when to use ,

P: n/a
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 ,

Jun 16 '07 #1
Share this Question
Share on Google+
13 Replies


P: n/a
In article <11**********************@o11g2000prd.googlegroups .com>,
Ravi <ra*********@gmail.comwrote:
>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 dj******@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
Jun 16 '07 #2

P: n/a

"Ravi" <ra*********@gmail.comwrote in message
news:11**********************@o11g2000prd.googlegr oups.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

Jun 16 '07 #3

P: n/a
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.
Jun 16 '07 #4

P: n/a

"Ravi" <ra*********@gmail.comha scritto nel messaggio
news:11**********************@o11g2000prd.googlegr oups.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.
Jun 16 '07 #5

P: n/a
On Jun 16, 10:26 am, Ravi <ra.ravi....@gmail.comwrote:
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
";".

Jun 17 '07 #6

P: n/a
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.
Jun 17 '07 #7

P: n/a
Army1987 wrote:
>
"Ravi" <ra*********@gmail.comha scritto nel messaggio
news:11**********************@o11g2000prd.googlegr oups.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/

Jun 17 '07 #8

P: n/a

"Richard Heathfield" <rj*@see.sig.invalidha scritto nel messaggio
news:ad******************************@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... :-)
Jun 17 '07 #9

P: n/a
Army1987 said:
>
"Richard Heathfield" <rj*@see.sig.invalidha scritto nel messaggio
news:ad******************************@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.
Jun 18 '07 #10

P: n/a

"Richard Heathfield" <rj*@see.sig.invalidha scritto nel messaggio news:ur*********************@bt.com...
Army1987 said:
>>
"Richard Heathfield" <rj*@see.sig.invalidha scritto nel messaggio
news:ad******************************@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...)
Jun 29 '07 #11

P: n/a
Army1987 said:
"Richard Heathfield" <rj*@see.sig.invalidha scritto nel messaggio
news:ur*********************@bt.com...
>Army1987 said:
>>"Richard Heathfield" <rj*@see.sig.invalidha scritto nel messaggio
news:ad******************************@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
Jun 29 '07 #12

P: n/a
Richard Heathfield wrote:
Army1987 said:
>"Richard Heathfield" <rj*@see.sig.invalidha scritto nel messaggio
news:ur*********************@bt.com...
>>Army1987 said:
"Richard Heathfield" <rj*@see.sig.invalidha scritto nel messaggio
news:ad******************************@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

Jun 29 '07 #13

P: n/a
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
Jul 6 '07 #14

This discussion thread is closed

Replies have been disabled for this discussion.