0
0
Cprogramming~10 mins

Predefined macros - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Predefined macros
Start Compilation
Compiler Inserts Predefined Macros
Code Uses Macros like __FILE__, __LINE__
Macros Expand to Values
Compilation Continues with Expanded Code
Program Runs with Macro Values Embedded
During compilation, the compiler automatically inserts predefined macros with specific values, which the code can use and expand before running.
Execution Sample
C
#include <stdio.h>

int main() {
    printf("File: %s\n", __FILE__);
    printf("Line: %d\n", __LINE__);
    return 0;
}
This code prints the current file name and the line number where the macros are used.
Execution Table
StepCode LineMacro UsedMacro ExpansionOutput Produced
1printf("File: %s\n", __FILE__);__FILE__"example.c"File: example.c
2printf("Line: %d\n", __LINE__);__LINE__6Line: 6
3return 0;NoneNoneProgram ends
💡 All macros expanded by compiler, program outputs file name and line number, then ends.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
__FILE__Not set"example.c""example.c""example.c"
__LINE__Not setNot set66
Key Moments - 2 Insights
Why does __LINE__ print 5 instead of the line where the program runs?
Because __LINE__ expands to the line number in the source code where it appears, not the runtime line. See execution_table row 2.
Is __FILE__ a string or a number?
__FILE__ expands to a string literal with the current file name, as shown in execution_table row 1.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what value does __FILE__ expand to at step 1?
A7
B"main.c"
C"example.c"
DUndefined
💡 Hint
Check the 'Macro Expansion' column in execution_table row 1.
At which step does __LINE__ get expanded?
AStep 1
BStep 2
CStep 3
DNever
💡 Hint
Look at the 'Macro Used' column in execution_table to find when __LINE__ is used.
If you add a new line before printf(__LINE__), what happens to the output?
AThe printed line number increases by 1
BThe printed line number stays the same
CThe program crashes
DThe file name changes
💡 Hint
Refer to variable_tracker for __LINE__ changes when code lines change.
Concept Snapshot
Predefined macros are special names the compiler replaces with info during compilation.
Common macros:
- __FILE__: current file name as string
- __LINE__: current line number as int
They help track code location without manual input.
Used inside code, expanded before running.
Full Transcript
Predefined macros in C are special names that the compiler automatically replaces with useful information during compilation. For example, __FILE__ becomes the current file name as a string, and __LINE__ becomes the line number in the source code where it appears. In the example code, when printf uses __FILE__, it prints the file name, and when it uses __LINE__, it prints the line number. These macros are expanded before the program runs, so the output shows the actual file name and line number. If you add or remove lines before the macro, the line number changes accordingly. This helps programmers know exactly where in the code something happens without typing it manually.