Lesson 3: The use and declaration of constants.
This lesson will show you how to:
- Use constants in C
- Declare constants in C
- the #define directive
- the keyword const
Q: What's the difference between a lawyer and a bucket
A: The bucket.
A constant is similar to a variable in the sense that it
represents a memory location (or simply, a value). It is
different from a normal variable, as I sure you can guess,
in that it cannot change it's value in the proram - it must
stay for ever stay constant. In general, constants are a
useful because they can prevent program bugs and logical
errors(errors are explained later). Unintended modifications
are prevented from occurring. The compiler will catch attempts
to reassign new values to constants.
There are three techniques used to define constants in C.
First, constants may be defined using the preprocessor directive
(the preprocessor-directives are explained in summary guide
1 in the advanced user category - don't be scared - they
are very easy to understand) #define. The preprocessor
is a program that modifies your source file prior to compilation.
Common preprocessor directives are #include, which is used
to include additional code into your source file, #define,
which is used to define a constant. The standard is that
constants are always in uppercase whereas normal variables
are in lowercase - you don't have to follow this convention
- it just makes things easier. Remember that this statement
must not contain a semi-colon, and must not contain the
assignment operator (in english it is equals, =).
The #define directive is used as follows:
#define ID_NO 12345
Wherever the constant appears in your source file, the
preprocessor replaces it by its value. So, for instance,
every "pi" in your source code will be replace
by 3.1415. The compiler will only see the value 3.1415 in
your code, not "pi". Every "pi" is just
replaced by its value. Here is a simple program illustrating
the preprocessor directive #define.
#define MONDAY 1
#define TUESDAY 2
#define WEDNESDAY 3
#define THURSDAY 4
#define FRIDAY 5
#define SATURDAY 6
#define SUNDAY 7
int today = MONDAY;
if ((today == SATURDAY) || (today
to work or school\n");
Don't worry if you don't understand this program, just
look at it's basic structure that's all. All will be revealed.
Good things come to those who wait!
How do you know if a woman is about to say something smart?
When she starts her sentence with
something like "A man once told me..."
Using const variables
The second technique is to use the keyword const
when defining a variable. When used the compiler will catch
attempts to modify variables that have been declared const.
const float pi = 3.1415;
const int id_no = 12345;
There are two main advantages over the first technique.
First, the type of the constant is defined. "pi"
is float. "id_no" is int. This allows some type
checking by the compiler. Second, these constants are variables
with a definite scope. The scope of a variable relates to
parts of your program in which it is defined. Some variables
may exist only in certain functions or in certain blocks
of code. You may want to use "id_no" in one function
and a completely unrelated "id_no" in your main
program. Sorry if this is confusing, the scope of variables
will be covered in a latter lesson.
back to top go
to next lesson