Question

I was trying to run following code and came across some results. Can somebody please explain:

int number = {12,13,14};
printf("%d",number);

Above code prints output as 12. If I try to run the following code:

int number = (12,13,14);
printf("%d",number);

this prints output as 14, but with following code it prints output as 12:

int number;
number = 12,13;
printf("%d",number);
Was it helpful?

Solution

number = {12,13,14} is scalar initialization, but you are providing more elements than what you should, compiler will throw a warning which, I assume, you have ignored and the first element 12 is assigned to variable number.

When you use number = (12,13,14),

comma (,) operator evaluates the first operand and discards the result, then evaluates the second operand and returns the result.

So here 12 is discarded and 13 is evaluated to 13 and returned. Now this returned 13 value is first operand for next comma operator. Now, it will evaluate 13 to 13 and will discard this result 13 and will evaluate second operand i.e. 14 and will return the result as 14 which is assigned to variable number.

When you do following

  number = 12,13;
  printf("%d",number);

This will print 12, because precedence of = is greater than , so first number = 12 will be evaluated and = return assigned value i.e. 12. Now comma operator got two operands retured value of = operation i.e. 12 and 13. With comma operator 12 will be evaluated as 12 and discarded. 13 is evaluated and returned. Returned to? Nobody. Not assigned to number because assignment is already executed. So number = 12,13; looks like (number=12),13;

Try this as well.

   int num1,num2;
   num2 = (num1 = 13,14);
   printf("%d %d",num1,num2);

This will print 13 14 as per the argument given for earlier code output.

OTHER TIPS

int number = {12,13,14};

number is an int, which doesn't expect an initalizer like this, so illegal C syntax.

int number = (12,13,14);

comma operator returns the right operand as the result, it's equivalent to int number = ((12,13),14), so number has a value of 14.

int number = 12,13;

This is an initialization, not assignment. So comma operator should n't be used here, illegal C syntax.

After your edit, this code

int number;
number = 12,13;

is an expression statement, it's a comma operator, the first operand an assignment number = 12 which assigns number with 12 as side effect, the second operand 13 is an expression statement as well, which does nothing as it has no side effect.

The first expression:

int number = {12,13,14};

is undefined behavior if we look a the draft C99 standard section 6.7.8 Initialization paragraph 2 says:

No initializer shall attempt to provide a value for an object not contained within the entity being initialized.

the intitializer is two elements over in this case. The second case relies on the comma operator which section 6.5.17 paragraph 2 says(emphasis mine):

The left operand of a comma operator is evaluated as a void expression; there is a sequence point after its evaluation. Then the right operand is evaluated; the result has its type and value.97

the second case:

int number = (12,13,14);

is equivalent to:

int number = ((12,13),14);

Following the rules laid out in 6.5.17 The result of (12,13) will be 13 and the result of 13,14 will be 14. In the final case:

number = 12,13;

since = binds tighter(has higher precedence) than , the value 12 will be assigned to number and then 13 will be evaluated with no side effect.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top