Output of C programs | Set 53 (Operators)

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: http://www.geeksforgeeks.org/type-conversion-c/

This article is contributed by Bishal Kumar Dubey. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



0 Average Difficulty : 0/5.0
No votes yet.










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.