473,386 Members | 1,758 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 ,

Jun 16 '07 #1
13 1224
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

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

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

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

"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Nuno | last post by:
Is there any SQL Error? Or I have to use Select case in VB code to control SQL instead. Thank you for any ans. Nuno
13
by: elad | last post by:
Hi The Menu doesn't work properly when I have 2 frame and the Menu popup frame=document target frame, when I choose item in the menu the doc opened and the menu get stuck. Here is the code...
24
by: Steven T. Hatton | last post by:
In the following code, at what point is S::c fully defined? #include <iostream> using std::cout; using std::endl; using std::ostream; class C { int _v;
7
by: sql-db2-dba | last post by:
Does DB2 just fudge it when it is an empty table? Is there a "formula" for average row size when you have variable length records. Or you really have to know what your application is packing into...
7
by: Nicolae Fieraru | last post by:
Hi All, I am trying to change the rowsource of a combobox when I click on it. I played with many events, associated with the form and the combobox, but still haven't figured out what is the way...
4
by: Peter Row | last post by:
Hi, I have created a UserControl which is subsequently hosted on a standard form. My control has a TabControl on it but it has no TabPages configured. At runtime I create X pages and put a...
5
by: AAguiar | last post by:
I have an asp.net project where the code behind the aspx page calls a c# class which makes calls to a managed static C++ class. The C# class works fine when the asp net worker process starts, when...
8
by: Galina | last post by:
Hello I have 6 dependent list boxes on my ASP page:  Faculty;  Lecturer;  Course;  Course occurrence;  Group;  Week commencing date. When faculty is selected, lists of lecturers and...
44
by: Smokey Grindle | last post by:
I have a list box on my form, but I need to databind it to a data table that is a private member of the form's class... so I basically have Public Class MyForm priate m_MyTable as new datatable...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.