jeudi 19 septembre 2019

Switch cases execute inside if-else statements [duplicate]

This question already has an answer here:

My coworker showed me the following code and asked me about the output. When I came to know the answer, I was shocked.

#include <stdio.h> 

int main() 
{ 
    int a = 2, b = 2; 
    switch(a) 
    { 
    case 1: 
        ; 

        if (b==5) 
        { 
        case 2: 
            printf("GeeksforGeeks"); 
        } 
    else case 3: 
    { 

    } 
    } 
} 

Output:

GeeksforGeeks

He saw this program here. Here, case 2 inside the "if" condition and variable "b" has assigned value 2. So condition becomes false.

We are really curious to know how this is possible?

Aucun commentaire:

Enregistrer un commentaire