In C, comma ( , ) can be used in three contexts:
- Comma as an operator
- Comma as a separator
- Comma operator in place of a semicolon
1. Comma as an Operator
A comma operator in C++ is a binary operator. It evaluates the first operand & discards the result, evaluates the second operand & returns the value as a result. It has the lowest precedence among all C++ Operators. It is left-associative & acts as a sequence point.
// 10 is assigned to i
int i = (5, 10);
// f1() is called (evaluated)
// first followed by f2().
// The returned value of f2() is assigned to j
int j = (f1(), f2());
Example 1:
C
#include <stdio.h>
int main()
{
int x = 10;
int y = 15;
printf ( "%d" , (x, y));
getchar ();
return 0;
}
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Example 2:
C
#include <stdio.h>
int main()
{
int x = 10;
int y = (x++, ++x);
printf ( "%d" , y);
getchar ();
return 0;
}
|
Time Complexity: O(1)
Auxiliary Space: O(1)
In the above examples, we have enclosed all the operands inside the parenthesis. The reason for this is the operator precedence of the assignment operator over comma as explained below.
Property of Precedence in Comma as an Operator
Consider the following expression:
x = 12, 20, 24;
// Evaluation is as follows
(((a = 12), 20), 24);
The main reason is that the assignment operator has high precedence over the comma operator.
Example:
C
#include <stdio.h>
int main()
{
int x = 12, 20, 24;
printf ( "%d" , x);
return 0;
}
|
Output
error: expected identifier or '(' before numeric constant
int x = 12, 20, 24;
^
2. Comma as a Separator
A comma as a separator is used to separate multiple variables in a variable declaration, and multiple arguments in a function call. It is the most common use of comma operator in C.
// comma as a separator
int a = 1, b = 2;
void fun(x, y);
The use of a comma as a separator should not be confused with the use of an operator. For example, in the below statement, f1() and f2() can be called in any order.
// Comma acts as a separator here
// and doesn't enforce any sequence.
// Therefore, either f1() or f2()
// can be called first
void fun(f1(), f2());
Example 1:
C
#include <stdio.h>
int main()
{
int x = 10, 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 = 11
x = 12
y = 12
x = 13
Time Complexity: O(1)
Auxiliary Space: O(1)
Example 2:
C
#include <stdio.h>
int main()
{
for ( int i = 1, j = 2; i < 10 && j < 10; i++) {
if (i == 5) {
i = 6, j = 10;
}
printf ( "%d %d\n" , i, j);
}
return 0;
}
|
Output
1 2
2 2
3 2
4 2
6 10
Time Complexity: O(1)
Auxiliary Space: O(1)
3. Comma Operator in Place of a Semicolon
We know that in C, every statement is terminated with a semicolon but the comma operator is also used to terminate the statement after satisfying the following rules.
- The variable declaration statements must be terminated with a semicolon.
- The comma operator can terminate the statements after the declaration statement.
- The last statement of the program must be terminated by a semicolon.
Example:
C
#include <stdio.h>
int main( void )
{
printf ( "First Line\n" ),
printf ( "Second Line\n" ),
printf ( "Third Line\n" ),
printf ( "Last line" );
return (0);
}
|
Output
First Line
Second Line
Third Line
Last line
Time Complexity: O(1)
Auxiliary Space: O(1)
Comma as a Separator vs Comma as an Operator
There is a slight difference between a comma as a separator and a comma as an operator. Let us observe using an example:
// Wrong Method
// Comma acts as separator during initialization
// Will generate error
int a = 4, 3;
// Correct Method
// Comma acts as operator
int a;
a = 4,3;
Now the value stored in a will be 3. Also, the following is valid,
int a =(4, 3); // value of a is 3
Similar Reads
Comma in C
In C, comma ( , ) can be used in three contexts: Comma as an operatorComma as a separatorComma operator in place of a semicolon1. Comma as an Operator A comma operator in C++ is a binary operator. It evaluates the first operand & discards the result, evaluates the second operand & returns th
5 min read
C Comments
The comments in C are human-readable explanations or notes in the source code of a C program. A comment makes the program easier to read and understand. These are the statements that are not executed by the compiler or an interpreter. Example: [GFGTABS] C #include <stdio.h> int main() { // Thi
3 min read
%d in C
The format specifiers in C are used in formatted strings to represent the type of data to be printed. Different data types have different format specifiers. %d is one such format specifier used for the int data type. In this article, we will discuss the %d format specifier in the C programming langu
3 min read
Char Comparison in C
Char is a keyword used for representing characters in C. Character size in C is 1 byte. There are two methods to compare characters in C and these are: Using ASCII valuesUsing strcmp( ) .1. Using ASCII values to compare characters The first method is pretty simple, we all know that each character ca
3 min read
LMNs-C Programming
C programming is a powerful and widely-used programming language that forms the backbone of many modern technologies. Known for its simplicity and efficiency, it is the foundation for learning advanced programming concepts. C programming is a powerful and widely-used programming language that forms
7 min read
C String Functions
The C string functions are built-in functions that can be used for various operations and manipulations on strings. These string functions make it easier to perform tasks such as string copy, concatenation, comparison, length, etc. The <string.h> header file contains these string functions. Th
6 min read
strcoll() in C/C++
strcoll() is a built-in library function and is declared in <string.h> header file. This function compares the string pointed to by str1 with the one pointed by str2.The strcoll() function performs the comparison based on the rules of the current locale's LC_COLLATE category. Syntax: int strco
3 min read
Format Specifiers in C
The format specifier in C is used to tell the compiler about the type of data to be printed or scanned in input and output operations. They always start with a % symbol and are used in the formatted string in functions like printf(), scanf, sprintf(), etc. The C language provides a number of format
6 min read
scanf in C
In C, scanf is a function that stands for Scan Formatted String. It is the most used function to read data from stdin (standard input stream i.e. usually keyboard) and stores the result into the given arguments. It can accept character, string, and numeric data from the user using standard input. It
4 min read
atoi() Function in C
In C, atoi stands for ASCII To Integer. The atoi() is a library function in C that converts the numbers in string form to their integer value. To put it simply, the atoi() function accepts a string (which represents an integer) as a parameter and yields an integer value in return. C atoi() function
6 min read