C / C++ / C#

  1. Home
  2. Computing & Technology
  3. C / C++ / C#

C++ Tutorial - About Expressions and Statements

By David Bolton, About.com

4 of 8

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. It's better to use const because the compiler can check them for type correctness and prevents changes to const variables.

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 <iostream>

using namespace std;
int main()
{

#define c __LINE__
const int d = __LINE__;
cout << "Value of Line is " << __LINE__ << endl ;
cout << "Value of Filename is " << __FILE__ << endl ;
cout << "Value of c is " << c << endl ;
cout << "Value of d is " << d << endl ;
cout << "Today's date and time is " << __DATE__ << __TIME__ << endl ;
return 0;
}
Note that I've used __LINE__ three times, once as a #define. The output of this is
Value of Line is 12
Value of Filename is e:\cplus\c++\lesson four\ex1\ex1.cpp
Value of c is 14
Value of d is 11
Today's date and time is Feb 19 200707:14:50
The value of __LINE__ changed between the three uses of it.

On the next page : About Type Conversions

Explore C / C++ / C#

About.com Special Features

Build Your Own Website

Step-by-step advice on how to do everything from choosing a Web host to promoting your content. More >

Connect Your Home Computers

Easy ways to connect two computers for networking purposes. More >

C / C++ / C#

  1. Home
  2. Computing & Technology
  3. C / C++ / C#
  4. C++
  5. Learn C++ Programming
  6. Preprocessor Built In Names

©2009 About.com, a part of The New York Times Company.

All rights reserved.