Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Output of C programs | Set 57 (for loop)

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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;
}


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?




#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?




#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?




#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?




#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?




#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.

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.


My Personal Notes arrow_drop_up
Last Updated : 02 Oct, 2017
Like Article
Save Article
Similar Reads