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.
#include <iostream>Note that I've used __LINE__ three times, once as a #define. The output of this is
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;
}
Value of Line is 12The value of __LINE__ changed between the three uses of it.
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
On the next page : About Type Conversions

