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

Scorri per mostrare il menu

book
Precise Control for Debugging and Code Generation

#line number
#line number "file_name"

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.

#line 4 "fileWithFuncs.c"

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

main.c

main.c

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:

#line 4 "fileWithFuncs.c"
myFunc();

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

fileWithFuncs.c:4: error: something went wrong

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.

main.c

main.c

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.

Compito

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 desktopCambia al desktop per esercitarti nel mondo realeContinua da dove ti trovi utilizzando una delle opzioni seguenti
Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 4. Capitolo 6
single

single

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

close

Awesome!

Completion rate improved to 5.56

book
Precise Control for Debugging and Code Generation

#line number
#line number "file_name"

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.

#line 4 "fileWithFuncs.c"

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

main.c

main.c

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:

#line 4 "fileWithFuncs.c"
myFunc();

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

fileWithFuncs.c:4: error: something went wrong

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.

main.c

main.c

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.

Compito

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 desktopCambia al desktop per esercitarti nel mondo realeContinua da dove ti trovi utilizzando una delle opzioni seguenti
Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

close

Awesome!

Completion rate improved to 5.56

Scorri per mostrare il menu

some-alt