×

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

Positioning of file in C

Positioning() function in c The fseek() function is utilized to change the document position of the stream . The fully extent of value from the source should be one of the...

3 minutes read.

Types of Array in C

What is an Array? One value can be stored in a variable at once. How many variables will you need if you have 100 values? Well, the answer isn't 100. It...

14 minutes read.

FIFO Example in the C Language

FIFO is an acronym that means "First In, First Out." It is a data structure handling method in which the first element is handled first, and the last element is...

3 minutes read.

What is required in each C Program?

Each C program must require one function, i.e., main() function. It is because when we execute the C program, C compiler looks for the main() function, and from here only...

5 minutes read.

Difference between while and do-while loop in C

Introduction Both 'While' and 'Do-While' loops in C mostly have a similar concept, and the code of both loops runs for mainly similar purposes. Both loops process the program/code in the...

3 minutes read.

Palindrome Number in C

What is a Palindrome? A number that doesn't change when reversed is known as a palindrome. We reverse a number and compare it to the original number to determine whether it is...

4 minutes read.

fopen() function in C

fopen() function, is one of the file handling functions. It is used to open the existing file and perform operations on the file. If the file does not exist, it...

3 minutes read.

Union Program in C

 How should a union be defined?  In C programming, a Union is a method used to organize different data types into groups in the same way that Structures are used. We...

4 minutes read.

Booleans in C

Introduction to Boolean With all the complexities of programming, it can take time to understand the basics. One concept, in particular, that confuses many beginners is the use of Booleans in...

6 minutes read.

C Program for Mean and Median of an Unsorted Array

In this tutorial, we will look at how to determine the mean and median of a given unsorted array. To determine the Mean: To get the average, mean is determined. The formula...

2 minutes read.

Dynamic memory allocation

Dynamic memory allocation is used to allocate the memory at runtime. It has following function within <stdlib.h> header file. Function Syntax malloc() malloc (number *sizeof(int)); calloc() calloc (number, sizeof(int)); realloc() realloc (pointer_name, number * sizeof(int)); free() free (pointer_name); malloc() function The process...

2 minutes read.

Exit control loop in C

To examine the termination condition for an exit, we usually use an exit control loop in C. If the evaluation for termination condition results in true, then the control would...

3 minutes read.

Multilevel Feedback Queue Scheduling (MLFQ) CPU Scheduling

In this article, you will learn how to initialise an array to 0 in C In C, an array is declared as: char ZEROARRAY[2022]; The global scope changes at runtime to all zeros....

3 minutes read.

Merge Sort in C

The merge sort follows the principle of the divide and conquers algorithm. Firstly it divides the input of an array into two halves and then calls itself for the two...

4 minutes read.

Variables in C

A variable can be defined as a name allocated to a storage space that can be manipulated by our programs. Every variable in C arbitrates about the overall layout and...

5 minutes read.

Difference between If and Switch Statement in C

Key Contrast: In the event that assertion is utilizes a Boolean articulation to execute the capability and can frequently be utilized to really look at various circumstances all at once.  The change...

4 minutes read.

C Program Swap Numbers in cyclic order Using Call by Reference

The three integers that the user entered in cyclical sequence are swapped in this tutorial using call by reference. C Program: #include <stdio.h> void cyclic_Swap ( int *X, int *Y, int *Z ); int...

2 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.

Entry Control Loop in C

Initially, an entry control loop verifies the termination condition at the entry point. After that, its control passes to the main body of the while or for loop if the...

3 minutes read.

Built-in functions in C

The function is a set of instructions and statements enclosed in the "{}" delimiter. In c, there are two types of functions. Pre-define functions/ Built-in functionsUser define function. Built-in functions in C:- These...

8 minutes read.