Output of C programs | Set 57 (for loop) - GeeksforGeeks

02 Oct, 2017
Prerequisite : for loop
Q.1 What is the output of this program?
#include <iostream>using namespace std;int main(){    for (5; 2; 2)        printf("Hello\n");    return 0;}
Optionsa) compilation errorb) Helloc) infinite loopd) 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?
#include <iostream>using namespace std;int main(){    static int i;    for (i++; ++i; i++) {        printf("%d ", i);        if (i == 6)            break;    }    return 0;}
OptionsOptions:a) 2 4 6b) compilation errorc) garbage valued) 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?
#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--;}
Optionsa) compilation errorb) can’t be predictedc) 8 5 2d) 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?
#include <iostream>using namespace std;int main(){    for (;;)        printf("%d ", 10);    return 0;}
Optionsa) compilation errorb) run time errorc) 10d) Infinite loop
ans : d
Explanation : Since no condition is provided so loop runs infinitely.
Q.5 What is the output of this program?
#include <iostream>using namespace std;int main(){    char i = 0;    for (; i++; printf("%d", i))        ;    printf("%d", i);    return 0;}
Options
a) 0b) 1c) Infinite loopd) 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?
#include <iostream>using namespace std;int main(){    int i;    for (i = 0; i < 0, 5; i++)        printf("%d ", i);    return 0;}
Optionsa) errorb) 1, 3c) program never endsd) 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.
This article is contributed by Pragya Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
C-Loops & Control Statements
C-Output
Program Output
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Output of Java program | Set 18 (Overriding)
Output of Java Program | Set 11
Output of Java programs | Set 13 (Collections)
Output of C++ programs | Set 34 (File Handling)
Different ways to copy a string in C/C++
Output of Java Program | Set 3
Runtime Errors
Output of Java program | Set 28
Output of Java program | Set 5
Output of Java Programs | Set 12