Explain output of this C program

后端 未结 4 2035
我寻月下人不归
我寻月下人不归 2021-01-27 09:08

Found this code at C Puzzles.

#include

int main()
{
  int a=1;
  switch(a)
  {   int b=20;
      case 1: printf(\"b is %d\\n\",b);
                       


        
4条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-27 09:31

    #include
    int main()
    {
        int a = 1;
        switch(a) {   
            int b=20;
            printf("This gets called!\n");
            case 1: 
                    printf("b is %d\n",b);
                    break;
            default:
                    printf("b is %d\n",b);
                    break;
        }
        return 0;
    }
    

    The key here is that int b=20; never gets called. The compiler will create the variable b but it never gets initialised.

提交回复
热议问题