Output of C programs | Set 51

1. What will be the output of the below program?

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
int main()
{
    printf("%d", printf("%d", printf("%d", printf("%s", "Welcome to geeksforgeeks"))));
    return (0);
}

chevron_right


Options:
1. Welcome to geeksforgeeks2531
2. Welcome to geeksforgeeks2421
3. Welcome to geeksforgeeks2124
4. Welcome to geeksforgeeks3125

The answer is option(2).

Explanation : As we all know that the printf function returns the numbers of character that is going to print and scanf function returns the number of input given.

2. What will be the output of the following program?

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
int main()
{
    int x = 30, y = 25, z = 20, w = 10;
    printf("%d ", x * y / z - w);
    printf("%d", x * y / (z - w));
    return (0);
}

chevron_right


Options:
1. 27 85
2. 82 27
3. 27 75
4. No output



 The output is option(3).

Explanation : In the above program, precedence takes the advantage. Therefore In the statement compiler first calculate x*y then / and after that subtraction.
Refer : https://www.geeksforgeeks.org/c-operator-precedence-associativity/

3. Guess the output?

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Options:
1. a=63 b=33
2. a=33 b=63
3. a=66 b=33
4. a=33 b=33

The answer is option(1).

Explanation:Here,

a = 15 and b = (a++)+(a++) i.e. b = 15+16 = 31 and a =3 2.
Now a = (b++) + (b++) = 31 + 32 = 63 and b = 33.
Therefore the result is a = 63 and b = 33.

Refer: https://www.geeksforgeeks.org/operators-in-c-set-1-arithmetic-operators/

4. What will be the output?

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
int main()
{
    main();
    return (0);
}

chevron_right


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

The answer is option(1).

Explanation:Here stack overflow occurs that’s result in run-time error.
Refer: https://www.geeksforgeeks.org/print-geeksforgeeks-empty-main-c/

5. What will be the output of following program?

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
int main()
{
    int c = 4;
    c = c++ + ~++c;
    printf("%d", c);
    return (0);
}

chevron_right


Options:
1. 3
2. -3
3. 31
4. compile time error

 The answer is option(2).

Explanation:Because here c++ is post increment and so it takes value as 4 then it will increment. ++c is pre-increment so it increment first and value 6 is assigned to c, .~ means -6-1=-7 then c=(4-7)=-3.

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.



My Personal Notes arrow_drop_up

Improved By : ManasChhabra2



Article Tags :

1


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