May 18, 2018

Comma Operator in C


   In C programming language, comma (,) can be used in two contexts :

  • As a separator 
  • As an operator



Comma as a Separator

Comma acts as a separator when used with function calls and definitions, variable declarations, enum declarations, and similar constructs. An example will make everything clear.

int x, y, z;

In this statement, comma is a separator and tells the compiler that these (x, y, and z) are three different variables.

Let's take some more examples.

int a=5, b=3; /* A comma separated variable declaration list. */

fun(x,y)  /* Comma-separated argument list. In fact, x and y can be evaluated in any order. */

void my_fun(int a, int b)  /* Comma-separated parameter list. */



Comma as an Operator

The comma operator (represented by the token , ) is a binary operator that takes two operands. It works by evaluating the first operand and discarding its value, and then evaluates the second operand and returns the value (and type) as the result of the expression. Comma-separated operands when chained together are evaluated in left-to-right sequence with the right-most value yielding the result of the expression. Among all the operators, the comma operator has the lowest precedence.


Example 1:

int a,b,sum; /* Comma as a separator. */ 
sum = (a = 4, b = 3, a+b); /* Comma as an operator. */


Here we have combined three expressions. Initially 4 is assigned to the variable a, then 3 is assigned to the variable b. At last a+b is evaluated and the result of the overall expression (i.e. the rightmost expression) is assigned to sum. Now the value of sum is 7

Note : Since precedence of comma operator is lower than that of assignment operator, the parentheses are necessary here.


Comma as an operator


The comma operator (,) helps us to make our code more compact, without the use of comma operator, the above task would have been done in 3 statements.

a=4;
b=3;
sum = a+b;



The following program demonstrates how to use comma operator(,).

#include <stdio.h>

int main(void)
{
    int a, b, sum; /* Comma as a separator. */
    sum = (a=4, b=3, a+b); /* Comma as an operator. */
    printf("Sum = %d\n", sum);

    return 0;
}


Output:

Sum = 7


Example 2:

int a=3,b=2,sum=0; /* Comma as a separator. */ 
sum = (++a, b += a); /* Comma as an operator. */


Here we have combined two expressions, let's see how it works. At first the value of a is incremented by 1. At last b+=a is evaluated and the result of the overall expression (i.e. the rightmost expression) is assigned to sum. Now the value of sum is 6

The following program demonstrates how to use comma operator(,).

#include <stdio.h>

int main(void)
{
    int a=3, b=2, sum=0; /* Comma as a separator. */
    sum = (++a, b += a); /* Comma as an operator. */
    printf("Sum = %d\n", sum);

    return 0;
}


Output:

Sum = 6



Example 3:

int a;
a = (10, 20, 30); /* Comma as an operator. */


In the above example the rightmost value is assigned to a. So the value of a is 30.

The following program demonstrates how to use comma operator(,).

#include <stdio.h>

int main(void)
{
    int a; 
    a = (10, 20, 30); /* Comma as an operator. */
    printf("Value of a = %d\n", a);

    return 0;
}


Output:

Value of a = 30


Note :

When this line is executed:

a = 10,20,30;

what value does a end up with? Well, we know that the value of a comma operator is the value of the rightmost operand. But here, assignment operator has higher precedence, so you actually get:

(a=10), 20, 30; /* a gets the value 10 */

a gets the value 10; then the literals 20 and 30 are evaluated and thrown away, a ends up being 10, not 30.


Example 4:

printf("Just","Coding");

You might feel that answer of this statement should be "Coding" because comma operator always returns the result of rightmost operand, but in this case output is "Just". In case of printf statement once comma is read then it will consider preceding things as variable or values for format specifier.

Consider the following program:

#include <stdio.h>

int main(void)
{
 
   
printf("Just", "Coding");
    printf("\n");
    printf("Just %s", "Coding");

    return 0;
}


Output:

Just
Just Coding




Using result of comma operator as l-value is not valid in C. But in C++, result of comma operator can be used as l-value if the right operand of the comma operator is l-value.

Consider the following program:

#include <stdio.h>

int main(void)
{
  int x = 5, y = 10;
 (x, y) = 15; /* Since y is l-value, this statement is valid in C++, but not in C. */
  printf("y = %d", y);
 
  return 0;
}


If we compile this program as a C++ program, then it works and prints y = 15. And if we compile this program as C program, then it gives warning/error in compilation.



next    Bitwise Operators and sizeof() Operator in C
top    Index
prev    Conditional Operator in C





No comments:

Post a Comment