Output of C programs | Set 50 (Control Structures) - GeeksforGeeks

12 Jul, 2019
Here are some C programs which is related to the control structures With the detailed explanation, guess the output of the following programs:-
1.what will be the output of the below program?
#include <stdio.h>int main(){    int x = 2;    switch (x) {        x--;        switch (x) {        case 1:            printf("Hello");            break;        case 2:            printf("GFG");            break;        case 3:            printf("Welcome");            break;        default:            printf("BYE");        }    }    return (0);}
Options:1. Hello2. GFG3. No Output4. BYE
The answer is option(3).
Explanation:The switch case control structure execute the statement present in the case. But in the above program, the outer switch does not has cases. Therefore, control does not enter inside the switch case. Thus, the program does not give any output and compiler does not produce any error.
2.Guess the correct answer of the mentioned program?
#include <stdio.h>int main(){    int x = 2;    switch (x) {    case 1:        printf("Hello");        break;    case 2:        printf("Hello");        break;    case 3:        printf("GFG");        break;    default:        printf("BYE");    }    return (0);}
Options:1. Abnormal termination2. Compile time error3. No output4. Hello
The output is option(4).
Explanation: In the above program, value of x is 2 so case 2 will execute and it will print Hello.Resource: https://www.geeksforgeeks.org/switch-statement-cc/
3.How many times does the loop run?
#include <stdio.h>int main(){    int i;    for (i = 1; i <= 100; i++) {        printf("GFG\n");        if (i == 5)            break;    }    return (0);}
Options:1. 5 times2. 15 times3. 10 times4. 20 times
The answer is option(1).
Explanation: Here the loop print GFG five times. Because when the for loop counter value i.e. i is 5, the break statement will transfer the control to outside loop.Resource:www.geeksforgeeks.org/break-statement-cc/
4.what will be the output?
#include <stdio.h>int main(){    int i;    for (i = 1; i <= 10; i++) {        printf("welcome\n");        continue;        printf("hii");    }    return (0);}
Options:1. print welcome 5 times2. print hii 5 times3. print hii 10 times4. print welcome 10 times
The answer is option(4).
Explanation : When the control of the for loop reaches continue statement, the control automatically transfers to the beginning of the loop and leave the printf(“hii”); statement.Resource: https://www.geeksforgeeks.org/continue-statement-cpp/
5.How many time the program will print?
#include <stdio.h>>int main(){    int i;    for (i = 2; i = 100; i += 2) {        printf("Hi\\n");    }    return (0);}
Options:1. print 2 times2. Infinitely3. print 10 times4. print 5 times
The answer is option(2).
Explanation: Because the value 100 is assigned to i every time and it does not check for any condition because here there is no = sign in the for loop. Therefore the loop goes into infinite loop.
This article is contributed by Bishal Kumar Dubey. 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.
shi143din
ManasChhabra2
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