1. Computing & Technology

Discuss in my forum

C Tutorial - Lesson Four - About Expressions

By , About.com Guide

4 of 4

Preprocessor Built In Names

Do I use #define or const?

Use Both. #defines can be included in header files and included in many files. But const variables tend to be limited to one file at a time.

Built In Preprocessor Names

These four names are built into the C preprocessor. They may come in handy for tracking down bugs.

Each starts and ends with two underscores.

  • __LINE__ This is an integer that has the current source file line number.
  • __FILE__ This is the name of the current source code file as a string.
  • __DATE__ This is the PC's current date, as a string like Aug 17 2006.
  • __TIME__ This is the PC's current time in string "hh:mm:ss" format.
Here is example 3c.
 #include <stdio.h> int main() {
 
 #define c __LINE__
 const int d = __LINE__;
 printf("Value of Line is %i\n\r",__LINE__) ;
 printf("Value of Filename is %s\n\r",__FILE__) ;
 printf("Value of c is %i\n\r",c) ;
 printf("Value of d is %i\n\r",d) ;
 printf("Today's date and time is %s %s\n\r",__DATE__,__TIME__) ;
 
 return 0;
 }
 
Note that I've used __LINE__ three times, once as a #define. The output of this is
 Value of Line is 7
 Value of Filename is example3.c
 Value of c is 9
 Value of d is 6
 Today's date and time is Aug 17 2006 21:15:50
 
The value of __LINE__ changed between the three uses of it. Notice that the #define for c was before the const int d but because c was substituted in line 9, it had that value.

That completes this lesson. In the next lesson, you'll learn about Control Statements.

©2012 About.com. All rights reserved.

A part of The New York Times Company.