Output of C programs | Set 56 (While loop)
Last Updated :
25 Sep, 2017
Prerequisite :
While loops
Q.1 What is the output of this program?
CPP
#include <iostream>
using namespace std;
int main()
{
unsigned int x = 3;
while (x-- >= 0) {
printf("%d ", x);
}
return 0;
}
Option
a) 3 2 1 0
b) 2 1 0 -1
c) infinite loop
d) -65535
Answer : C
Explanation: Here x is an unsigned integer andit can never become negative. So the expression x-->=0 will always be true, so its a infinite loop.
Q.2 What is the output of this program?
CPP
#include <iostream>
using namespace std;
int main()
{
int x = 3, k;
while (x-- >= 0) {
printf("%d ", x);
}
return 0;
}
option:- a
a) 3 2 1 0
b) 2 1 0 -1
c) infinite loop
d) -65535
Answer: b
Explanation: Here x is an integer with value 3. Loop runs till x>=0 ; 2, 1, 0 will be printed and after x>=0, condition becomes true again and print -1 after false.
Q.3 What is the output of this program?
CPP
#include <iostream>
using namespace std;
int main()
{
int x = 0, k;
while (+(+x--) != 0) {
x++;
}
printf("%d ", x);
return 0;
}
option
a) 1
b) 0
c) -1
d) infinite
Answer : C
Explanation Unary + is the only dummy operator in c++. So it has no effect on the expression.
Q.4 What is the output of this program?
CPP
#include <iostream>
using namespace std;
int main()
{
int x = -10;
while (x++ != 0)
;
printf("%d ", x);
return 0;
}
option
a) 0
b) 1
c) -1
d) infinite
Answer: b
Explanation: The semicolon is after the while loop. while the value of x become 0, it comes out of while loop. Due to post-increment on x the value of x while printing becomes 1.
Q.5 What is the output of this program?
CPP
#include <iostream>
using namespace std;
int main()
{
while (1) {
if (printf("%d", printf("%d")))
break;
else
continue;
}
return 0;
}
option
a) Garbage value
b) 1
c) 0
d) Error
Answer : a
Explanation: The inner printf executes and print some garbage value.
Similar Reads
Output of C programs | Set 61 (Loops) Prerequisite : Loops in C Q.1 What is the output of this program? CPP #include <iostream> using namespace std; int main() { int i, j, var = 'A'; for (i = 5; i >= 1; i--) { for (j = 0; j < i; j++) printf("%c ", (var + j)); printf("\n"); } return 0; } Options a)A B C D
3 min read
Output of C programs | Set 35 (Loops) Short question based on c loops 1. What will be the output of the following code? C #include <stdio.h> int main() { int i = 0, j = 0; while (i<5,j<10) { i++; j++; } printf("%d %d", i, j); } options : a) 5 5 b) syntax error c) 5 10 d) 10 10 Answer: d Explanation : Here, both the
3 min read
Output of C programs | Set 57 (for loop) Prerequisite : for loop Q.1 What is the output of this program? C #include <iostream> using namespace std; int main() { for (5; 2; 2) printf("Hello\n"); return 0; } Options a) compilation error b) Hello c) infinite loop d) none of the above ans: c Explanation : Putting a non zero value in cond
3 min read
Output of C Programs | Set 6 Predict the output of below programs Question 1 c int main() { unsigned int i=65000; while ( i++ != 0 ); printf("%d",i); return 0; } Output: 1 Explanation: It should be noticed that there's a semi-colon in the body of while loop. So even though, nothing is done as part of while body, the c
4 min read
Output of C Programs | Set 5 Predict the output of below programs Question 1 c int main() { while(1){ if(printf("%d",printf("%d"))) break; else continue; } return 0; } Output: Can't be predictedExplanation: The condition in while loop is 1 so at first shot it looks infinite loop. Then there are break and con
5 min read
Output of C programs | Set 51 1. What will be the output of the below program? CPP #include <stdio.h> int main() { printf("%d", printf("%d", printf("%d", printf("%s", "Welcome to geeksforgeeks")))); return (0); } Options: 1. Welcome to geeksforgeeks2531 2. Welcome to geeksf
2 min read