Comma Operator : Lowest Precedence | Returns Righmost Operand



Comma Operator : Lowest Precedence | Returns Righmost

#include<stdio.h>
void main()
{
int a=1,b=2;
int k;
k = (a,b);
printf("%d",k);
}
  1. Comma Operator has Lowest Precedence. [Priority] i.e evaluated at last .
  2. Comma operator returns the value of the rightmost operand.
  3. Comma Operator Can acts as -
    • Operator : In the Expression
    • Separator : Declaring Variable , In Function Call Parameter List

In the Above Example -
1 : Comma as Seperator

int a=1,b=2;

It can acts as Seperator in -

  • Function calls
  • Function definitions
  • Variable declarations
  • Enum declarations

2 : Comma as Operator

k = (a,b);

Different Typical Ways of Comma as Operator :

int a=1,b=2,c;

Way 1 :

c = (a,b);
  • c = Value Stores in b . = 2

Way 2 :

c = a,b;
  • c = Value Stores in a . = 1