Question 1 |
Predict the output of following program. Assume that the numbers are stored in 2's complement form.
#include<stdio.h> int main() { unsigned int x = -1; int y = ~0; if (x == y) printf("same"); else printf("not same"); return 0; }
same | |
not same |
Discuss it
Question 1 Explanation:
-1 and ~0 essentially have same bit pattern, hence x and y must be same. In the comparison, y is promoted to unsigned and compared against x (See this for promotion rules). The result is “same”. However, when interpreted as signed and unsigned their numerical values will differ. x is MAXUNIT and y is -1. Since we have %u for y also, the output will be MAXUNIT and MAXUNIT.
Question 2 |
Which of the following is not a valid declaration in C?
1. short int x;
2. signed short x;
3. short x;
4. unsigned short x;
3 and 4 | |
2 | |
1 | |
All are valid |
Discuss it
Question 2 Explanation:
All are valid. First 3 mean the same thing. 4th means unsigned.
Question 3 |
Predict the output
#include <stdio.h> int main() { float c = 5.0; printf ("Temperature in Fahrenheit is %.2f", (9/5)*c + 32); return 0; }
Temperature in Fahrenheit is 41.00 | |
Temperature in Fahrenheit is 37.00 | |
Temperature in Fahrenheit is 0.00 | |
Compiler Error |
Discuss it
Question 3 Explanation:
Since 9 and 5 are integers, integer arithmetic happens in subexpression (9/5) and we get 1 as its value.
To fix the above program, we can use 9.0 instead of 9 or 5.0 instead of 5 so that floating point arithmetic happens.
Question 4 |
Predict the output of following C program
#include <stdio.h> int main() { char a = '12'; printf("%d", a); return 0; }
Compiler Error | |
12 | |
10 | |
Empty |
Discuss it
Question 4 Explanation:
The value '