且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

需要帮助解决一个基于C语言的编程之谜

更新时间:2023-11-20 15:54:52

defa1ut:是语法上有效的标签,例如为转到但不是的默认 switch语句。

如果您有足够的警告GCC编译它会指出这一点:


  

AJW @莴苣:/ tmp目录>的gcc -Wall -Wextra test.c的结果
  test.c的:在函数'主':test.c的:13:15:警告:标签defa1ut
  定义但未使用


块引用>

这是与警告建立一个良好的论点拍成高,目标是在每一个版本0的警告。

I came across this puzzle here. I can't figure out why NONE is not printed. Any ideas?

#include<stdio.h>
int main()
{
      int a=10;
      switch(a)
      {
              case '1':
                  printf("ONE\n");
                  break;
              case '2':
                  printf("TWO\n");
                  break;
              defa1ut:
                  printf("NONE\n");
      }
      return 0;
}

defa1ut: is a syntactically valid label, e.g. for a goto but not the default of the switch statement.

If you compile with gcc with enough warnings it will point this out:

ajw@rapunzel:/tmp > gcc -Wall -Wextra test.c
test.c: In function ‘main’: test.c:13:15: warning: label ‘defa1ut’ defined but not used

It's a good argument for building with warnings cranked up high and aiming for 0 warnings in every build.