Comma

In C and C++, comma (,) can be used in two contexts:

  1. Comma as an operator:
    The comma operator (represented by the token ,) is a binary operator that evaluates its first operand and discards the result, it then evaluates the second operand and returns this value (and type). The comma operator has the lowest precedence of any C operator, and acts as a sequence point.
/* comma as an operator */

int i = (5, 10); /* 10 is assigned to i*/

int j = (f1(), f2()); /* f1() is called (evaluated) first followed by f2().

The returned value of f2() is assigned to j */

  1. Comma as a separator:
    Comma acts as a separator when used with function calls and definitions, function like macros, variable declarations, enum declarations, and similar constructs.

/* comma as a separator */

int a = 1, b = 2;

void fun(x, y);

The use of comma as a separator should not be confused with the use as an operator. For example, in below statement, f1() and f2() can be called in any order.

/* Comma acts as a separator here and doesn't enforce any sequence.
Therefore, either f1() or f2() can be called first */

void fun(f1(), f2());

You can try below programs to check your understanding of comma in C.

// PROGRAM 1
#include<stdio.h>
int main()
{
   int x = 10;
   int y = 15; 
  
   printf("%d", (x, y));
   getchar();
   return 0;
}
// PROGRAM 2:  Thanks to Shekhu for suggesting this program
#include<stdio.h>
int main()
{
   int x = 10;
   int y = (x++, ++x);
   printf("%d", y);
   getchar();
   return 0;
}
// PROGRAM 3:  Thanks to Venki for suggesting this program
int main()
{
    int x = 10, y;
  
    // The following is equavalent to y = x++
    y = (x++, printf("x = %d\n", x), ++x, printf("x = %d\n", x), x++);
  
    // Note that last expression is evaluated
    // but side effect is not updated to y
    printf("y = %d\n", y);
    printf("x = %d\n", x);
  
    return 0;
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容