× Home
Next Lec → ← Previous Lec

Predefined Macros & other pre-processor directives

Recap - Preprocessor directive

  • Preprocessor is a king of automated editor that modifies a copy of the original source code
  • The #include directive causes the preprocessor to fetch the contents of some other file to be included in the present file
  • In C programming there are two common formats for #include:
  • This file may in turn #include some other file(s) which may in turn do the same
  • Most commonly #included files have a ".h" extension, indicating that they are header files.
  • The #define directive is used to "define" preprocessor "variables"

Other C Preprocessor

#undef

Undefines a macro or preprocessor variable

#ifdef

Returns true if some preprocessor variable or macro is defined

#ifndef

Returns true if some preprocessor variable or macro is not defined

#if

#else

elif

pragma

it is used to issue some special commands to the compiler

Predefined Macros in C

  • __DATE__→ The current date as character literal in "MMM DD YYY" format.
  • __TIME__→ This contains the current time as a character literal in "HH:MM:SS" format.
  • __FILE__→ The current filename as a string literal.
  • __LINE__→ The current line number as a decimal constant.
  • __STDC__→ Defined as 1(one) when the compiler complies with the ANSI standard.

__FILE__

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

The file name is code.c

__DATE__

#include <stdio.h>
int main()
{
printf("Today's date is %s\n",__DATE__);
return 0;
}

Today's date is Jul 10 2022

__TIME__

#include <stdio.h>
int main()
{
printf("Time now is %s\n",__TIME__);
return 0;
}

Time now is 10:49:55

__LINE__

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

Line no is 4

__STDC__

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

ANSI: 1