這個編譯沒有任何警告。在C和C++中返回void類型
這是合法的C和C++還是隻是在gcc和鏗鏘聲中工作?
如果它是合法的,C99之後它是否是一些新的東西?
void f(){
}
void f2(){
return f();
}
更新
爲 「拉德雷克薩斯」 建議我嘗試這樣做:
$ gcc -Wall -Wpedantic -c x.c
x.c: In function ‘f2’:
x.c:7:9: warning: ISO C forbids ‘return’ with expression, in function returning void [-Wpedantic]
return f();
$ clang -Wall -Wpedantic -c x.c
x.c:7:2: warning: void function 'f2' should not return void expression [-Wpedantic]
return f();
^ ~~~~~
1 warning generated.
$ gcc -Wall -Wpedantic -c x.cc
(no errors)
$ clang -Wall -Wpedantic -c x.cc
(no errors)
更新
有人問這樣的結構是如何幫助。那麼語法糖或多或少。這裏是一個很好的例子:
void error_report(const char *s){
printf("Error %s\n", s);
exit(0);
}
void process(){
if (step1() == 0)
return error_report("Step 1");
switch(step2()){
case 0: return error_report("Step 2 - No Memory");
case 1: return error_report("Step 2 - Internal Error");
}
printf("Processing Done!\n");
}
投票重新開放;提出的複製只適用於C++。這也被標記爲C.(C和C++在使用'void'方面差別很大)。 – Bathsheba
所以你要求C或C++?選擇一種語言。 – 2501
注意:同時使用'gcc -Wall-Wpedantic -std = c99'和'-std = c11',你會得到一個警告:「warning:ISO C禁止'返回'帶表達式,返回void [-Wpedantic]的函數中。 – usr2564301