Comma Operator in C Program

Comma Operator in C – A comma operator is used to separate a pair of expressions.

A pair of expressions separated by a comma is evaluated left to right, and the type and value of the results are the value of the type and value of the right operand.

All side effects from the evaluation of the left operand are completed before beginning evaluation of the right operand. The left side of

The left side of the comma operator is always evaluated to void. This means that the expression on the right hand side becomes the value of the total comma-separated expression.

For example,
x = (y=2, y – 1);

First assigns y the value 2 and then x the value 1. Parenthesis is necessary since comma operator has lower precedence than assignment operator.

Generally, comma operator (,) is used in the for loop

For example,

for (i = 0,j = n;i<j; i++,j–)
{
printf (“A”);
}

In this example for is the looping construct (discussed in the next unit). In this loop, i = 0 and j = n are separated by comma (,) and i++ and j—are separated by comma (,).

The example will be clear to you once you have learnt for loop (will be introduced in the next unit).

Essentially, the comma causes a sequence of operations to be performed.

When it is used on the right hand side of the assignment statement, the value assigned is the value of the last expression in the comma-separated list.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: