Sunday, June 14, 2015

Comma as Sequential-Evaluation Operator in C/C++


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

Syntax

expression:
             assignment-expression
             expression , assignment-expression

The left operand of the sequential-evaluation operator is evaluated as a void expression. The result of the operation has the same value and type as the right operand. Each operand can be of any type. The sequential-evaluation operator does not perform type conversions between its operands, and it does not yield an l-value. There is a sequence point after the first operand, which means all side effects from the evaluation of the left operand are completed before beginning evaluation of the right operand.

The sequential-evaluation operator is typically used to evaluate two or more expressions in contexts where only one expression is allowed.

Commas can be used as separators in some contexts. However, you must be careful not to confuse the use of the comma as a separator with its use as an operator; the two uses are completely different.

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.

A sequence point defines any point in a computer program's execution at which it is guaranteed that all side effects of previous evaluations will have been performed, and no side effects from subsequent evaluations have yet been performed.
/* comma as an operator */

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

int j = (func1(), func2());  /* func1() is evaluated first followed by func2(). The returned value of func2() is assigned to j */

2) Comma as a separator:

Comma also 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 func(x, y);

The use of comma as a separator should not be confused with the use as an operator. For example, in below statement, func1() and func2() 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 func(func1(), func2());

Have a look at this :- http://stackoverflow.com/questions/2087026/effect-of-using-a-comma-instead-of-a-semi-colon-in-c-and-c

See the programs below to check your understanding of comma in C.
#include<stdio.h>
int main()
{
   int x = 10;
   int y = 15;

   printf("%d", (x, y));
   return 0;
}


#include<stdio.h>
int main()
{
    int x = 10, y;

    //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;
}

0 comments :

Post a Comment

Follow Me!

Blog Archive

Followers

Visitor Map