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

C++ Tutorial - About Expressions and Statements

By , About.com Guide

3 of 8

Const and Include Files

The const keyword

Most C++ compilers, including Open Watcom allow the const keyword (and it's part of C++). However it is part of the C++ syntax, not preprocessed, so it applies to a variable rather than a value at compile time that the compiler understands.

Here's an example.

#include <iostream>

using namespace std;
int main() {

#define c 12
const int a=11;
const int b=5+c;

cout << "Value of a= " << a << endl ;
cout << "Value of b= " << b << endl ;

return 0;
}
The line assigning a value to b may appear odd as it mixes #define and const. When compiled, the value 12 is substituted for c, so this line sets b to 5 + 12, i.e. 17.

Important Points About Const

  • Once a variable is declared const it cannot be changed. The compiler will reject code that tries.
  • As it can't be changed later, you must initialize it when declaring it.
This compiles but gives undefined values.
const int a;
Const has many more uses in function parameters and return types and we'll come back to these in the future.

Include Files

We've already used an include file in previous examples.
#include <iostream>
The preprocessor picks up the #include and merges the header file into the source prior to compiling it.

This is how to split large C and C++ split large applications into many smaller source code files.

There are two versions of #include.

  1. #include <system file>
  2. #include "filename"
The first one includes a compiler's standard header files. The location of these varies with each compiler. The second one looks for files in the same folder or within a specified path.

On the next page : Learn about built in preprocessor names.

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

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

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

  1. Home
  2. Computing & Technology
  3. C / C++ / C#
  4. C++
  5. Learn C++ Programming
  6. Const and Include Files

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

All rights reserved.