Skip to content
StudyFame

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) */
NameTypeValuePrint with
__FILE__StringName of the source file being compiled%s
__LINE__IntegerLine number where it is written%d
__DATE__StringDate of compilation, e.g. "Nov 25 2026"%s
__TIME__StringTime of compilation, e.g. "16:05:42"%s
__STDC__Integer1 when the compiler follows ISO C%d
__STDC_VERSION__Long199901L (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

  1. __FILE__ becomes the string "predefined.c": the file name as given to the compiler (it may include a folder path if you compile src/predefined.c).
  2. __LINE__ becomes the number of the line where it appears: 6 on line 6, and 10 on line 10. It changes on every line.
  3. __func__ gives "main" because the code is inside main.
  4. __STDC__ is 1, so the compiler follows standard C.
  5. __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: divide or main.
  • 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 #define or #undef them.
  • __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

MistakeWhat happensFix
printf("%d", __FILE__)Wrong format: __FILE__ is a stringUse %s
_LINE_ with one underscoreUnknown identifier errorTwo underscores on each side: __LINE__
Expecting __TIME__ to show the current timeIt shows the compile timeUse time() from <time.h> for the current time
#define __LINE__ 0Undefined behavior / compiler warningNever redefine predefined macros
#if __func__The preprocessor doesn't know __func__Use __func__ only in normal code

Predefined Macros Practice Questions

  1. What are predefined macros? Name any four.
  2. What is the format of the __DATE__ string?
  3. What does __LINE__ give when used inside a macro?
  4. Is __func__ a macro? Explain.
  5. What value of __STDC_VERSION__ means C11?
Show answers
  1. Macros defined automatically by the compiler: __FILE__, __LINE__, __DATE__, __TIME__ (also __STDC__, __STDC_VERSION__).
  2. "Mmm dd yyyy", for example "Nov 25 2026".
  3. The line number where the macro is used (called), not where it is defined.
  4. No. It is a predefined identifier (C99) that holds the current function's name; the preprocessor doesn't replace it.
  5. 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)

  1. Which predefined macro gives the current line number?

    A) __FILE__   B) __LINE__   C) __NUM__   D) __ROW__

  2. The type of __FILE__ is:

    A) int   B) char   C) String literal   D) float

  3. __TIME__ shows the time when the program was:

    A) Run   B) Compiled   C) Saved   D) Installed

  4. Which is a predefined identifier, not a macro?

    A) __DATE__   B) __STDC__   C) __func__   D) __LINE__

  5. __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
  1. B: __LINE__.
  2. C: a string literal.
  3. B: compiled.
  4. C: __func__.
  5. 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 macrosDefined 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.