Conditional Operator, Comma operator and sizeof() operator in C
Last updated on July 27, 2020
Conditional Operator #
The conditional operator (?
and :
) is a special operator which requires three operands. Its syntax is as follows:
Syntax: expression1 ? expression2 : expression3
Here is how the conditional operator works.
The first expression1
is evaluated, if it is true then the value of expression2
becomes the result of the overall expression. On the other hand, if expression1
is false, then the value of expression3
becomes the result of the overall expression.
Let's take an example:
1 2 | int a = 5, b = 3;
a > b ? a : b
|
In the above expression, a>b
is true, so the value of variable a
becomes the result of the overall conditional expression.
Since a > b ? a : b
is an expression, we can assign its value to a variable.
max = a > b ? a : b
The conditional operator also sometimes known as the Ternary operator.
The following program demonstrates how to find the greatest of two numbers using the conditional operator
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #include<stdio.h>
int main()
{
int a, b, max;
printf("Enter a and b: ");
scanf("%d%d", &a, &b);
max = a > b ? a : b;
printf("Largest of the two numbers = %d\n", max);
// Signal to operating system everything works fine
return 0;
}
|
Expected Output:
1 2 | Enter a and b: 1993 1534
Largest of the two numbers = 1993
|
The precedence of the conditional operator is quite lower than the arithmetic, logical and relational operators. But it is higher than the assignment and compound assignment operator. The associativity of conditional operator is from right to left (see Operator Precedence in C).
Consider the following conditional expression:
x ? y : a ? b : c
In this case, the expression3
is itself a conditional expression. Further, since the conditional operator associates from right to left, the above expression is equivalent to:
x ? y : (a ? b : c)
If the value of x
is true (i.e. non-zero) then the value of the entire expression will be y
. Otherwise, the value of the entire expression will be (a ? b : c)
.
Comma operator #
The Comma operator allows us to place one or more expression where C syntax allows only one expression. Each expression must be separated using the comma ( ,
) and are evaluated from left to right. The value of rightmost expression becomes the value of the overall expression. An example will make everything clear.
a=2, a++, a+10
Here we have combined three expressions, let's see how it works. At first 2
is assigned to variable a
, then the value of a
is incremented by 1
. At last, a+10
is evaluated. So the value of the overall expression is 13
.
Let's take one more example.
sum = (a=3, b=4, c=5, a+b+c);
Here first, 3
is assigned to variable a
, then 4
is assigned to variable b
, 5
is assigned to variable c
. At last a+b+c
is evaluated and the result of the overall expression is (i.e the rightmost expression) assigned to sum
.
The precedence of comma operator ( ,
) is the lowest and it associates from left to right (see Operator Precedence and Associativity in C). For this reason, parentheses in the above expression is necessary, otherwise, the variable sum
would be assigned a value of 3
.
The comma operator ( ,
) helps us to make our code more concise. Without the use of the comma operator, the above task would need at least 2 statements.
1 2 | a=3, b=4, c=5;
sum = a+b+c;
|
The following program demonstrates the comma operator( ,
) in action:
1 2 3 4 5 6 7 8 9 10 | #include<stdio.h>
int main()
{
int a, b, c, sum;
sum = (a=3, b=4, c=5, a+b+c);
printf("Sum = %d\n", sum);
// Signal to operating system everything works fine
return 0;
}
|
Expected Output:
Sum = 12
sizeof operator #
The sizeof
is a unary operator used to determine the size of its operand. The general form of sizeof
operator is:
sizeof(object)
where object can be data type keywords like int
, float
, double
or expression or variable.
For example, sizeof(int)
gives the size occupied by an int
data type. The sizeof
operator returns size in bytes.
The following program demonstrates how to use sizeof()
operator to check the size of fundamental types on your system.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #include<stdio.h>
int main()
{
printf("Size of short = %lu\n", sizeof(short));
printf("Size of int = %lu\n", sizeof(int));
printf("Size of unsigned int = %lu\n", sizeof(unsigned int));
printf("Size of char = %lu\n", sizeof(char));
printf("Size of float = %lu\n", sizeof(float));
printf("Size of double = %lu\n", sizeof(double));
printf("Size of long double = %lu\n", sizeof(long double));
// Signal to operating system everything works fine
return 0;
}
|
Expected Output:
1 2 3 4 5 6 7 | Size of short = 2
Size of int = 4
Size of unsigned int = 4
Size of char = 1
Size of float = 4
Size of double = 8
Size of long double = 16
|
Since C is quite flexible in terms of storage requirements. The output of the above program may differ on your machine.
The precedence of sizeof
operator is same as the prefix increment/decrement operator and it associates from right to left (see Operator Precedence and Associativity in C ).
Load Comments