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
// C Program to Demonstrate
// comma as operator
#include <stdio.h>
int main()
{
int x = 10;
int y = 15;
// using comma as an operator
printf("%d", (x, y));
getchar();
return 0;
}
Time Complexity: O(1)
Auxiliary Space: O(1)
Example 2:
C
// C Program to Demonstrate
// Comma as operator
#include <stdio.h>
int main()
{
int x = 10;
// Using Comma as operator
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
// C program to demonstrate the
// use of comma as an operator
#include <stdio.h>
int main()
{
// using comma without parenthesis
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
// C Program to Demonstrate
// comma as separator and
// as operator
#include <stdio.h>
int main()
{
// Comma separating x=10 and y
int x = 10, y;
// Comma acting as operator
// y= (x+2) and x=(x+3)
y = (x++, printf("x = %d\n", x), ++x,
printf("x = %d\n", x), x++);
// Note that last expression is evaluated
// but side effect is not updated to y
printf("y = %d\n", y);
printf("x = %d\n", x);
return 0;
}
Outputx = 11
x = 12
y = 12
x = 13
Time Complexity: O(1)
Auxiliary Space: O(1)
Example 2:
C
// C Program to
// demonstrate Use of
// comma as a separator
#include <stdio.h>
int main()
{
// i=1 and j=2 are initialized
// uses comma as separator
for (int i = 1, j = 2; i < 10 && j < 10; i++) {
if (i == 5) {
// using comma as separator
i = 6, j = 10;
}
printf("%d %d\n", i, j);
}
return 0;
}
Output1 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
// C Program to illustrate
// the use of comma operator
// in the place of semicolon
#include <stdio.h>
int main(void)
{
printf("First Line\n"),
printf("Second Line\n"),
printf("Third Line\n"),
printf("Last line");
return (0);
}
OutputFirst 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
%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
Command Line Arguments in C
The most important function of C is the main() function. It is mostly defined with a return type of int and without parameters.int main() { ... }We can also give command-line arguments in C. Command-line arguments are the values given after the name of the program in the command-line shell of Operat
4 min read
system() in C/C++
The system() function is used to invoke an operating system command from a C/C++ program. For example, we can call system("dir") on Windows and system("ls") in a Unix-like environment to list the contents of a directory.It is a standard library function defined in <stdlib.h> header in C and
6 min read
isless() in C/C++
In C++, isless() is a predefined function used for mathematical calculations. math.h is the header file required for various mathematical functions.isless() function used to check whether the 1st argument given to the function is less than the 2nd argument given to the function or not. Means if a is
2 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
6 min read
C String Functions
C language provides various 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
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 s
5 min read
scanf in C
In C, scanf() is a function is used to read data from stdin (standard input stream i.e. usually keyboard) and stores the result into the given arguments. It is defined in the <stdio.h> header file.Example:C#include <stdio.h> int main() { int n; // Reading an integer input scanf("%d",
3 min read
getx() function in C
The header file graphics.h contains getx() function which returns the X coordinate of the current position. Syntax : int getx(); Example : Explanation : Initially, the X coordinate of the current position is 0. On moving the coordinates using moveto() function, the X coordinate changes to 80. Below
2 min read