×

Comma Operator in C

What is a comma operator?

The comma operator in the C programming language has the least priority. 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 by those evaluations.

Why comma operator is used?

In the C programming language, the comma symbol primarily serves the dual functions of being an operator and a separator. Thus, depending on where we use it or place it in a program, its behaviour will vary greatly.

C token for the comma (,)

  1. In the C programming language, the comma (,) is used as both an operator and a separator.
  2. In variable declarations, function calls, and function definitions, commas serve as separators.
  3. The comma operator only takes two operands since it is a binary operator.
  4. The Expressions are separated by a comma operator. Expressions that have been split are assessed from left to right.

Comma as an operator

The comma operator evaluates its first operand in a binary operator, tosses the outcome out, and then evaluates its second operand in a binary operator, returning the result. In either C or C++, the comma operator has the lowest precedence.

When we wish to assign numerous numbers of values to any variable in a program with the aid of a comma, we utilize the comma in the form of an operator.

for example

x = 10, 20, 30, 40, 50, 60;

y = (10, 20, 30, 40, 50, 60);

The variable p will have the value 10 in the first of the aforementioned statements. The reason for this is that the equal to assignment operator (=) has a greater priority than the comma operator (,). As a result, the program will assign the value 10 to the variable x.

The value of y will be comparable to 60 in the second sentence. The reason for this is that the numbers 10, 20, 30, 40, 50, and 60 are wrapped in braces (), which have precedence over the equal to (=) assignment operator. Therefore, the program will treat the right-most value as the result or output of this expression when we supply the variable y with several values (using the comma operator and the braces). As a result, the program variable y will be given the value of 60.

Example code

#include<stdio.h>
int main() 
{
   int x = (10, 20);
   int y = (f1(), f2());
}

In this case, x will receive the value 20. The first function in the following statement, f1(), will be run before the second one.

Example program using comma as an operator

#include <stdio.h>
int main()
{
int x,y;
x = 10,20,30;
y = (10,20,30);
printf(“x= %d, y= %d\n”,x,y);
return 0;
}

Output

x = 10, y= 30

Comma as aseparator

The comma is used as a separator in programs when several variables need to be declared and distinct arguments need to be provided for functions.

For example,

x, y, and z;

The comma serves as a separator and informs the compiler that the x, y, and z variables are three distinct types of variables in the aforementioned statement.

Example code

#include<stdio.h>
int main() 
{
int x = 5, y = 10;
void function(x, y);
}

Here, x and y will both be used by the program as function parameters. To learn how to utilize comma operators in the C language, look at the following program.

Example program using comma as a separator

#include<stdio.h>
main() 
{
   int x = 8;
   int y = (x++, ++x);
printf("%d", y);
}

Output

10

Example programs of comma operator in c

Example 1

#include<stdio.h>
int main()
{
        int x = 10, y = 20, z ; //  comma as a Separator
        z = x , y ;      // comma as a Operator
printf(“x = %d\n y = %d\n z = %d”,x,y,z);
        return 0;
}

Output

x = 10
y = 20
z = 10

The comma operator is used in the programme statement z = x, y; above. z is initially given a value. Z becomes 10 as a result, and y is subsequently ignored.

Example 2

#include<stdio.h>
int main()
{
        int x = 10, y = 20, z ; //  comma as a Separator
        z = (x , y) ;      // comma as a Operator
printf(“x = %d\n y = %d\n z = %d”,x,y,z);
        return 0;
}

Output

x = 10
y = 20
z = 20

In the above program because of the parenthesis around x and y, c = (x, y), the output of the aforementioned program, z, contains 20, which denotes the value of y;

Example 3

#include <stdio.h>
int main()
{
	int x = 4, y;
	y = (x++,
		printf("x = %d\n", x),
		++x,
		printf("x = %d\n", x),
		x++);
	printf("y = %d\n", y);
	printf("x = %d\n", x);
return 0;
}

output

