Output of C programs | Set 36

1. What will be the output of following?

filter_none

edit
close

play_arrow

link
brightness_4
code

void main()
{
    int const* p = 5;
    printf("%d", ++(*p));
}

chevron_right


Options:
A. 6
B. 5
C. Garbage Value
D. Compiler Error

Answer : D

Explanation : It will give compile time error because we are trying to change the constant value. p is a pointer to a “constant integer”. But we tried to change the value of the “constant integer”.

2. What will be the output of following?

filter_none

edit
close

play_arrow

link
brightness_4
code

void main()
{
    char* p;
    printf("%d %d", sizeof(*p), sizeof(p));
}

chevron_right


Options:
A. 1 1
B. 1 8
C. 2 1
D. 2 2

Answer : B

Explanation : The sizeof() operator gives the number of bytes taken by its operand. P is a character pointer, which needs one byte for storing its value (a character). Hence sizeof(*p) gives a value of 1. Since it needs two bytes to store the address of the character pointer sizeof(p) gives 8.

3. What will be the output of following?

filter_none

edit
close

play_arrow

link
brightness_4
code

void main()
{
    int m, i = 0, j = 1, k = 2;
    m = i++ || j++ || k++;
    printf("%d %d %d %d", m, i, j, k);
}

chevron_right


Options:
A. 1 1 2 3
B. 1 1 2 2
C. 0 1 2 2
D. 0 1 2 3

Answer : B

Explanation : In an expression involving || operator, evaluation takes place from left to right and will be stopped if one of its components evaluates to true(a non zero value).
So in the given expression m = i++ || j++ || k++.
It will be stop at j and assign the current value of j in m.
therefore m = 1, i = 1, j = 2 and k = 2 (since k++ will not encounter. so its value remain 2)

4. What will be the output of following?

filter_none

edit
close

play_arrow

link
brightness_4
code

void main()
{
    int i = 0;
    printf("%d %d", i, i++);
}

chevron_right


Options:
A. 0 1
B. 1 0
C. 0 0
D. 1 1

Answer : B

Explanation : Since the evaluation is from right to left. So when the print statement execute value of i = 0, as its executing from right to left – when i++ will be execute first and print value 0 (since its post increment ) and after printing 0 value of i become 1.

5. What will be the output of following?

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
int main(void)
{
    char p;
    char buf[10] = { 1, 2, 3, 4, 5, 6, 9, 8 };
    p = (buf + 1)[5];
    printf("%d", p);
    return 0;
}

chevron_right


Options:
A. 5
B. 6
C. 9
D. Error

Answer : C

Explanation : x[i] is equivalent to *(x + i), so (buf + 1)[5] is *(buf + 1 + 5), or buf[6].

This article is contributed by Rishabh Jain. 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 : Akanksha_Rai



Article Tags :

5


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