Output of C programs | Set 36
1. What will be the output of following?
void main()
{
int const * p = 5;
printf ( "%d" , ++(*p));
}
|
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?
void main()
{
char * p;
printf ( "%d %d" , sizeof (*p), sizeof (p));
}
|
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?
void main()
{
int m, i = 0, j = 1, k = 2;
m = i++ || j++ || k++;
printf ( "%d %d %d %d" , m, i, j, k);
}
|
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?
void main()
{
int i = 0;
printf ( "%d %d" , i, i++);
}
|
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?
#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;
}
|
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].
Last Updated :
19 Jun, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...