Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Precise Control for Debugging and Code Generation | Compiler Directives and Advanced Control
C Preprocessing

Swipe to show menu

book
Precise Control for Debugging and Code Generation

python

number β€” the new line number the compiler should treat as the current line.

"file_name" β€” an optional logical file name that will appear in compiler messages and the FILE macro.

python

This means: "The next physical line of code will be treated by the compiler as line number 4."

c

main

copy
1234567891011
#include <stdio.h> #line 100 "template.dsl" void create_label() { int a = "oops"; // error } int main() { create_label(); return 0; }

Why this is useful

The #line directive changes how the compiler sees the location of the code β€” that is, which file and line it appears to be in. This does not affect how the program executes.

This is useful for code generation tools, improving clarity of error messages or mapping between generated code and templates or DSLs.

If you suspect that a certain function might behave incorrectly, you can place the following before it:

python

And if an error does occur, the compiler will report:

python

In other words, you have "tricked" the compiler into thinking that this part of the code comes from a different source.

How long does it last?

The effect of #line applies to all subsequent code, up until the end of the file or the next #line directive.

If you want to "fool" the compiler for just a few lines β€” you can insert another #line afterward to switch back to the original location.

c

main

copy
123456789101112
#include <stdio.h> #line 4 "fileWithFuncs.c" void func() { printf("%s:%d\n", __FILE__, __LINE__); // fileWithFuncs.c:5 } #line 9 "main.c" int main() { func(); return 0; }

So, to limit the effect of #line, just set a new location β€” either back to the real one or to something else.

Task

Swipe to start coding

Imagine you're building a code generator for a GUI, which converts simple text-based templates into C functions.
When something goes wrong in a generated function, you want the compiler to report the error as if it happened in the original template file, not in the generated .c file.
To achieve this, you decide to use the #line directive.

  • Fill in the first ___ so that the compiler believes the function create_button() starts at line 15 in the file buttons.ui.
  • Fill in the second ___ to return the context to main.c, starting at line 20.
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 4. ChapterΒ 6
We're sorry to hear that something went wrong. What happened?

Ask AI

expand
ChatGPT

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

book
Precise Control for Debugging and Code Generation

python

number β€” the new line number the compiler should treat as the current line.

"file_name" β€” an optional logical file name that will appear in compiler messages and the FILE macro.

python

This means: "The next physical line of code will be treated by the compiler as line number 4."

c

main

copy
1234567891011
#include <stdio.h> #line 100 "template.dsl" void create_label() { int a = "oops"; // error } int main() { create_label(); return 0; }

Why this is useful

The #line directive changes how the compiler sees the location of the code β€” that is, which file and line it appears to be in. This does not affect how the program executes.

This is useful for code generation tools, improving clarity of error messages or mapping between generated code and templates or DSLs.

If you suspect that a certain function might behave incorrectly, you can place the following before it:

python

And if an error does occur, the compiler will report:

python

In other words, you have "tricked" the compiler into thinking that this part of the code comes from a different source.

How long does it last?

The effect of #line applies to all subsequent code, up until the end of the file or the next #line directive.

If you want to "fool" the compiler for just a few lines β€” you can insert another #line afterward to switch back to the original location.

c

main

copy
123456789101112
#include <stdio.h> #line 4 "fileWithFuncs.c" void func() { printf("%s:%d\n", __FILE__, __LINE__); // fileWithFuncs.c:5 } #line 9 "main.c" int main() { func(); return 0; }

So, to limit the effect of #line, just set a new location β€” either back to the real one or to something else.

Task

Swipe to start coding

Imagine you're building a code generator for a GUI, which converts simple text-based templates into C functions.
When something goes wrong in a generated function, you want the compiler to report the error as if it happened in the original template file, not in the generated .c file.
To achieve this, you decide to use the #line directive.

  • Fill in the first ___ so that the compiler believes the function create_button() starts at line 15 in the file buttons.ui.
  • Fill in the second ___ to return the context to main.c, starting at line 20.
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 4. ChapterΒ 6
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt