Comma operator

In the previous section of operator and expressions I had discussed about sizeof operator in C. Today I am going to discuss about Comma Operator.

Comma Operator in programming languages like C,C++ is a binary operator.It is represented by token ( , ). This operator combines multiple line statements into single line and these statements are separated by comma operator. So let us convert this thought into programming.

================================================================================

OUTPUT:

================================================================================

In the above example for swapping two values three expression has been used in three different lines terminated by semicolon(semicolon tokens is used to terminate the line).

Now let us have a look to this program, this program will also swap two integer values using comma operator.

================================================================================

OUTPUT:

================================================================================

You might have observed instead of all those three lines used in example 1 ,in this program I have written those three lines in a single line  and each expression is  separated by comma operator.

Evaluation of comma operator:

Expressions separated by comma operator are evaluated from left to right and the result of the rightmost expression becomes the result of the whole expression .

For example

In the above example variable a is assigned with value 10,b is assigned with value 20,c is assigned with value 30,and the result of the rightmost expression a+b+c i.e. 10+20+30=60 will be assigned to the variable res.

Let us have a look to this program it will clear you more about above example:

================================================================================

OUTPUT:

================================================================================

LeEco Le Max2 (Rose Gold, 32 GB)

Galaxy on Note

Swipe Elite 2 Plus (Ivory White, 8 GB)


 

Leave a Reply