Home Ask Login Register

Developers Planet

Your answer is one click away!

akumar36 February 2016

C data type declaration query

In the C programming language, on assigning 4,3 to an integer-type variable, like:

int a;

a = 4,3;

the variable receives the value to the left of the comma (i.e. a is set to 4 in the example). On assigning a parenthesized list, however, like

a = (4,3);

, the variable takes the last value in the comma-delimited list (3 in the example).

What is the reason for this?


c_mnon February 2016

Yes . That would be the correct operation.

   i = (a, b);             // stores b into i 
                          // ... a=1, b=2, c=3, i=2
   i = a, b;               // stores a into i. Equivalent to (i = a), b;
                          // ... a=1, b=2, c=3, i=1

Check property of comma operator. "In the C and C++ programming languages, the comma operator (represented by the token ,) is a binary operator that evaluates its first operand and discards the result, and then evaluates the second operand and returns this value (and type)." - wikipedia

John Bollinger February 2016

C uses the comma (,) in two distinct ways: as an element separator in compound constructs such as array literals or lists of declarations, and as a binary operator. It does not have list-based assignment such as some higher-level languages do.

As an operator, the comma evaluates its left-hand operand first, then evaluates its right-hand operand. The value of the overall expression is the second result. This is in some ways complementary to the && and || operators, both of which evaluate their left-hand operands first, but each of which evaluates its right-hand operand only conditionally, depending on the left-hand result.

The other key to understanding your observation is that the equals sign (=) is also an operator. It evaluates both operands, in unspecified order, and its result is the same as the right-hand operand. Modifying the value of the left-hand operand is a side effect.

The assignment operator has very low precedence, but the comma operator has the lowest precedence of all. Therefore, if you do not use parentheses to alter evaluation order, this ...

a = 4,3;

... is equivalent to ...

(a = 4), 3;

. It evaluates the assignment first, yielding the value 4 with the side effect of assigning that value to variable a. Then it discards that value and evaluates 3, yielding the value 3 as the overall result. Since the whole thing is not part of any larger expression, the result is discarded.

On the other hand, you can override precedence with suitable use of parentheses, as in this:

a = (4, 3);

. In that case, the comma operator is evaluated first, yielding 3 as its result, and that is the right-hand operand of the assignment operator. The assignment expression yields result 3

dlmeetei February 2016

In C/C++, comma(,) operator is evaluated left-to-right, and the value of the left expression is discarded. The type and value of the result are the type and value of the right operand. All side effects from the evaluation of the left-operand are completed before beginning the evaluation of the right operand. Taken from KR book A.7.18

Also, comma operator has the least precedence and order of evaluation.

As = operator has higher precedence than , operator. In following statement

int a;
a = 4,3;

assignment happens first resulting in a = 4.

and also as we all know, () overrides the precedence, so we have a = 3 in case of

int a = (4,3);

Now, We all understood the theory of , and =, Let us concentrate when to use ,. Comma operator should be used sparingly, The most suitable uses are for constructs strongly related to each other. for instance in for loop

for (i = 0, j = strlen(s)-1; i < j; i++, j--)

Wherever possible, I will use () in conjunction with ,

Post Status

Asked in February 2016
Viewed 1,859 times
Voted 5
Answered 3 times


Leave an answer

Quote of the day: live life