Predefined Macros in C
The compiler already knows a lot of useful facts while it compiles your program: the file name, the current line number, the date and time of compilation and which C standard it follows. It gives you these facts through predefined macros, names like __FILE__ and __LINE__ that you can use without defining them.
In this lesson you will learn every important predefined macro, the __func__ identifier, and how to build a logging macro that tells you exactly where a message came from.
What You Will Learn
- What predefined macros are.
- The meaning and format of __FILE__, __LINE__, __DATE__ and __TIME__.
- How __STDC__ and __STDC_VERSION__ tell you the C standard.
- Why __func__ is an identifier, not a macro.
- How to write a debug log macro with file, line and function.
- Rules: you can't redefine predefined macros.
Predefined Macros in C Explained
Exam definition: Predefined macros are macros that are automatically defined by the compiler; the programmer can use them without #define but must not redefine or #undef them. The standard ones are __FILE__ (current file name), __LINE__ (current line number), __DATE__ (compilation date), __TIME__ (compilation time), __STDC__ (1 if the compiler conforms to ISO C) and __STDC_VERSION__ (the C standard version).
Memory trick: Predefined macros are the stamp on a parcel. The post office (compiler) automatically stamps where it came from (__FILE__, __LINE__), when it was packed (__DATE__, __TIME__) and which rules were used (__STDC_VERSION__). You can read the stamp, but you can't change it.
Syntax: List of Predefined Macros
__FILE__ /* "program.c" a string */
__LINE__ /* 42 an int */
__DATE__ /* "Nov 25 2026" string, "Mmm dd yyyy" */
__TIME__ /* "16:05:42" string, "hh:mm:ss" */
__STDC__ /* 1 conforms to ISO C */
__STDC_VERSION__ /* 201710L a long */
__func__ /* "main" name of the current function (C99) */
| Name | Type | Value | Print with |
|---|---|---|---|
__FILE__ | String | Name of the source file being compiled | %s |
__LINE__ | Integer | Line number where it is written | %d |
__DATE__ | String | Date of compilation, e.g. "Nov 25 2026" | %s |
__TIME__ | String | Time of compilation, e.g. "16:05:42" | %s |
__STDC__ | Integer | 1 when the compiler follows ISO C | %d |
__STDC_VERSION__ | Long | 199901L (C99), 201112L (C11), 201710L (C17), 202311L (C23) | %ld |
__func__ | String (identifier) | Name of the enclosing function | %s |
Example 1: Printing Predefined Macros
Save this program as predefined.c. The line numbers matter, so type it exactly as shown.
#include <stdio.h>
int main(void)
{
printf("File : %s\n", __FILE__);
printf("Line : %d\n", __LINE__);
printf("Function : %s\n", __func__);
printf("Standard C: %d\n", __STDC__);
printf("C version : %ld\n", __STDC_VERSION__);
printf("Line now : %d\n", __LINE__);
return 0;
}
Output
File : predefined.c Line : 6 Function : main Standard C: 1 C version : 201710 Line now : 10
Code Explanation
__FILE__becomes the string"predefined.c": the file name as given to the compiler (it may include a folder path if you compilesrc/predefined.c).__LINE__becomes the number of the line where it appears: 6 on line 6, and 10 on line 10. It changes on every line.__func__gives"main"because the code is insidemain.__STDC__is 1, so the compiler follows standard C.__STDC_VERSION__is 201710 here because the program was compiled as C17 (-std=c17). A newer compiler set to C23 prints 202311.
How Predefined Macros Work
Like other macros, they are replaced before compilation, so their values are fixed into the program. __DATE__ and __TIME__ show when the program was compiled, not when it runs:
#include <stdio.h>
int main(void)
{
printf("Compiled on %s at %s\n", __DATE__, __TIME__);
return 0;
}
(Sample output: your date and time will be different) Compiled on Nov 25 2026 at 16:05:42
Run the same executable tomorrow and it still prints the old date. Recompile, and the new date appears. Programs often print this as a "build date" in their About screen.
__func__ works differently: it is not a macro but a predefined identifier that C99 creates inside every function, as if you had written static const char __func__[] = "function_name";. That's why the preprocessor can't use it in #if, but your code can print it.
Example 2: A Logging Macro with File, Line and Function
Save this as logdemo.c.
#include <stdio.h>
#define LOG(msg) printf("[%s:%d %s()] %s\n", __FILE__, __LINE__, __func__, msg)
int divide(int a, int b)
{
if (b == 0)
{
LOG("division by zero, returning 0");
return 0;
}
return a / b;
}
int main(void)
{
LOG("program started");
printf("10 / 2 = %d\n", divide(10, 2));
printf("7 / 0 = %d\n", divide(7, 0));
LOG("program finished");
return 0;
}
Output
[logdemo.c:17 main()] program started 10 / 2 = 5 [logdemo.c:9 divide()] division by zero, returning 0 7 / 0 = 0 [logdemo.c:20 main()] program finished
__LINE__inside a macro gives the line where the macro is used (9, 17 and 20), not where it is defined. That's what makes it so useful for logs.__func__shows which function wrote the message:divideormain.- The divide-by-zero message appears before "7 / 0 = 0", because
divide(7, 0)runs while printf is collecting its arguments. - In big projects, this kind of macro tells you instantly where a problem happened. The standard
assert()macro from<assert.h>uses the same trick.
Important Points to Remember
- Predefined macros are created by the compiler; never
#defineor#undefthem. __FILE__,__DATE__,__TIME__are strings;__LINE__,__STDC__are integers.__DATE__/__TIME__= time of compilation, not of running.__LINE__in a macro reports the line where the macro is called.__func__is a predefined identifier (C99), not a macro.- Each predefined macro name starts and ends with two underscores.
Common Mistakes Beginners Make
| Mistake | What happens | Fix |
|---|---|---|
printf("%d", __FILE__) | Wrong format: __FILE__ is a string | Use %s |
_LINE_ with one underscore | Unknown identifier error | Two underscores on each side: __LINE__ |
| Expecting __TIME__ to show the current time | It shows the compile time | Use time() from <time.h> for the current time |
#define __LINE__ 0 | Undefined behavior / compiler warning | Never redefine predefined macros |
#if __func__ | The preprocessor doesn't know __func__ | Use __func__ only in normal code |
Predefined Macros Practice Questions
- What are predefined macros? Name any four.
- What is the format of the
__DATE__string? - What does
__LINE__give when used inside a macro? - Is
__func__a macro? Explain. - What value of
__STDC_VERSION__means C11?
Show answers
- Macros defined automatically by the compiler: __FILE__, __LINE__, __DATE__, __TIME__ (also __STDC__, __STDC_VERSION__).
"Mmm dd yyyy", for example"Nov 25 2026".- The line number where the macro is used (called), not where it is defined.
- No. It is a predefined identifier (C99) that holds the current function's name; the preprocessor doesn't replace it.
201112L.
Coding Exercise
Write a macro CHECK(cond) that prints "passed: condition" if the condition is true, or "FAILED: condition (line N)" if it is false, using # and __LINE__. Test it with age >= 18 and marks >= 40 for age = 20 and marks = 35. Save the file as check.c.
Show solution
#include <stdio.h>
#define CHECK(cond) \
do { \
if (cond) \
printf("passed: %s\n", #cond); \
else \
printf("FAILED: %s (line %d)\n", #cond, __LINE__); \
} while (0)
int main(void)
{
int age = 20, marks = 35;
CHECK(age >= 18);
CHECK(marks >= 40);
return 0;
}
passed: age >= 18 FAILED: marks >= 40 (line 16)
Line 16 is where CHECK(marks >= 40) is written.
Quick Quiz: Predefined Macros (MCQ)
-
Which predefined macro gives the current line number?
A)
__FILE__B)__LINE__C)__NUM__D)__ROW__ -
The type of
__FILE__is:A) int B) char C) String literal D) float
-
__TIME__shows the time when the program was:A) Run B) Compiled C) Saved D) Installed
-
Which is a predefined identifier, not a macro?
A)
__DATE__B)__STDC__C)__func__D)__LINE__ -
__STDC__has the value 1 when:A) The program has no errors B) The compiler conforms to ISO C C) It is the first line D) Debug mode is on
Show answers
- B:
__LINE__. - C: a string literal.
- B: compiled.
- C:
__func__. - B: the compiler conforms to ISO C.
Quick Revision for Exams and Interviews
Read this before your exam. The highlighted words are the answer.
Must-remember facts
| Predefined macros | Defined by the compiler, can't be redefined |
|---|---|
| __FILE__ | File name (string) |
| __LINE__ | Line number (int) |
| __DATE__ / __TIME__ | Compilation date / time |
| __STDC__ | 1 if ISO C conforming |
| __STDC_VERSION__ | 201710L = C17, 202311L = C23 |
| __func__ | Function name, a predefined identifier |
Frequently asked questions
- What are predefined macros in C?
- Macros automatically defined by the compiler, such as __FILE__, __LINE__, __DATE__ and __TIME__.
- Why are __FILE__ and __LINE__ useful?
- They show where a message or error came from, which makes debugging and logging much easier.
- Does __DATE__ change every time the program runs?
- No. It is fixed at compile time; it changes only when you recompile.
Remember
- Predefined macros = the parcel stamp: where, when, which rules.
- Two underscores on both sides.
Next Topic
Next lesson: Command Line Arguments in C. Module 15 begins: you will pass values to a program from the terminal using argc and argv.