# Conditional Operator, Comma operator and sizeof() operator in C

## 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:

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.

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

Expected Output:

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:

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:

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.

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.

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.

The following program demonstrates the comma operator( `,` ) in action:

Expected Output:

## sizeof operator

The `sizeof` is a unary operator used to determine the size of its operand. The general form of `sizeof` operator is:

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.

Expected Output:

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