Open In App

Output of C programs | Set 53 (Operators)

Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite: Operators in C

1. What will be the output of the following?




#include <stdio.h>
int main(void)
{
    int a;
    int b = 1;
    int x[5] = { 1, 2, 3, 4, 5 };
    a = 5 * 4 + x[--b] - (9 / b);
    printf("%d", a);
    return 0;
}


Options:
1. 12
2. Abnormal Termination
3. 21
4. No Output

 The answer is option(2).

Explanation : Here the square bracket has the highest priority that’s why it is evaluated first. After that parenthesis enjoys the priority but we have error because float point exception occurs when we divide 9/0.

2. What will be the output of the following?




#include <stdio.h>
int main(void)
{
    int a;
    int b = 1;
    int x[5] = { 1, 2, 3, 4, 5 };
    a = 5 * 4 + x[b++] - (9 / b);
    printf("%d", a);
    return 0;
}


Options:
1. 12
2. 20
3. 18
4. No Output

 The answer is option(3).

Explanation:Here the square bracket has the highest priority that’s why it is evaluated first. After that parenthesis enjoys the priority.Now a=5*4+x[b++]-(9/b)=18.
Refer: www.geeksforgeeks.org/c-operator-precedence-associativity/

3. what will be the output of the following?




#include <stdio.h>
int main(void)
{
    int a;
    int i = 1;
    int b = 10 * i + sizeof(--i) + 4 - 10 / i;
    printf("%d", b);
    return 0;
}


Options:
1. 4
2. 2
3. 6
4. 8

 The answer is option(4).

Explanation:Here the sizeof() operator enjoys the highest priority, which result in 4. But evaluation is not possible inside sizeof() operator.

4. What will be the output of the following?




#include <stdio.h>
int main(void)
{
    int a = 9;
    float x;
    x = a / 2;
    printf("%f", x);
    return 0;
}


Options:
1. 4.000000
2. 0
3. 4
4. No output

 The answer is option(1).

Explanation: Here 9/2 which is 4. Then 4 is assigned to float and it becomes 4.000000.

5. What will be the output of the following?




#include <stdio.h>
int main(void)
{
    int a = 9;
    float x;
    x = (float)a / 2;
    printf("%f", x);
    return 0;
}


Options:
1. 4.000000
2. 4.50
3. 4
4. 4.500000

 The answer is option(4).

Explanation: With the help of type-casting we are able to get the exact result.
Reference: https://www.geeksforgeeks.org/type-conversion-c/



Last Updated : 06 Sep, 2017
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads