Wednesday 9 January 2013

Standard Predefined Macros in C/C++

  C and C++ have the additional benefit of a Preprocessor which can make changes to your program even before compilation. There are some constants predefined by the C/C++ Preprocessor. There are mainly five of them about which I am discussing today. 
  • __FILE__ : This predefined constant contains the name of the C Program being used currently. 
  • __LINE__ : This constant contains the line number of the code being processed currently.
  • __TIME__ : This constant holds the current system time.
  • __DATE__ : This constant holds the current system date.
  • __STDC__ : If this constant is defined as 1 yours is an ANSI standard C Compiler.
Program given below illustrates the uses of these predefined constants.

#include<stdio.h>
#ifdef __STDC__
#define MESSAGE printf("\nYour Compiler is an ANSI Standard C Compiler\n");
#else
#define MESSAGE printf("\nSorry!!! Your Compiler is not an ANSI Standard C Compiler\n");
#endif
int main()
{
    int a,b,c;
    printf("\nCompiled the Program %s and this is line no %d",__FILE__,__LINE__);
    printf("\nThis is line no %d\n",__LINE__);
    #line 100 "MyProg.c"
    printf("\nThe Program is now called %s and this is line no %d",__FILE__,__LINE__);
    printf("\nThis is line no %d\n",__LINE__);
    printf("\nTime is %s",__TIME__);
    printf("\nDate is %s\n",__DATE__);
    MESSAGE
    return 0;
}



The Figure Given Below Shows the Output

No comments:

Post a Comment