Comma Operator in C

Last Updated :

Prerequisite – Operators in C
The comma operator in C is an operator that allows multiple expressions to be evaluated sequentially in a single statement.

Syntax:
The syntax of the comma operator is as follows:

expr1, expr2, ..., exprn;

Here, expr1, expr2, …, and exprn are expressions that are evaluated in order from left to right. The value of the entire expression is the value of the last expression exprn.

Example:
Here’s an example code that demonstrates the use of the comma operator in C:

#include <stdio.h>

int main() {
    int a = 1, b = 2, c = 3;
    
    // Assign b to a, then assign c to b, and print the value of c
    printf("%d\n", (a = b, b = c, c));
    
    return 0;
}

In this example, we declare three integer variables a, b, and c, with the values 1, 2, and 3, respectively. We then use the comma operator to assign b to a, then assign c to b, and print the value of c.

The expression (a = b, b = c, c) is evaluated from left to right. First, the value of b (2) is assigned to a, then the value of c (3) is assigned to b. Finally, the value of c (3) is returned as the value of the entire expression, which is printed to the console.

The output of this program will be:

3

As you can see, the value of c is printed to the console, which is the value returned by the comma operator.

Please write comments below if you find anything incorrect, or you want to share more information about the topic discussed above. A gentle request to share this topic on your social media profile.

Comment
Next Article
Mithlesh Upadhyay Published 18 Feb, 2023 · 2 min read

Mithlesh Upadhyay is a Computer Science and AI expert from Madhya Pradesh with strong academic background (BE in CSE and M.Tech in AI) and over six years of experience in technical content development. He has contributed tech articles, led teams, and worked in Full Stack Development and Data Science. He founded the w3colleges.org portal for learning resources.

Similar Reads

  • What should we use void main() or int main() ?

    There is question that what should we use void main() or int main() ? void main() { /* ... */ } Or, int main() { /* ... */…

    2 min read
  • C Comments

    Prerequisite – C Language Introduction C comments explain code and improve readability. These do not affect program execution. You can use comments in C to clarify code and describe…

    1 min read
  • C Programming Language Standard

    Prerequisite – C Language Introduction The C programming language has various versions: C89/C90, C99, C11, and C18. C89/C90: Released in 1989/1990. It introduced key language features. C99: This…

    2 min read
  • Features of C Programming Language

    Prerequisite – C Language Introduction C is a simple language made in 1972 by Dennis Ritchie. It’s for system programming. C has low-level memory access, basic keywords, good…

    1 min read
  • C Language Introduction

    C is a programming language developed by Dennis Ritchie in 1972. It has a significant historical impact. It was originally created at Bell Laboratories of AT&T Labs for…

    4 min read
  • Code editors

    Code editors are where programmers spend most of their time. There are two main types of code editors: IDEs and Lightweight editors. IDEs (Integrated Development Environments) IDEs (Integrated…

    1 min read