Output of C programs | Set 52

1. What will be the output of following program?

#include <stdio.h>
int main()
{
    int a = 5, *b, c;
    b = &a;
    printf("%d", a * *b * a + *b);
    return (0);
}

Options:
1. 130
2. 103
3. 100
4. 310

 The answer is the option(1).

Explanation:
Here the expression a**b*a + *b uses pointer in C/C++ concept. Here a**b*a + *b means 5*(value of pointer b that is 5)*5 +(value at pointer b which is 5 again).
So the result is 130.

2. What will be the output of following program?

#include <stdio.h>
int main()
{
    int i, j = 3;
    float k = 7;
    i = k % j;
    printf("%d", i);
    return (0);
}

Options:
1. No output
2. compile time error
3. Abnormal termination
4. 1

The answer is option(2).

Explanation:Here k is floating-point variable and we can’t apply % operator in floating-point variable.The modulo operator % in C and C++ is defined for two integers only, but there is an fmod() function available for usage with doubles.
Refer : http://www.geeksforgeeks.org/can-use-operator-floating-point-numbers/

3. What will be the output of following program?

#include <stdio.h>
int main()
{
    int a;
    int b = 5;
    a = 0 && --b;
    printf("%d %d", a, b);
}

Options:
1. 0 4
2. compile time error
3. 0 5
4. syntax error

The answer is option(3).

Explanation:In the logical AND operator, if any of the condition is false then the whole result is false. Here 0 acts as a false value in c therefore the whole result is false and –b is not executed. Therefore the result is 0 5.

4. What will be the output of following program?

#include <stdio.h>
int main()
{
    int a = 0;
    while (a < 5) {
        printf("%d\\n", a++);
    }
}

Options:
1. No output
2. 0\n\1\n\2\n\3\n\4\n
3. 0\n1\n2\n3\n4\n
4. compilation error

The answer is option(3).

Explanation:Here, the while loop is going to execute 5 times. We know that a++ is post increment and in post-increment we first assign then increment.when first time while loop execute, while(0<5) the printf function contains \\n which acts as a backslash escape character. Therefore it prints 0\n in the first loop, 1\n in the 2nd loop, 3\n in the 3rd loop and so on.
 
5. What will be the output of following program?

#include <stdio.h>
int main()
{
    int x = 5;
    if (x >= 10)
        printf("Hello");
    printf("GFG");
    else printf("hi");
}

Options:
1. No output
2. hi
3. HelloGFG
4. compilation error

The answer is option(4).

Explanation:It will give compilation error because when there is only one statement in the if clause, then curly braces are not required but if there are more than one statement then we must enclose with curly braces and here we are not giving curly braces. Therefore we will get compile time error with a message else without a previous if. It is a problem of handing if statement which is not allowed in 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

Recommended Posts:







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