0

I am experimenting with this C code:

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

It prints 6. Same is the case with below code:

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

But how this code is generating the output:

#include <stdio.h>
int main()
{
  int a = (2, 3, 4, 5);  // or {2, 3, 4, 5} in place of parenthesis
  printf("%d", a--);
  printf("%d", --a);
  
  return 0;
}

It prints 53. Why it is considering the last value 5 for int variable a (when it is enclosed in parenthesis)? Also, why 2 for int variable a when it is enclosed in curly braces?

mediocrevegetable1
  • 4,086
  • 1
  • 11
  • 33
  • Enclosed in parentheses, you're getting the comma operator. Look that up. – Ben Voigt Oct 06 '21 at 17:52
  • First question: [duplicate](https://stackoverflow.com/questions/52550/what-does-the-comma-operator-do). Second question: [I don't believe that compiles](https://godbolt.org/z/YbcfKEsPf). – Drew Dormann Oct 06 '21 at 17:58
  • @DrewDormann second code is working with warning in this [platform](https://www.onlinegdb.com/) – Abhishek Sinha Oct 06 '21 at 18:08

0 Answers0