Predict the output:
1. int c = (3<4)2 34.3+4,
2. int a = 14, b=4,
boolean x = (a>b)? true false,
3. int x = 90;
char c = (x<=90)? "Z:T';
4. int a = 18; int b = 12;
boolean t = (a>20&& b< 15)? true false
5. c = (val + 550 < 1700)? 200-400,
if: (a) val = 1000
Answers
Predict the output of below programs
Question 1
int main()
int main() {
int main() { while(1){
int main() { while(1){ if(printf("%d",printf("%d")))
int main() { while(1){ if(printf("%d",printf("%d"))) break;
int main() { while(1){ if(printf("%d",printf("%d"))) break; else
int main() { while(1){ if(printf("%d",printf("%d"))) break; else continue;
int main() { while(1){ if(printf("%d",printf("%d"))) break; else continue; }
int main() { while(1){ if(printf("%d",printf("%d"))) break; else continue; } return 0;
int main() { while(1){ if(printf("%d",printf("%d"))) break; else continue; } return 0; }
int main() { while(1){ if(printf("%d",printf("%d"))) break; else continue; } return 0; } Output:
int main() { while(1){ if(printf("%d",printf("%d"))) break; else continue; } return 0; } Output:Can’t be predicted
Explanation:
The condition in while loop is 1 so at first shot it looks infinite loop. Then there are break and continue in the body of the while loop, so it may not be infinite loop. The break statement will be executed if the condition under if is met, otherwise continue will be executed. Since there’s no other statements after continue in the while loop, continue doesn’t serve any purpose. In fact it is extraneous. So let us see the if condition. If we look carefully, we will notice that the condition of the if will be met always, so break will be executed at the first iteration itself and we will come out of while loop. The reason why the condition of if will be met is printf function. Function printf always returns the no. of bytes it has output. For example, the return value of printf(“geeks”) will be 5 because printf will output 5 characters here. In our case, the inner printf will be executed first but this printf doesn’t have argument for format specifier i.e. %d. It means this printf will print any arbitrary value. But notice that even for any arbirary value, the no. of bytes output by inner printf would be non-zero. And those no. of bytes will work as argument to outer printf. The outer printf will print that many no. of bytes and return non-zero value always. So the condition for if is also true always. Therefore, the while loop be executed only once. As a side note, even without outer printf also, the condition for if is always true.
int main()
{
unsigned int i=10;
while(i-- >= 0)
printf("%u ",i);
return 0;
}
Output:
9 8 7 6 5 4 3 2 1 0 4294967295 4294967294 …… (on a machine where int is 4 bytes long)
9 8 7 6 5 4 3 2 1 0 65535 65534 …. (on a machine where int is 2 bytes long)
int main()
{
int x,y=2,z,a;
if ( x = y%2)
z =2;
a=2;
printf("%d %d ",z,x);
return 0;
}
Output:
< some garbage value of z > 0
Question 4
int main()
{
int a[10];
printf("%d",*a+1-*a+3);
return 0;
}
Question 5
#define prod(a,b) a*b
int main()
{
int x=3,y=4;
printf("%d",prod(x+2,y-1));
return 0;
}
Output:
10
pls mark me as brainlist ✔️✔️
Answer:
1. 12
2. false
3. Z
4. false
5. (a) 200
(b) 400