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

Sveip for å vise menyen

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.

Oppgave

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 desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 4. Kapittel 6
Vi beklager at noe gikk galt. Hva skjedde?

Spør AI

expand
ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

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.

Oppgave

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 desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 4. Kapittel 6
Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Vi beklager at noe gikk galt. Hva skjedde?
some-alt