Skip to content
Related Articles

Related Articles

Improve Article

Output of C programs | Set 50 (Control Structures)

  • Difficulty Level : Easy
  • Last Updated : 12 Jul, 2019
Geek Week

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. Hello
2. GFG
3. No Output
4. 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 termination
2. Compile time error
3. No output
4. 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 times
2. 15 times
3. 10 times
4. 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 times
2. print hii 5 times
3. print hii 10 times
4. 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 times
2. Infinitely
3. print 10 times
4. 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.




My Personal Notes arrow_drop_up
Recommended Articles
Page :