C Programming Comma Operator



In this tutorial we will be learning comma operator in C programming. It is special kind of operator which is widely used in programming to separate the declaration of multiple variable.

We have listed some of the hidden secrets of Comma operator and how it can be used in programming in efficient manner.

C Programming Comma Operator

#include<stdio.h>
void main() {
   int num1 = 1, num2 = 2;
   int res;
   res = (num1, num2);
   printf("%d", res);
}

Explanation :

  1. Comma Operator has Lowest Precedence i.e it is having lowest priority so it is evaluated at last.
  2. Comma operator returns the value of the rightmost operand when multiple comma operators are used inside an expression.
  3. Comma Operator Can acts as –
    • Operator  : In the Expression
    • Separator : Declaring Variable , In Function Call Parameter List

Usage of Comma Operator

Consider above example –

Comma as Separator

int num1 = 1, num2 = 2;

It can acts as Seperator in –

  1. Function calls
  2. Function definitions
  3. Variable declarations
  4. Enum declarations

Comma as Operator

res = (num1, num2);

In this case value of rightmost operator will be assigned to the variable. In this case value of num2 will be assigned to variable res.

Examples of comma operator :

Comma Operator have lowest priority in C Programming Operators. All possible operations that can be performed on  comma operator are summarized below –

Verity 1 : Using Comma Operator along with Assignment

#include<stdio.h>
int main()
{
          int i;
          i = 1,2,3;
          printf("i:%d\n",i);
          return 0;
}

Output :

i:1

Explanation :

i = 1,2,3;
  1. Above Expression contain 3 comma operator and 1 assignment operator.
  2. If we check precedence table then we can say that “Comma” operator has lowest precedence than assignment operator
  3. So Assignment statement will be executed first .
  4. 1 is assigned to variable “i”.

Verity 2 : Using Comma Operator with Round Braces

#include<stdio.h>
int main()
{
          int i;
          i = (1,2,3);
          printf("i:%d\n",i);
          return 0;
}

Output :

i:3

Explanation :

i = (1,2,3);
  1. Bracket has highest priority than any operator.
  2. Inside bracket we have 2 comma operators.
  3. Comma operator has associativity from Left to Right.
  4. Comma Operator will return Rightmost operand
i = (1,2,3)
==> 1,2 will return 2
==> 2,3 will return 3
==> means (1,2,3) will return 3
==> Assign 3 to variable i.

Verity 3 : Using Comma Operator inside printf statement

#include<stdio.h>
#include< conio.h>
void main()
{
clrscr();
printf("Computer","Programming");
getch();
}

Output :

Computer

You might feel that answer of this statement should be “Programming” because comma operator always returns rightmost operator, in case of printf statement once comma is read then it will consider preceding things as variable or values for format specifier.

Verity 4 : Using Comma Operator inside Switch cases.

#include<stdio.h>
#include< conio.h>
void main()
{
 int choice = 2 ;
 switch(choice)
 {

  case 1,2,1:
        printf("\nAllas");
        break;

  case 1,3,2:
        printf("\nBabo");
        break;

  case 4,5,3:
        printf("\nHurray");
        break;
 }
}

Output :

Babo

Verity 5 : Using Comma Operator inside For Loop

#include<stdio.h>

int main()
{
int i,j;

for(i=0,j=0;i<5;i++)
  {
  printf("\nValue of J : %d",j);
  j++;
  }

return(0);
}

Output :

Value of J : 0
Value of J : 1
Value of J : 2
Value of J : 3
Value of J : 4

Verity 6 : Using Comma Operator for multiple Declaration

#include<stdio.h>

int main()
{
int num1,num2;
int a=10,b=20;

return(0);
}

Note : Use of comma operator for multiple declaration in same statement.

You can also refer wikipedia to learn more about comma operator.