AsegGasiaBlog

C Programming - Operators - Comma Operator

Comma Operator

The comma operator has left-to-right associativity. Two expressions separated by a comma are evaluated left to right. 

The left operand is always evaluated, and all side effects are completed before the right operand is evaluated.

  • The comma operator is mainly useful for obfuscation.
  • The comma operator allows grouping expression where one is expected.
  • Commas can be used as separators in some contexts, such as function argument lists.

Example :


#include <stdio.h>
int main ()
{
    int i = 10, b = 20, c= 30;

    // here, we are using comma as separator
    i = b, c;

    printf("%i\n", i);

    // bracket makes its one expression.
    // all expression in brackets will evaluate but
    // i will be assigned with right expression (left-to-right associativity)
    i = (b, c);
    printf("%i\n", i);

    return 0;
}

Output :

    20
    30

Popular Posts