In this tutorial, we are going to learn about the comma and sizeof operators in C.
In C, there are two special operators: the comma (,) operator and the sizeof operator.
The Comma(,) operator is used to declare multiple expressions in a single statement. Comma operator acts as a separator between multiple expressions, in declaring multiple variables and in function call parameters.
Given below are a few examples of comma(,) operator:
int a = 0, b = 0, c = 0; a = 17; b = 35; c = a + b;
The example given above can also be written as:
int a = 0, b = 0, c = 0; c = (a = 17, b = 3, a + b);
In the expression result = (expression1, expression2); expression1 is first evaluated. After that, expression2 is evaluated and the value of expression2 is returned for the whole expression.
sizeof operator is also called the compile-time operator and when used with an operand, will return the number of bytes that are occupied by the operand.
The operand may be a variable, a constant, or a data type. The format of the sizeof operator is:
sizeof operator will return the size as an unsigned integer.
Since sizeof returns an unsigned integer, %zu has to be used instead of %d for the format string.
For example, the statement
will produce an output of 4 since a float data type occupies 4 bytes in memory.