Output of C programs | Set 57 (for loop)
Last Updated :
02 Oct, 2017
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 condition part makes it infinite.
Q.2 What is the output of this program?
CPP
#include <iostream>
using namespace std;
int main()
{
static int i;
for (i++; ++i; i++) {
printf("%d ", i);
if (i == 6)
break;
}
return 0;
}
Options
Options:
a) 2 4 6
b) compilation error
c) garbage value
d) no output
ans : a
Explanation : After first iteration, program looks like (0, 2, 2). It breaks when i = 6.
Q.3 What is the output of this program?
CPP
#include <iostream>
using namespace std;
int fun();
int main()
{
for (fun(); fun(); fun()) {
printf("%d ", fun());
}
return 0;
}
int fun()
{
int static num = 10;
return num--;
}
Options
a) compilation error
b) can't be predicted
c) 8 5 2
d) none of the above
ans: c
Explanation :
At first iteration:
for(10; 9; fun()) //condition true
printf("%d", 8) //8 prints
At second iteration:
for(10; fun(); 7)
for(7; 6 ;fun()) //condition true
printf("%d", 5) //5 prints
At third iteration:
for(7; fun(); 4)
for(4; 3; fun()) //condition true
printf("%d", 2) //2 prints
At fourth iteration:
for(4; fun(); 1)
for(1; 0; fun()) //condition false
Program terminates
Q.4 What is the output of this program?
CPP
#include <iostream>
using namespace std;
int main()
{
for (;;)
printf("%d ", 10);
return 0;
}
Options
a) compilation error
b) run time error
c) 10
d) Infinite loop
ans : d
Explanation : Since no condition is provided so loop runs infinitely.
Q.5 What is the output of this program?
CPP
#include <iostream>
using namespace std;
int main()
{
char i = 0;
for (; i++; printf("%d", i))
;
printf("%d", i);
return 0;
}
Options
a) 0
b) 1
c) Infinite loop
d) compilation error
ans: b
Explanation : The following condition fails for first time so loop terminates and value of i is incremented to 1.
for(; 0; printf("%d", i))
Q.6 What is the output of this program?
CPP
#include <iostream>
using namespace std;
int main()
{
int i;
for (i = 0; i < 0, 5; i++)
printf("%d ", i);
return 0;
}
Optionsa) error
b) 1, 3
c) program never ends
d) none of these
ans: c
Explanation :-
Considers two conditions:
(a)i<0 fails for first iteration
(b)5 in condition part makes it infinite loop as it never becomes 0.
Similar Reads
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 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 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 7 Predict the output of below programsQuestion 1 c int main() { int i = 0; while (i <= 4) { printf("%d", i); if (i > 3) goto inside_foo; i++; } getchar(); return 0; } void foo() { inside_foo: printf("PP"); } Output: Compiler error: Label "inside_foo" used but not defined.Expl
2 min read
Output of C programs | Set 56 (While loop) 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
2 min read
Output of C programs | Set 55 1. What will be the output of the following program? C #include <stdio.h> int main() { int a = 03489; printf("%d", a); return (0); } Options: 1. 1865 2. runtime error 3. Syntax error 4. 0 The answer is option(3). Explanation: Any integral value prefix with 0 acts as octal number but
3 min read