Predefined Macros in C with Examples - GeeksforGeeks

21 Dec, 2021
According to C89 Standard, C programming language has following predefined macros:
__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.CC#include <stdio.h>int main(){    printf("Line number is: %d\n",           __LINE__);    return 0;}Output:Line number is: 5
__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.CC#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__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 abbreviated month name.CC#include <stdio.h>int main(){    printf("Program Compilation Date: %s\n",           __DATE__);    return 0;}Output:Program Compilation Date: Dec 26 2019
__TIME__ Macro: __DATE__ macro gives the time at which program was compiled. Time is in the format hour:minute:second.CC#include <stdio.h>int main(){    printf("Time of compilation is: %s\n",           __TIME__);    return 0;}Output:Time of compilation is: 13:17:20
__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.CC#include <stdio.h>int main(){    printf("Compiler Standard Number: %d\n",           __STDC__);    return 0;}Output:Compiler Standard Number: 1
__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.CC#include <stdio.h>int main(){    printf("STDC_HOSTDED Number: %d\n",           __STDC_HOSTED__);    return 0;}Output:STDC_HOSTDED Number: 1
__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 standardThe value 201112L signifies the 2011 revision of the C standardThese 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.CC#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:__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 __cplusplus199711L for the 1998 C++ standard201103L for the 2011 C++ standard201402L for the 2014 C++ standard201703L for the 2017 C++ standard__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.
__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.CC#include <stdio.h>int main(){    printf("Line number is: %d\n",           __LINE__);    return 0;}Output:Line number is: 5

C
#include <stdio.h>int main(){    printf("Line number is: %d\n",           __LINE__);    return 0;}
Line number is: 5

__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.CC#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
C
#include <stdio.h>int main(){    printf("File name of this"           " program is: %s\n",           __FILE__);    return 0;}
File name of this program is: /usr/share/IDE_PROGRAMS/C/other/703ad0b087fbd7d18cde5ea81f148f36/703ad0b087fbd7d18cde5ea81f148f36.c
__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 abbreviated month name.CC#include <stdio.h>int main(){    printf("Program Compilation Date: %s\n",           __DATE__);    return 0;}Output:Program Compilation Date: Dec 26 2019

C
#include <stdio.h>int main(){    printf("Program Compilation Date: %s\n",           __DATE__);    return 0;}
Program Compilation Date: Dec 26 2019

__TIME__ Macro: __DATE__ macro gives the time at which program was compiled. Time is in the format hour:minute:second.CC#include <stdio.h>int main(){    printf("Time of compilation is: %s\n",           __TIME__);    return 0;}Output:Time of compilation is: 13:17:20

C
#include <stdio.h>int main(){    printf("Time of compilation is: %s\n",           __TIME__);    return 0;}
Time of compilation is: 13:17:20

__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.CC#include <stdio.h>int main(){    printf("Compiler Standard Number: %d\n",           __STDC__);    return 0;}Output:Compiler Standard Number: 1

C
#include <stdio.h>int main(){    printf("Compiler Standard Number: %d\n",           __STDC__);    return 0;}
Compiler Standard Number: 1

__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.CC#include <stdio.h>int main(){    printf("STDC_HOSTDED Number: %d\n",           __STDC_HOSTED__);    return 0;}Output:STDC_HOSTDED Number: 1

C
#include <stdio.h>int main(){    printf("STDC_HOSTDED Number: %d\n",           __STDC_HOSTED__);    return 0;}
STDC_HOSTDED Number: 1

__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 standardThe value 201112L signifies the 2011 revision of the C standardThese 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.CC#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:
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;}
Compiler Standard VERSION Number: 201112

__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 __cplusplus199711L for the 1998 C++ standard201103L for the 2011 C++ standard201402L for the 2014 C++ standard201703L for the 2017 C++ standard
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
__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
surinderdawra388
gulshankumarar231
C Macro
C Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
TCP Server-Client implementation in C
'this' pointer in C++
Multithreading in C
Exception Handling in C++
Arrow operator -> in C/C++ with Examples
UDP Server-Client implementation in C
Ways to copy a vector in C++
Understanding "extern" keyword in C
Use of bool in C
Smart Pointers in C++ and How to Use Them