x = 5
x = 6
y = 6
x = 7

Example 4

#include <stdio.h>
int main()
{
	int x =4, y;
	y = (x++,
		printf("x = %d\n", x),
		--x,
		printf("x = %d\n", x),
		x--);
	printf("y = %d\n", y);
	printf("x = %d\n", x);
return 0;
}

Output

x = 5
x = 4
y = 4
x = 3

Related Topics

How to use floor() function in C

Introduction: The floor() used as a function in the C programming language. This function uses to return the largest or most significant integer value. The integer value is smaller than...

3 minutes read.

Actual and Formal Parameters

Any variable declared within the parenthesis is referred to as the parameters during the function declaration. Parameters tell the function about the argument datatype, their order, and the number of...

5 minutes read.

Limitations of Inline Function in C

The compiler is unable to conduct inlining in two instances. It just accepts the inline definitions and produces space for the function in the same way it does for a...

3 minutes read.

Nested Loops in C Programming Examples

A nested loop is generally used when we want to run a loop statement inside another loop statement. This kind of loop is also known as a “loop inside the...

6 minutes read.

Bigint (BIG INTEGERS) in C with Example

The maximum number of digits that a long, long int can have in C/C++ is 20. The issue is how to store the 22-digit number, which is difficult to do...

9 minutes read.

User Defined Functions in C

C Functions: A function is defined as a block of code that is used to perform some specific task. In functions, we use flower brackets ({}) which contain the set of programming...

5 minutes read.

Sizeof in C

A pointer in C is defined as a variable that stores the information of the address of another variable. A pointer can be incremented or decremented, which means we can...

4 minutes read.

Java Array vs ArrayList

As we all know, arrays are linear data structures that allow you to add elements to them continuously in memory address space, whereas ArrayList is a Collection framework class. Despite...

4 minutes read.

Fibonacci series program in C using Recursion

In this tutorial, we’ll explore how to utilise recursion to build the Fibonacci sequence in C language. What does the term "Fibonacci Series" mean? The following number in a Fibonacci number series is...

2 minutes read.

Commenting in C

Commenting in C Commenting in the C language is used to give out the information about the lines of the code which are included. It is one of the things that...

3 minutes read.

What is 2s Complement in C?

The complement of 2s in C is generated from the 1s in C. As we know, the 1s complement of a binary number is generated by converting bit 1 to...

4 minutes read.

memcmp() in C

Introduction: In this article we are discuss about memcmp() function in C. This function permits the person to evaluate the bytes of the two characters, as mentioned above. Depending on...

4 minutes read.

C Programming Tutorial

What is C Programming C is most popular and widely used computer programming language. It was developed by Dennis Ritchie in 1972 at the Bell Lab. It is used to develop system application software. There...

8 minutes read.

Armstrong program in C using function

In this article we will learn how to find Armstrong of a number using function in C. An Armstrong number is a three-digit number that is the sum of its separate...

1 minute read.

Call Back Function in Embedded C

Callback function are one the most remarkable systems in C. A callback function is any code that is passed as a contention to another code, so this is the last...

3 minutes read.

Purpose of a Function Prototype in C

A function prototype in C is a declaration of a function that specifies the function's name, return type and parameters. It has the following syntax: return_type function_name(parameter_list); For example, the prototype for...

4 minutes read.

Bank Account System in C using File Handling

This tells about the creation of bank account system using the C language and handling of files in C. Approach Let us see the approaches and the functions how they are covering...

5 minutes read.

Pointers in C

Pointers in C: In the C programming language, a pointer is a pointer variable that points to the address of the other variable. It also stores the address of the...

4 minutes read.

Else If Ladder in C

What is Else If Ladder: When there are multiple options and a user has to decide from the options, we use Else If Ladder. Basically, it is an extension of if...

3 minutes read.

Scope of variables in C

Introduction The scope of variables in C can be defined as the scope of reach of a variable, the term scope is used to determine the visible range of an object....

4 minutes read.