Comma, an operator or separator

Saturday, January 21, 2017 Unknown 0 Comments

Comma works as a separator and as an operator as well.
Let's take examples where comma acts as a separator and where acts as an operator.

Program 1

int main()
{
int value=100, 101, 102;
printf("value=%d",value);
return 0;
}

In program 1, comma is a separator and will give following compilation Error.
 In function 'main':
6:16: error: expected identifier or '(' before numeric constant
 int value=100, 101, 102;

Program 2

int main()
{
int value=(100, 101, 102);
printf("value=%d",value);
return 0;
}

Output:
value=102
In program 2, comma is an operator. Since bracket is used here, comma will be executed first then bracket and will give output "value=102".

Program 3

int main()
{
int value;
value=100, 101, 102;
printf("value=%d",value);
return 0;
}

Output:
value=100
In program 3, comma is an operator. Here, assignment operator will get precedence over comma and the expression "value=100, 101, 102" will be equivalent to "value=100" and will give output "value=100".

0 comments: