Comma Operator in C Programming Language

Notes:

Comma Operator in C Programming Language:
- is used to group related expressions together and it returns the result of the rightmost expression

Syntax:
(expression1, expression2, …. , rightmost expression)

Ex: Program to find addition of 2 numbers

#include <stdio.h>
int main()
{
int a=0, b=0, c=0;
c = (a=10, b=20, c=a+b);
printf("c= %d\n", c);
return 0;
}

Note: Comma symbol is most commonly used
- while declaring variables and constants
- with looping statements
- while passing arguments to a function etc.
where it just behaves like a separator (i.e. comma separator)
Ex:
int a, b, c; // declaring variables
int a=0, b=0, c=0; // declaring and initializing variables
for(i=0,j=1; j<=10; i++, j++){} // for loop
printf(“%d + %d = %d”, 2 , 2, 2+2); // function call