C / C++ / C#

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

C Tutorial - Lesson Four - About Expressions

By David Bolton, About.com

3 of 4

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 <stdio.h>

int main() {

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

printf("Value of %i\n\r",a) ;
printf("Value of %i\n\r",b) ;

return 0;
}
The line assigning 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 initialise it when declaring it.
This compiles but gives undefined values.
const int a;

Include Files

We've already used an include file in previous examples.
#include <stdio.h>
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 with a specified path.

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

Explore C / C++ / C#

About.com Special Features

C / C++ / C#

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

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

All rights reserved.