Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is Comma Operator in C?
3.
Why Do We Use Comma Operators in C?
3.1.
Uses as an Operator
3.1.1.
Example 1
3.1.2.
Implementation
3.2.
C++
3.2.1.
Example 2
3.2.2.
Implementation
3.3.
C++
3.4.
Uses as a Separator
3.4.1.
Example 1
3.4.2.
Implementation
3.5.
C++
3.5.1.
Example 2
3.5.2.
Implementation
3.6.
C++
4.
Examples of Comma Operators in C
4.1.
Implementation
4.2.
C
5.
Comma Operator in Multiple Initialization
5.1.
Implementation
5.2.
C
6.
Comma Operator in Updating Values
6.1.
Implementation
6.2.
C
7.
Comma Operator in if Condition
7.1.
Implementation
7.2.
C
8.
Comma as a Separator vs Comma as an Operator
9.
Frequently Asked Questions
9.1.
Can we use the comma operator with variables of different data types?
9.2.
Is the order of evaluation always left to right with the comma operator?
9.3.
What are some disadvantages of using the comma operator?
9.4.
How does the precedence of the comma operator compare to other operators in C?
10.
Conclusion
Last Updated: Jun 12, 2024
Easy

Comma Operator in C

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

In the C programming language, the comma operator has the lowest precedence. The comma operator is essentially a binary operator that operates on the first operand that is available, discards the result that is obtained from it, evaluates the operands that are present after this, and then returns the result/value in accordance with those evaluations.

When discussing operators, you rarely encounter the comma operator. The Comma Operator in C evaluates two or more expressions and returns the result of the last expression. Some features of this operator make it different from other operators in C. 

comma operator in c

In this article, we will explore the Comma Operator in C, its syntax, order of evaluation, and use cases.

What is Comma Operator in C?

The comma operator in C is a binary operator that evaluates multiple expressions in a single statement. It returns the result of the last expression. The comma symbol (,) represents the comma operator in code. The comma operator is the operator with the lowest precedence.

The syntax of the comma operator is:

expression1, expression2, expression3, …, expressionN


These expressions evaluate from left to right in sequence, and the result of the expression is returned. The comma operator introduces a sequence point and ensures that the side effects of previous expressions are complete before the execution of the current expression starts.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Why Do We Use Comma Operators in C?

In C, we use the comma operator to separate two or more expressions. Where expression1 comes first, followed by expression2, and the result of expression2 is returned for the full expression. The comma sign is used as an operator and a separator in the C programming language.

Uses as an Operator

As an operator, We use a comma to evaluate two or more expressions and return the result of the last expression. 

Example 1

Let us look at the below example.

Implementation

  • C++

C++

#include <stdio.h>

int main() {
   int a = 3, b = 5;
   // Using comma as an operator
   int x = (a++, b++, (++b, a + b));
   printf("%d\n", x);
}


Output

11


Explanation

In this example, the comma operator evaluates (a++, b++, (++b, a + b)) in order from left to right. First, a’s value increases and becomes 4. Then, b’s value increases and becomes 6. The last expression (++b, a + b) contains two sub-expressions, which are also executed from left to right. Now, b’s value increases again and becomes 7. The comma operator returns the result of the last expression, a + b which is 11.

Note: We can also use the comma operator in an if statement, which executes only when the final expression evaluates to true. Let’s look at an example of this case.

Example 2

Let’s look at an example of the case where we use the comma operator in an if statement.

Implementation

  • C++

C++

#include <stdio.h>

int main() {
   int a = 0, b = 0;
   if (a = 4, b = 5, a + b > 8) {
       printf("Yes\n");
   } else {
       printf("No\n");
   }
}


Output

Yes


Explanation

In this example, the value of a and b was initially 0. Multiple expressions inside the if statement execute in sequential order, and the last condition, i.e. a + b > 8, evaluates to true.

Uses as a Separator

As a separator, We use a comma to separate two or more expressions, variables, or function arguments.

Example 1

Let us look at the below example.

Implementation

  • C++

C++

#include <stdio.h>

int main() {
   // Using comma as a separator for initialisation
   int a = 3, b = 5;
   int x = (a++, b++, (++b, a + b));
   // Using comma as a separator for passing multiple arguments
   printf("%d\n", x);
}


Output

11


Explanation

In this example, we use commas as a separator for initialising multiple variables in a single line. We also use commas to pass three separate arguments to the printf function.

Example 2

As we can use a comma as a separator to separate two expressions and get the desired result, we can use a comma instead of a semicolon. Let’s look at an example of this case.

Implementation

  • C++

C++

#include <stdio.h>

int main() {
   int a = 10, b = 20;

   if (a < b)
       printf("a is less than b.\n"), printf("Statement 1.\n");
   else
       printf("a is not less than b.\n"), printf("Statement 2.\n");

   return 0;
}


Output

a is less than b.
Statement 1.


Explanation

In this example, the printf statements are separated by commas, not semicolons. The second printf statement executes after the first printf statement.

Must Read Passing Arrays to Function in C

Examples of Comma Operators in C

Comma Operator is frequently used with printf and scan in C programming language. Here's an example program that uses the comma operator in the printf() and scanf() statement.

Implementation

  • C

C

#include <stdio.h>

int main() {
int num;
printf("Enter a Number ");
scanf("%d", &num);
printf("\nThe square of number is: ");
printf("%d", num*num);

return 0;
}

 

Output:

Enter a Number:6 
The square of number is: 36

 

Explanation:

This C program asks the user for an integer input using scanf, stores it in the "number" variable, and then prints the entered number using printf.

Comma Operator in Multiple Initialization

Here's an example program that makes multiple use of the comma operator.

Implementation

  • C

C

#include <stdio.h>

int main() {
int temp1=(10,20,30); //correct
int temp2 = 10,20,30; //incorrect
printf("%d %d", temp2, temp1);

return 0;
}

 

Output:

Comma Operator in Multiple Initialization

Explanation:

We might use comma operators like int temp1=(10,20,30) to set the value of b to 30, but not like int temp2=10,20,30 since the = operator takes precedence over a comma.

Comma Operator in Updating Values

Here's an example program that updates values with the comma operator.

Implementation

  • C

C

#include<stdio.h>
int main()
{
int num = 10;

printf("%d %d\n", num, num++);

return 0;
}

 

Output:

11 10

 

Explanation:

The comma operator has having least precedence over than increment operator as a result first value of i is incremented and then printed.

Comma Operator in if Condition

An example program that uses the comma operator in the conditional statement is given below:

Implementation

  • C

C

#include<stdio.h>
int main()
{
int num1 = 50, nums2 = 25;
if(num1 > nums2, num1 < nums2)
printf("if condition is executed");
else
printf("else condition is executed");
return 0;
}

 

Output:

else condition is executed

 

Explanation:

The comma operator inside the if condition functions similarly to the or operator; if the statement is true, it will be placed in the true statement; otherwise, it will be placed in the false statement.

Comma as a Separator vs Comma as an Operator

S.No.Comma as SeparatorComma as Operator
1When used as a separator, comma separates expressions processed sequentially without evaluating their return values. Comma evaluates numerous expressions and returns the value of the last expression when used as an operator. This can be handy when combining multiple expressions to obtain a single value. 
2Its use cases include separating elements in arrays, function arguments, and variable declarations.It is used in expressions to perform multiple operations in a single line.
3When used as a separator it has no impact on operator precedence.When used as an operator it has the lowest precedence among C operators.
4Errors related to comma as a separator are typically related to syntax and formatting.Errors with the comma operator can cause logical problems or unexpected behavior in code.
5Has no effect on a statement's or function's return value.The comma operator produces the result of the last expression, which influences the outcome of an operation.

Must Read Decision Making in C

Frequently Asked Questions

Can we use the comma operator with variables of different data types?

Yes, we can use the comma operator with variables of different data types. However, the resulting expression will have the type of the rightmost expression.

Is the order of evaluation always left to right with the comma operator?

Yes, the order of evaluation is always from left to right with the comma operator.

What are some disadvantages of using the comma operator?

Some disadvantages of using the comma operator include reduced readability and maintainability of code and the potential for unintended side effects.

How does the precedence of the comma operator compare to other operators in C?

The comma operator in c comes with the lowest precedence in the C language. The comma operator is basically a binary operator that initially operates the first available operand, discards the obtained result from it, evaluates the operands present after this, and then returns the result/value accordingly.

Conclusion

This article discussed the comma operator in C and its uses. Using the comma operator carefully is crucial as it might make the code harder to read and understand. 

To learn more about C, we recommend reading the following articles:

Also check out the Interview guide for Product Based Companies as well as some of the Popular interview problems from top tech companies like Amazon, Adobe, Google, Uber, Microsoft, etc.

Refer to our guided pathways on Code studio to learn more about DSACompetitive ProgrammingJavaScriptSystem DesignDSA C++etc. Enroll in our courses, and use the accessible sample exams and questions as a guide. For placement preparations, look at the interview experiences and interview package.

Happy Coding!

Previous article
Sequence Points in C
Next article
Execution of printf with ++ operators
Live masterclass