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

C++ Tutorial - About Expressions and Statements

By , About.com Guide

2 of 8

Preprocessing for Beginners

When compiling C and C++ applications, the preprocessor lets you include constants, macros and include files. This occurs prior to compiling so that what is actually compiled is a processed version of the source code. The preprocessor is a separate stage that happens just before the compilation. A macro is text that gets expanded by the preprocessor such as constants. This can get quite involved and we'll return to it in a future lesson.

Constants

If used in more than one place use a constant, not a number. If you know that the maximum number of widgets you'll use is 25 then define a constant for it like MAXIMUMWIDGETS, rather than use 25 everywhere.

It's a convention to write constants in upper case to distinguish them from variables.

#define MAXIMUMWIDGETS 25
Before the compilation the preprocessor will substitute each instance of MAXIMUMWIDGETS with the value 25. The original source code files aren't changed. This happens before every compilation. If in the future you can use a value of 50 then changing the one #define is simpler and safer than changing every value of 25. It would be easy to change the wrong 25.

The preprocessor does not understand C or C++ syntax. It's just a text editor. That's why there's no semi-colon needed at the end of each line.

Having learnt about #define, you'll learn a better way for defining constants on the next page.

On the next page : Learn more about constants and include files.

Explore C / C++ / C#
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

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

  1. Home
  2. Computing & Technology
  3. C / C++ / C#
  4. C++
  5. Learn C++ Programming
  6. Preprocessing for Beginners

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

All rights reserved.