Why does this use of comma work in a expression but fail in a declaration?

后端 未结 3 810
独厮守ぢ
独厮守ぢ 2021-01-13 08:48

Am from high level OOP languages C# and Java and recently started scratching my head in C. I feel C a bit weird as equally as one feels JS is. So want to clarify below:

3条回答
  •  爱一瞬间的悲伤
    2021-01-13 09:42

    1.

      int i, j;
      i = 3,0,1,2;
      printf("%d\n", i); => prints 3
    

    2.

      i = 3,j =7;
      printf("%d, %d\n", i, j); => prints 3 and 7
    

    i = 3,0,1,2; This assigns 3 to i, then executes 0, 1 and 2. Check the 2nd example I mentioned.

    Also, Try i=3;0;1;2; This will not report any error. It will just execute (i=3), (0), (1) and (2).

提交回复
热议问题