Skip to content
Related Articles

Related Articles

A comma operator question

Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 29 Sep, 2022
Improve Article
Save Article

Consider the following C programs. 

C




// PROGRAM 1
#include<stdio.h>
 
int main(void)
{
    int a = 1, 2, 3;
    printf("%d", a);
    return 0;
}


The above program fails in compilation, but the following program compiles fine and prints 1. 

C




// PROGRAM 2
#include<stdio.h>
 
int main(void)
{
    int a;
    a = 1, 2, 3;
    printf("%d", a);
    return 0;
}


And the following program prints 3, why? 

C




// PROGRAM 3
#include<stdio.h>
 
int main(void)
{
    int a;
    a = (1, 2, 3);
    printf("%d", a);
    return 0;
}


In a C/C++ program, comma is used in two contexts: (1) A separator (2) An Operator. (See this for more details). Comma works just as a separator in PROGRAM 1 and we get compilation error in this program. Comma works as an operator in PROGRAM 2. Precedence of comma operator is least in operator precedence table. So the assignment operator takes precedence over comma and the expression “a = 1, 2, 3” becomes equivalent to “(a = 1), 2, 3”. That is why we get output as 1 in the second program. In PROGRAM 3, brackets are used so comma operator is executed first and we get the output as 3 (See the Wiki page for more details).

Now can you guess the output of this program without looking below the answer?

C




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


Output

2

If you are not able to understand then here is the explanation:

  1. Firstly the code inside the brackets is evaluated, so we get 2. 
  2. Then the code becomes a = 2, 3 which means (a = 2), 3 as explained earlier. 
  3. Therefore the output is 2.

 Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!