Ages of three children
October 19, 2012
Sum the digits of a number in single statement
October 20, 2012

comma operator

Comma in C and C++ comes in two flavours

  1. As separator (used to separate parameters in a function call)
  2. As operator (rarely used)

The two flavors are as different as it can be. Lets discuss them one by one:

1. Comma as separator:

When comma is used to separate arguments in a function call, the order of evaluation of arguments (separated by comma) is not defined. To understand more about Order of evaluation of operands, see this post.

For example: Consider the below code. What do you think will be the output?

// Global variable
int x = 0;
int a(){
    x = 10;
    return 2;
}
int b(){
    x = 20;
    return 3;
}
int sum(int x, int y){
    return x + y;
}
int main(){
    int var = sum(a(), b());
    printf("%d", x);
}

Note that we are printing the value of x and not what is returned from any function.

Function a() and b() are called during the resolution of arguments of function sum().

int var = sum(a(), b());

If functions are called from LEFT to RIGHT

If function a() is called first, then it will set the value of x (global variable) to 10. Then function b() is called which will set the value of x to 20. Hence, the final value of x will be 20.

If functions are called from RIGHT to LEFT

If function b() is called before function a(), then the final value of x will be 10.

Hence, the value of x depends on the order in which parameters of function sum() are getting evaluated (which are separated by comma).

But, when comma is used as a separator, the order in which two expressions (separated by comma) are evaluated is NOT DEFINED.

Hence, the value is UNDEFINED. (It will be defined by the compiler).

2. Comma as operator:

When comma is used as an operator, the operands will be evaluated from LEFT to RIGHT, and hence the order is strictly defined. If a() and b() functions are defined as above, then the value of var in the below code will be 20 (and NOT Undefined),

int var = (a(), b());

because in this case a() will be called first and then function b() will be called (Order of evaluation of operands for comma operator is Left to Right). The value of var in below code will be 10

int var = (5, 10);

Result of comma operator:

In C language the result of comma operator should not be used as an l-value. But in C++ it can be used as an l-value.

For example: in the below code of C lagnuage

// C-LANGUAGE Code
int x=5, y=10
(x, y) = 20;
printf("%d", y);  // Undefined in C-Language

The output will be undefined in C language. Because the result of comma operator cannot be used as l-value.

But a similar code will be fine in C++

// C++ Code
int x=5, y=10
(x, y) = 20;
cout<< y;  OK. Will print 20

The output will be 20 in this case.

2 Comments

  1. louisborder says:

    actually i donot have any idea about c and c++ i want to learn c and c++ can you give the book’s names which is useful to me
    and give some suggestions on C language
    thank’s
    job Description

  2. Kamal Rawat says:

    Our book on C language will be available in 2-3 months.. If you are in Greater Noida, I am planning free tutorials in C/C++ language.. for C++ you may read ‘Primer’

Leave a Reply to Anonymous Cancel reply

Your email address will not be published. Required fields are marked *