Output of C programs | Set 59 (Loops and Control Statements) - GeeksforGeeks

31 Aug, 2021
Prerequisite : Control StatementsQ.1 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 1 2 ... infinite times b) 0 1 2 ... 127 c) 0 d) 1  
ans:- d 
Explanation : Before entering into the for loop the CHECK CONDITION is “evaluated”. Here it is evaluated to 0 (false) and comes out of the loop, and i is incremented (note the semicolon after the for loop).Q.2 What is the output of this program?  
CPP
#include <iostream>using namespace std;fun(){print:    printf("geeksforgeeks.org");}int main(){    int i = 1;    while (i <= 5) {        printf("%d", i);        if (i == 5)            goto print;        i++;    }    return 0;}
Optionsa) Compiler Error b) 12345geeksforgeeks.org c) 1234geeksforgeeks.org d) 1geeksforgeeks.org 2geeksforgeeks.org 3geeksforgeeks.org 4geeksforgeeks.org 5geeksforgeeks.org  
ans:- a
Explanation : Compiler error: Undefined label ‘print’ in function main. Labels have functions scope; in other words, the scope of the labels is limited to functions. The label ‘print’ is available in function fun(). Hence it is not visible in function main.Q.3 What is the output of this program?  
CPP
#include <iostream>using namespace std;int main(){    unsigned char counter = 0;    for (counter = 0; counter <= 255; counter++) {        printf("%d ", counter);    }    return 0;}
Options a) 0 1 2 ... infinite times b) 0 1 2 ... 255 c) compilation error d) run time error  
ans:- a
Explanation : The range of unsigned char is 0 to 255 and when the value of var will cross over 255, value will be 0 and again same process will happen.Q.4 What is the output of this program?  
CPP
#include <iostream>using namespace std;int main(){    int count = 0;    for (;;) {        if (count == 10)            break;        printf("%d ", ++count);    }    return 0;}
Options a) 0 1 2 3 4 5 6 7 8 9 10 b) 0 1 2 3 ... infinite times c) 1 2 3 4 5 6 7 8 9 10 d) 1 2 3 4 5 6 7 8 9 
ans:- c
Explanation : for(;;) it is possible in C, there is no need to place condition with in the for(), you can place condition with in the body of the loop.Q.5 What is the output of this program?  
CPP
#include <iostream>#include <string.h>using namespace std;int main(){    int count;    for (count = 0; count < 10; ++count) {        printf("#");        if (count > 6)            continue;        printf("%d", count);    }    return 0;}
Options a)#0#1#2#3#4#5#6### b)#0#1#2#3#4#5#6#7#8#9#10 c)#0#1#2#3#4#5##7#8#9#10 d)#0#1#2#3#4#5#  
ans:- a
Explanation : prints # and then value of count until its value becomes 6. After count is greater than 6, loop continues and prints # for the left iterations(i.e. for 7 8 and 9). Now the condition becomes false so loop terminates.This article is contributed by Pragya Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@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. 
gulshankumarar231
C-Loops & Control Statements
C-Output
Program Output
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Different ways to copy a string in C/C++
Runtime Errors
Output of Java Program | Set 3
unsigned specifier (%u) in C with Examples
Output of C++ Program | Set 1
Output of Java program | Set 5
Output of C Programs | Set 3
How to show full column content in a PySpark Dataframe ?
Output of Java Program | Set 7
error: call of overloaded ‘function(x)’ is ambiguous | Ambiguity in Function overloading in C++