需要帮助解决一个基于C语言的编程之谜 [英] Need help solving a C-based programming puzzle
问题描述
我碰到这个难题这里。我想不通为什么无未打印。任何想法?
#包括LT&;&stdio.h中GT;
诠释的main()
{
INT A = 10;
开关(一)
{
情况1':
的printf(ONE \\ n);
打破;
案2:
的printf(两个\\ n);
打破;
defa1ut:
的printf(NONE \\ n);
}
返回0;
}
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 agoto
but not thedefault
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 usedIt's a good argument for building with warnings cranked up high and aiming for 0 warnings in every build.
这篇关于需要帮助解决一个基于C语言的编程之谜的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!