What does a comma separated list of values, enclosed in parenthesis mean in C? a = (1, 2, 3); [duplicate]

StackOverflow https://stackoverflow.com/questions/16563235

  •  29-05-2022
  •  | 
  •  

Question

I've just come across code that essentially does the following:

int a = (1, 2, 3);

I've never seen this notation before. What does it mean?

Was it helpful?

Solution

This is the comma operator: evaluation of a, b first causes a to be evaluated, then b, and the result is that of b.

int a = (1, 2, 3); first evaluates 1, then 2, finally 3, and uses that last 3 to initialise a. It is useless here, but it can be useful when the left operand of , has side effects (usually: when it's a function call).

OTHER TIPS

It uses the comma operator, which just evaluates each operand expression sequentially (introducing proper sequence points in between) and returns the last one. Thus your example is actually equivalent to int a = 3;.

But it is indeed one of the least used operators in C and C++ and not to be confused with the commas used in function call expressions, initializer lists, and all the other places. A not so rare use-case would be multiple increments in for loops (for(...; ...; ++i,++j)), even though you probably never thought about this actually using a so-called comma operator.

Another interresting use case is when trying to put multiple conceptually related expressions into a single statement (like a return) for the sake of clarity and conciseness, like in an implementation of the good old frexp with its weird pointer return argument (ignore the fact that proper C++ would just return a pair):

double frexp(double arg, int *exp)
{
    if(...)
        return *exp=..., result;
    ...
}

which is much more streamlined than the equivalent

double frexp(double arg, int *exp)
{
    if(...)
    {
        *exp = ...;
        return result;
    }
    ...
}

Wiki: Comma operator

i = (a, b, c);          // stores c into i

It is comma operator. C11 standard tells about one use case of this kind of operator.

C11 standard 6:5:17

Comma operator

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

The comma operator (as described in this subclause) cannot appear in contexts where a comma is used to separate items in a list (such as arguments to functions or lists of initializers). On the other hand, it can be used within a parenthesized expression or within the second expression of a conditional operator in such contexts. In the function call f(a, (t=3, t+2), c) the function has three arguments, the second of which has the value 5.

This is the comma operator. It "wraps" multiple expressions, evaluates them from left to right, and the value of the whole expression is determined by the last sub-expression. In your example, it evaluates to 3.

A situation where the comma operator is especially handy is if you want to do multiple things in a for-loop "increment" expression, for example to increment two variables.

Example: Iterate an image along the diagonal, using x and y as separate variables. I use two separate variables for x and y because I might want to change one of them in the loop independently from the other (remember, it's just a stupid example). So I want to increment both x and y in the "increment" statement of the for-loop:

for(int x = 0, y = 0; x < width && y < height; ++x, ++y) {
    // ...                                     ^^^^^^^^
}

Note that the "initialization" expression of the for-loop does not use the comma operator; it just declares two variables.

It just evaluates 1, 2 and 3 (since they are only values, but could as well be functions calls), and sets the value (or return value) of the last one to the left operand (in your example, a).

Maybe this will help you understand how it works:

#include <stdio.h>

int toto()
{
  printf("toto()\n");
  return (21);
}

int tata()
{
  printf("tata()\n");
  return (42);
}

int main()
{
  int a = (toto(), tata());
  printf("%d\n", a);
  return (0);
}

Output:

toto()
tata()
42

Edit: Tha's C code, works the same in C++

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