Dies kompiliert ohne Warnungen.Rückgabe void type in C und C++
Ist das in C und C++ erlaubt oder funktioniert es nur in gcc und clang?
Wenn es legal ist, ist es etwas Neues nach C99?
void f(){
}
void f2(){
return f();
}
aktualisieren
als "Rad Lexus", schlug ich das versucht:
$ 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)
aktualisieren
Jemand gefragt, wie dieser Aufbau zu helfen ist. Gut ist mehr oder weniger syntaktischer Zucker. Hier ist ein gutes Beispiel:
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");
}
Gewählt, um wieder zu öffnen; Das vorgeschlagene Duplikat war nur für C++. Dies ist auch markiert mit C. (C und C++ unterscheiden sich beträchtlich in ihrer Verwendung von "void"). – Bathsheba
Sie fragen also nach C oder C++? Wählen Sie eine Sprache. – 2501
Hinweis: mit beiden 'gcc -Wall -Wpedantic -std = c99' und' -std = c11', erhalten Sie eine Warnung: "Warnung: ISO C verbietet 'Rückkehr' mit Ausdruck, in Funktion zurückgeben void [-Wpedantic]" . – usr2564301