Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn GCC | Compiler Directives and Advanced Control
C Preprocessing
course content

Course Content

C Preprocessing

C Preprocessing

1. Introduction to Preprocessing
2. Macros
3. Conditional compilation
4. Compiler Directives and Advanced Control

book
GCC

python
  • #pragma: Directive for the compiler

  • GCC diagnostic: Indicates a diagnostic command specific to GCC

  • action: What to do: ignored, warning, or error

  • 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.

c

main

copy
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.

c

main

copy
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; }
question mark

What does the following directive do?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 4. ChapterΒ 3

Ask AI

expand
ChatGPT

Ask anything or try one of the suggested questions to begin our chat

course content

Course Content

C Preprocessing

C Preprocessing

1. Introduction to Preprocessing
2. Macros
3. Conditional compilation
4. Compiler Directives and Advanced Control

book
GCC

python
  • #pragma: Directive for the compiler

  • GCC diagnostic: Indicates a diagnostic command specific to GCC

  • action: What to do: ignored, warning, or error

  • 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.

c

main

copy
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.

c

main

copy
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; }
question mark

What does the following directive do?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 4. ChapterΒ 3
We're sorry to hear that something went wrong. What happened?
some-alt