×

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

Length of an Array Function in C

In C, there is no built-in function to get the length of an array. However, there are a few ways you can determine the length of an array. It is necessary...

15 minutes read.

Fibonacci series in C

We have learned about the Fibonacci series in mathematics. For a quick recap, A Fibonacci series is the sequence of numbers following a certain pattern i.e., the next number should...

3 minutes read.

fork() in C

Introduction To create a new function in the system, there is a need for a system call, i.e. fork system call. It is also known as the child process. These child...

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

Bit Stuffing Program in C

What is a Bit Stuffing? The process of inserting one or more extra bits (0) into data is known as bit stuffing. To provide signalling information to a recipient, these are...

3 minutes read.

Difference between rand() and srand() function in C

What is the rand()?The rand() means random function. This function is used in C. It generates random numbers in the range of 0 to the RAND_MAX. Suppose we generate a...

3 minutes read.

Unformatted input() and output() function in C

Unformatted Input and Output functions take the character, character array, and strings as input. We can read-only character data types with these functions. These functions are already defined in the...

5 minutes read.

Macros in C

In the C programming language, the macro is defined as a segment of the code replaced by the macro defined value at the beginning. ‘#define’ is the directive that defines...

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

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.

Pure Virtual Function in C

Before knowing about the pure virtual function some of the important points about the virtual function in C++ are given below. In c++ the member function that is defined in the...

4 minutes read.

GCD program of two numbers in C

GCD stands for Greatest Common Divisor, which is also known as Highest Common Factor, which can be abbreviated as HCF. Mathematically it can be defined as any two positive integers...

3 minutes read.

C Token

C Token and Keyword The C tokens are the basic buildings blocks in C language which are constructed together to write a C program. Each and every smallest individual unit in the C program is...

2 minutes read.

While-Loop in C

Syntax of While Loop The syntax that has been used for the while loop in C programming language is: while (termination condition) {   // the body of the loop  } Working of While-Loop Initially, we...

3 minutes read.

Types Of Structures In C

We can normally store elements of the same datatype with the help of an array in C programming. We can store multiple numbers of elements of a character data type...

3 minutes read.

Type Casting in C

Type casting is a way to convert a variable from one data type to another data type.Syntax: (type) expression;     Example: #include <stdio.h> int main() { int a,b; float sum; printf("Example of Type Casting\n"); printf("Enter any integer nos:"); scanf("%d",&a,&b); sum=a+b; printf("Sum: %f",sum); return 0; } Output Example...

1 minute read.

Compound Interest Program in C

Interest on loans and deposits is compound interest. This is the most commonly used concept in everyday life. Compound interest on an amount is based on principal and interest earned...

3 minutes read.

What is String Comparison in C

String comparison is the process of comparing two strings (sequences of characters) to determine if they are equal, or if one is greater or less than the other. The comparison...

4 minutes read.

Storage Class in C

C storage class is used to define the scope variables and function. There are four various types of storage classes that are given below. auto: The auto keyword is the default...

1 minute read.

Volatile in C

Introduction A volatile keyword is a qualifier in C. Qualifiers are nothing but keywords which are used to modify the properties of a variable. Qualifiers are of two types: 1) Const The const type...

3 minutes read.