Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Effect of using a comma instead of a semi-colon in C and C++

Tags:

c++

c

I've noticed on a number of occasions when refactoring various pieces of C and C++ code that a comma is used rather than a semi-colon to seperate statements. Something like this;

int a = 0, b = 0; a = 5, b = 5; 

Where I would have expected

int a = 0, b = 0; a = 5; b = 5; 

I know that C and C++ allow use of commas to seperate statements (notably loop headers), but what is the difference if any between these two pieces of code? My guess is that the comma has been left in as the result of cut & pasting, but is it a bug and does it effect execution?

like image 507
SmacL Avatar asked Jan 18 '10 15:01

SmacL


People also ask

Why do we use comma in C?

The comma operator in c comes with the lowest precedence in the C language. The comma operator is basically a binary operator that initially operates the first available operand, discards the obtained result from it, evaluates the operands present after this, and then returns the result/value accordingly.

Why do we use semicolons if we could just use commas and periods?

Rule to Remember Use a semicolon to replace a comma when you use a coordinating conjunction to link independent clauses that already contain commas. In this example, using a semicolon makes it easier to read the two independent clauses on either side of the coordinating conjunction: Correct: My dog is sick.

What is the difference between comma and semi colon with the help of an example?

When a comma separates two complete sentences joined by a conjunction (and, but, or, nor, for, so, or yet) the comma and the conjunction can be replaced with a semicolon. I ate dinner, and I went to the movies. = I ate dinner; I went to the movies. She finished top of her class, but she was struggling to find work.

Why are semicolons important in C?

Semicolons are end statements in C. The Semicolon tells that the current statement has been terminated and other statements following are new statements. Usage of Semicolon in C will remove ambiguity and confusion while looking at the code.


2 Answers

It doesn't make a difference in the code you posted. In general, the comma separates expressions just like a semicolon, however, if you take the whole as an expression, then the comma operator means that the expression evaluates to the last argument.

Here's an example:

b = (3, 5); 

Will evaluate 3, then 5 and assign the latter to b. So b = 5. Note that the brackets are important here:

b = 3, 5; 

Will evaluate b = 3, then 5 and the result of the whole expression is 5, nevertheless b == 3.

The comma operator is especially helpful in for-loops when your iterator code is not a simple i++, but you need to do multiple commands. In that case a semicolon doesn't work well with the for-loop syntax.

like image 99
Frank Avatar answered Sep 19 '22 08:09

Frank


The comma is a operator that returns a value which is always the 2nd (right) argument while a semicolon just ends statements. That allows the comma operator to be used inside other statements or to concatenate multiple statements to appear as one.

Here the function f(x) gets called and then x > y is evaluated for the if statement.

if( y = f(x), x > y ) 

An example when it's used just to avoid a the need for block

if( ... )    x = 2, y = 3;  if( ... ) {    x = 2;    y = 3; } 
like image 35
x4u Avatar answered Sep 20 '22 08:09

x4u