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

Conditional operator

Conditional operator ( ? and : ) is a special operator which requires three operands. Here is the syntax of conditional operator:

Syntax: expression1 ? expression2 : expression3

Here is how conditional operator works.

The first expression1 is evaluated, if it is true then the value of expression2 becomes the value of the overall expression.

On the other hand, if expression1 is false, then the value of expression3 becomes the value of the overall expression.

Let's take an example:

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 value 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

This operator is also known as Ternary operator.

The following program demonstrates how to find greatest of two numbers using conditional operator

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

Enter a and b: 1993 1534
Largest of the two numbers = 1993

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.

Note: Parentheses is necessary here because precedence of the comma operator is less than assignment operator.

So as you can see comma operator (,) helps us to make our code more concise, without the use of comma operator, the above task would need at least 2 statements.

a=3, b=4, c=5;
sum = a+b+c;

The following program demonstrates how to use comma operator(,).

#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

sizeof() is an unary operator used to determine the size of its operand, which may be constants, variable or expression. We can even pass data type keywords like int, float, double etc. For example, sizeof(int) gives the size occupied by an int data type. The sizeof() perator returns size in bytes.

The following program demonstrates how you use sizeof() operator to check the size of fundamental types on your system.

#include<stdio.h>

int main()
{
    printf("Size of int = %d\n", sizeof(int));
    printf("Size of char = %d\n", sizeof(char));
    printf("Size of float = %d\n", sizeof(float));
    printf("Size of double = %d\n", sizeof(double));
    // Signal to operating system everything works fine
    return 0;
}

Expected Output:

Size of int = 4
Size of char = 1
Size of float = 4
Size of double = 8