Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

Comma : Sequential-Evaluation Operator

, 18 May 2014
Rate this:
Please Sign up or sign in to vote.
Comma as a Sequential-Evaluation Operator in C
In C and C++, comma (,) can be used in two ways:

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 */ 
/* 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 :- Page on stackoverflow.com

See the programs below to check your understanding of comma in C.

Example 1:

#include<stdio.h>
int main()
{ 
    int x = 10; 
    int y = 15; 
    printf("%d", (x, y));
    getchar(); 
    return 0;  
}   

Example 2:

#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); 
    getchar(); 
    return 0; 
} 

References:

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Psycho_Coder
Student
India India
My name is Animesh Shaw, Currently I am pursuing B. Tech in Computer Science. My Screen name is Psycho_Coder. I am active in many Security or Ethical Hacking forums. I love programming and exploring the depths of Computer Science.
 
Programming Languages know : C, C++,Java, Python and PHP.
 
You can even contact me on the following :-
 
1, CS - Computer Science Lovers : https://www.facebook.com/CSComputerScienceLovers. Please like it.
 
2. CS(Computer Science) Lovers : https://www.facebook.com/groups/csloversforever/
My facebook group to discuss programming. You can join this group if you are serious about computer science and programming.
 
3. You can mail me at psychocoder@outlook.com
 

My Interests :
 
I have great interests in the following topics in Computer Science :-
 
1. Programming Language Concepts and Methodologies.
2. Computational Linguistics.
3. Cryptography.
 
Github Profile : https://github.com/PsychoCoderHC
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 19 May 2014
Article Copyright 2014 by Psycho_Coder
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid