Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Predefined Macros in C with Examples

  • Difficulty Level : Easy
  • Last Updated : 05 Aug, 2021

According to C89 Standard, C programming language has following predefined macros:

Take a step-up from those "Hello World" programs. Learn to implement data structures like Heap, Stacks, Linked List and many more! Check out our Data Structures in C course to start learning today.
  1. __LINE__ Macro: __LINE__ macro contains the current line number of the program in the compilation. It gives the line number where it is called. It is used in generating log statements, error messages, throwing exceptions and debugging codes. Whenever the compiler finds an error in compilation it first generates the line number at which error occurred using __LINE__ and prints error message along with line number so that user can easily fix that error easily.

    C




    #include <stdio.h>
    int main()
    {
        printf("Line number is: %d\n",
               __LINE__);
        return 0;
    }
    Output:
    Line number is: 5
    
  2. __FILE__ Macro: __FILE__ macro holds the file name of the currently executing program in the computer. It is also used in debugging, generating error reports and log messages.

    C




    #include <stdio.h>
    int main()
    {
        printf("File name of this"
               " program is: %s\n",
               __FILE__);
        return 0;
    }
    Output:

    File name of this program is: /usr/share/IDE_PROGRAMS/C/other/703ad0b087fbd7d18cde5ea81f148f36/703ad0b087fbd7d18cde5ea81f148f36.c



  3. __DATE__ Macro: __DATE__ macro gives the date at which source code of this program is converted into object code. Simply put, it returns the date at which the program was compiled. Date is in the format mmm dd yyyy. mmm is the abbrevated month name.

    C




    #include <stdio.h>
    int main()
    {
        printf("Program Compilation Date: %s\n",
               __DATE__);
        return 0;
    }
    Output:
    Program Compilation Date: Dec 26 2019
    
  4. __TIME__ Macro: __DATE__ macro gives the time at which program was compiled. Time is in the format hour:minute:second.

    C




    #include <stdio.h>
    int main()
    {
        printf("Time of compilation is: %s\n",
               __TIME__);
        return 0;
    }
    Output:
    Time of compilation is: 13:17:20
    
  5. __STDC__ Macro: __STDC__ Macro is used to confirm the compiler standard. Generally it holds the value 1 which means that the compiler conforms to ISO Standard C.

    C




    #include <stdio.h>
    int main()
    {
        printf("Compiler Standard Number: %d\n",
               __STDC__);
        return 0;
    }
    Output:
    Compiler Standard Number: 1
    
  6. __STDC__HOSTED Macro: This macro holds the value 1 if the compiler’s target is a hosted environment. A hosted environment is a facility in which a third-party holds the compilation data and runs the programs on its own computers. Generally, the value is set to 1.

    C




    #include <stdio.h>
    int main()
    {
        printf("STDC_HOSTDED Number: %d\n",
               __STDC_HOSTED__);
        return 0;
    }
    Output:
    STDC_HOSTDED Number: 1
    
  7. __STDC_VERSION__: This macro holds the C Standard’s version number in the form yyyymmL where yyyy and mm are the year and month of the Standard version. This signifies which version of the C Standard the compiler conforms to.
    Values hold by __STDC_VERSION__
    • The value 199409L signifies the C89 standard amended in 1994. This is the current default standard.
    • The value 199901L signifies the C99 standard
    • The value 201112L signifies the 2011 revision of the C standard

    These standard values are changed when the user is required to use the function or header file in C89 standard which is removed in C99 standard. The compiler changes its standard of execution and runs the output.

    Check out this article which changes the standard to run asctime_s() function declared in the C89 standard.

    C




    #include <stdio.h>
    int main()
    {
        printf("Compiler Standard "
               "VERSION Number: %ld\n",
               __STDC_VERSION__);
        return 0;
    }
    Output:
    Compiler Standard VERSION Number: 201112
    

    Predefined Macros in C99 standard:

  8. __cplusplus: __cplusplus Macro is defined when the C++ compiler is used. It is used to test whether a header is compiled by a C compiler or a C++ compiler. This macro gives value similar to __STDC_VERSION__, in that it expands to a version number.
    Values hold by __cplusplus
    • 199711L for the 1998 C++ standard
    • 201103L for the 2011 C++ standard
    • 201402L for the 2014 C++ standard
    • 201703L for the 2017 C++ standard
  9. __OBJC__ Macro:: __OBJC__ Macro has value 1 if the Objective-C compiler is in use. __OBJC__ is used to test whether a header is compiled by a C compiler or an Objective-C compiler.

References: https://gcc.gnu.org/onlinedocs/cpp/Standard-Predefined-Macros.html




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!