Predefined Macros

This page was translated by a robot.

By default, the preprocessor provides some predefined macros that are available in every code. Depending on the system, however, the scope of the predefined macros can vary. Only the most important are listed here.

Standard Macros

The following macros should be available on every system under every compiler.

__FILE__   Returns the full path of the file as a string.
           Can be controlled using the #linedirective .
__LINE__   Returns the current line number as a constant.
           Can be controlled using the #linedirective .
__DATE__   Returns the current date as a string.
__TIME__   Returns the current time as a string.



File: test.cpp
Line: 5
Date: Aug 27 2007
Time: 21:23:48
#include <stdio.h>

int main(){
  printf("File: %s\n", __FILE__);
  printf("Line: %d\n", __LINE__);
  printf("Date: %s\n", __DATE__);
  printf("Time: %s\n", __TIME__);
  return 0;
}

Debug or Release

Another macro that is needed in one form or another on most systems is whether the compiler is compiling a debug version or a release version. The only macro that exists according to the standard for this case is NDEBUG, which means NON-Debug . This constant should then be defined when the compiler creates a release version.

Additional checks are often programmed into debug compiles, which, however, should not be carried out in the release version. Here is an example of such a check:

double getInverse(double x){
  #ifndef NDEBUG
    if(x == 0.f){
      printf("Warning! Inverse of zero.\n");
      return 0.f;
    }
  #endif
  return 1.f / x;
}

Note that the notation is to be ifndeftaken as a double negative: if the compiler is NOT in NOT debug mode, then execute debug code. This might be confusing at first, but that's how it's used.

Some standard library functions check this macro and perform extended checks as the case may be. For example, the assert()function is defined as an empty function if NDEBUGis defined. However, not all systems define this constant automatically, so it may have to be set manually in the compile settings for release versions.

C or C++ Compiler

Today's development environments use the file extension to recognize whether a C or a C++ compiler is to be called. Within a header file, this decision can be checked using macros. Most compilers have now agreed to use the same macros. For the sake of completeness, the following table also lists two other frequently requested macros.

__ASSEMBLER__ Should be defined, when an Assembler is running.
__STDC__      Should be defined, when the C compiler is standardized.
__cplusplus   Should be defined, when the C++ is being compiled.
__OBJC__      Should be defined, when the Objective-C is being compiled.

Variable Parameters

If a macro with a variable number of arguments is defined using the directive, the preprocessor makes them available as a macro #define, .__VA_ARGS__.

#define PRINT(string, ...) printf(string, __VA_ARGS__)