A structured lesson workspace with readable content, hands-on examples, and a clean path to completion.
The #line directive overrides the values that __LINE__ and __FILE__ report. #line 100 "generated.c" tells the preprocessor that the next line is line 100 of generated.c. This is used by code generators: yacc, lex, protobuf compilers, and template engines emit C code with #line directives so that error messages and debug information reference the original source file, not the generated intermediate. You will rarely write #line by hand. You will frequently encounter it in generated code.
#include <stdio.h>int main(void) {printf("Before: %s:%d\n", __FILE__, __LINE__);#line 100 "custom_source.y"printf("After: %s:%d\n", __FILE__, __LINE__);printf("Still: %s:%d\n", __FILE__, __LINE__);#line 1printf("Reset: %s:%d\n", __FILE__, __LINE__);return 0;}
After the #line directive, __FILE__ reports custom_source.y and __LINE__ starts at 100. The next line is 101. The compiler and debugger use these overridden values for error messages and breakpoints. If this code had a compile error on the line after #line 100, the error would reference custom_source.y:100, not the actual generated file. This is how yacc reports errors in your grammar file rather than in y.tab.c.
#line is infrastructure. You will not write it often. But when you see it in generated code, you will understand why it is there and what it does. Next: include guards in depth, the pattern that every C header file must implement.
Finish the lesson once you have worked through the material. This awards ★ 20 XP.