Output of C programs | Set 50 (Control Structures)

2

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 bellow 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(2).

Explanation: In the above program, it gives a compilation error because we mentioned two cases with the same case statement i.e. “Hello”. which is a duplicate in case statement and avoided by C language.
Resource: http://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: http://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.

GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



2 Average Difficulty : 2/5.0
Based on 1 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.