when to use ; and when to use ,

R

Ravi

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 ,
 
D

Dave Vandervies

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)
 
M

Malcolm McLean

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 ,
#
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.
 
R

Richard Heathfield

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.
 
A

Army1987

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 ,
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.
 
B

BiGYaN

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;
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
";".
 
R

Richard Heathfield

BiGYaN said:

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>
 
C

Chris Dollin

Army1987 said:
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]).
 
A

Army1987

Richard Heathfield said:
BiGYaN said:



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.)

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... :)
 
R

Richard Heathfield

Army1987 said:
That does make sense if you do stuff such as

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

Why does that make sense?

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.
 
A

Army1987

Richard Heathfield said:
Army1987 said:


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...)
 
R

Richard Heathfield

Army1987 said:
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.
 
C

Chris Dollin

Richard said:
Army1987 said:

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.
 
J

Johan Bengtsson

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 ,
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=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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,434
Messages
2,571,685
Members
48,796
Latest member
Greg L.

Latest Threads

Top