为什么在不返回值的情况下从非空函数的末尾流出不产生编译器错误?自从很多年前我意识到这不会在默认情况下产生错误(至少在GCC中),我一直在想为什么?我知道您可以发出编译器标志来产生警告,但是它不应该总是一个错误吗?为什么不返回值的非空函数是有效的?评论中要求的一个例子:#include <stdio.h>int stringSize(){}int main(){
char cstring[5];
printf( "the last char is: %c\n", cstring[stringSize()-1] );
return 0;}.编译.
3 回答
慕斯709654
TA贡献1840条经验 获得超5个赞
Color getColor(Suit suit) {
switch (suit) {
case HEARTS: case DIAMONDS: return RED;
case SPADES: case CLUBS: return BLACK;
}
// Error, no return?}char getChoice() {
int ch = read();
if (ch == -1 || ch == 'q') {
System.exit(0);
}
else {
return (char) ch;
}
// Cannot reach here, but still an error.}- 3 回答
- 0 关注
- 726 浏览
添加回答
举报
0/150
提交
取消
