GCC
#pragma GCC diagnostic [action] [warning-name]
#pragma
: Directive for the compilerGCC diagnostic
: Indicates a diagnostic command specific to GCCaction
: What to do:ignored
,warning
, orerror
warning-name
: The name of the warning, e.g.-Wunused-variable
,-Wreturn-type
, etc.
The function broken_function
has a return type of int
, but it does not contain a return
statement.
This means the function doesn't return a value, even though its declared return type requires one — which violates the function's contract.
main.c
1234567891011#include <stdio.h> int broken_function() { // warning: no return statement in function returning non-void [-Wreturn-type] } int main() { int result = broken_function(); printf("Result: %d\n", result); return 0; }
The directive #pragma GCC diagnostic error "-Wreturn-type"
changes the compiler's behavior so that a warning about a missing return
statement in a function with return type int
is treated as a compilation error instead of just a warning.
main.c
1234567891011121314#include <stdio.h> #pragma GCC diagnostic push #pragma GCC diagnostic error "-Wreturn-type" int broken_function() { // error: no return statement in function returning non-void [-Werror=return-type] } #pragma GCC diagnostic pop int main() { int result = broken_function(); printf("Result: %d\n", result); return 0; }
Bedankt voor je feedback!
Vraag AI
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.
Stel mij vragen over dit onderwerp
Vat dit hoofdstuk samen
Toon voorbeelden uit de praktijk
Awesome!
Completion rate improved to 5.56
GCC
Veeg om het menu te tonen
#pragma GCC diagnostic [action] [warning-name]
#pragma
: Directive for the compilerGCC diagnostic
: Indicates a diagnostic command specific to GCCaction
: What to do:ignored
,warning
, orerror
warning-name
: The name of the warning, e.g.-Wunused-variable
,-Wreturn-type
, etc.
The function broken_function
has a return type of int
, but it does not contain a return
statement.
This means the function doesn't return a value, even though its declared return type requires one — which violates the function's contract.
main.c
1234567891011#include <stdio.h> int broken_function() { // warning: no return statement in function returning non-void [-Wreturn-type] } int main() { int result = broken_function(); printf("Result: %d\n", result); return 0; }
The directive #pragma GCC diagnostic error "-Wreturn-type"
changes the compiler's behavior so that a warning about a missing return
statement in a function with return type int
is treated as a compilation error instead of just a warning.
main.c
1234567891011121314#include <stdio.h> #pragma GCC diagnostic push #pragma GCC diagnostic error "-Wreturn-type" int broken_function() { // error: no return statement in function returning non-void [-Werror=return-type] } #pragma GCC diagnostic pop int main() { int result = broken_function(); printf("Result: %d\n", result); return 0; }
Bedankt voor je feedback!