In C and C++, comma (, ) can be used in two contexts:
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.
2) Comma as a separator:
Comma acts as a separator when used with function calls and definitions, function like macros, variable declarations, enum declarations, and similar constructs.
The use of comma as a separator should not be confused with the use as an operator. For example, in below statement, f1() and f2() can be called in any order.
See this for C vs C++ differences of using comma operator.
You can try below programs to check your understanding of comma in C.
3) Comma operator in place of a semicolon.
We know that in C and C++, every statement is terminated with a semicolon but comma operator also used to terminate the statement after satisfying the following rules.
- The variable declaration statements must be terminated with semicolon.
- The statements after declaration statement can be terminated by comma operator.
- The last statement of the program must be terminated by semicolon.
First Line Second Line Third Line Last line
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Result of comma operator as l-value in C and C++
- Comma operator should be used carefully
- A comma operator question
- How to input a comma separated string in C++?
- Program to Parse a comma separated string in C++
- How can we use Comma operator in place of curly braces?
- Strict Aliasing Rule in C with Examples
- Difference between cout and puts() in C++ with Examples
- Difference between forward list and list in C++
- Short-Circuiting in C++ and Linux
- Different types of range-based for loop iterators in C++
- Learn C++ Programming Step by Step - A 20 Day Curriculum!
- Difference between C and Dart
- Difference between Sentinel and Counter Controlled Loop in C