- Computers & Software»
- Computer Science & Programming»
- Programming Languages
Declaring variables and constants in C
C programs manipulate data through the use of variables and constants. If you haven't already, please click this link: C Data Types, Variables and Constants to get an overview of data types in C.
Variables and constants need to be "declared" before they can be used. A declaration statement in C is a statement that lists an identifier or identifiers and their data type so that they can be used in a program. Recall that an identifier can be a variable, constant or a function. These are the letters or words that you create in your programs.
For example, to declare an integer variable called "busNumber", you'd do this:
You can also list mutlple identifiers separated by a comma in one line like this:
int busNumber, totalSeats, noOfDrivers, noOfStops;
Note that all the variables above, busNumber, totalSeats, noOfDrivers and noOfStops are all declared as an Integer. You can also do this equally well for the other data types. Please see more examples below for the different ways to declare variables.
float height, weight; float temperature; char letterPressed, firstLetter, lastLetter; char ch; int min_value, max_value; int a; int b; int c;
I've only declared variables so far, however, the declarations above will work exactly the same for constants. Remember, though, that constants can only be assigned a single value and that value can never be changed as the program runs. Furthermore, that value can only be assigned at the time of compilation (also known as "compile-time").
This brings us to the topic of "initialization".
Initialization is the process of assigning the very first value to a variable or constant. For a variable, this can happen on or after declaration. For a constant, however, it can only happen during declaration.
Below are two examples of initializing a variable. Note that the first block of code below initializes the variable after the declaration while the second block of code initializes the variable during declaration.
int startTime; startTime = 0;
int startTime = 0;
To initialize a constant, you would do it like this:
const int startTime = 0;
If you try to initialize a constant after the declaration, the compiler will give you an error message, something similar to this one:
$ gcc t.c -o t t.c: In function `main`: t.c:13: error: assignment of read-only variable `startTime`
Please see more examples below for a mixture of declarations and initializations of variables and constants.
float height = 0.0, weight = 0.0; float temperature = 37.5; char letterPressed = '', firstLetter = 'a', lastLetter = 'z'; char ch; int min_value = -12, max_value = 12; const int a = 1; const int b = 5; int c = 0;
Declare before use
Always remember to declare a variable or constant before you use it, otherwise, if you don't, maybe something like the code block below:
a = 1000; int a;
The compiler will give you an error complaining about an unknown variable, something similar to this:
$ gcc t.c -o t t.c: In function `main`: t.c:13: error: `a` undeclared (first use in this function) t.c:13: error: (Each undeclared identifier is reported only once t.c:13: error: for each function it appears in.)
I'd like to show you a different way of creating constants. Technically this would fall under the topic known as "C preprocessor". However, many programmers would use this facility in order to define constants.
We make use of the preprocessor term "#define". There are many other pre-processor terms that we'll discuss later, but for now, let's simply look at how to use #define to define a constant. Please note that I'm using the term "define" and not "declare", you'll know why shortly.
Here's a statement that would define a constant identifier called "MAX_VALUE".
#define MAX_VALUE 1000000
Three things to note:
- "MAX_VALUE" in all capital letters compared to our earlier examples of variables and constants. Actually, you can use the usual small letters as before, however, today's convention would tell you that it's actually good to use capital letters to differenciate them from constants declared with the "const" keyword.
- There is no semi-colon character to end the statement.
- There is no data type specified.
What this #define does is create a word or letter that gets substituted with the expression defined. In other words, anywhere in your program that "MAX_VALUE" appears, it will be substituted with "1000000". This happens during compilation. Here's an example:
#define MAX_VALUE 1000000 int x, y; x = MAX_VALUE; y = MAX_VALUE + 100;
The previous code defines MAX_VALUE to be 1000000 ("one million"). Then we declare two integer variables x and y. Then the x and y assignment statements will be translated by the compiler into this:
x = 1000000; y = 1000000 + 100;
Note that the compiler simply did a straight substitution of MAX_VALUE with 1000000. Analyze this next one:
#define CINT const int CINT x = 5; CINT y = 10;
The code above defines CINT as "const int". Then followed by two lines that started with CINT. These two statements would be translated by the compiler as:
const int x = 5; const int y = 10;
As before, the compiler simply did a straight substitution and the statements resulted into two valid constant declarations. This behavior of straight substitution is the reason why you don't use a data type when defining constants this way. Imagine what will happen if you do this:
#define const int MAX_VALUE 1000000
The compiler will translate the above code as if you are defining "const" to have the value of "int MAX_VALUE 1000000". In other words, you are not defining "MAX_VALUE" at all.
The same reason for not putting a semi-colon at the end of the #define statement. If you did something like this: (This is a common mistake, by the way, since most C statements end with semi-colon so it's tempting to put one here.)
#define MAX_VALUE 1000000; int x = MAX_VALUE + 100;
The assignment statement ("x = ") in the previous code will be translated into a code which is not what you wanted, as shown below:
int x = 1000000; + 100;
The #define is pretty powerful, though what often comes with power is responsibility. Therefore use with care.
You can think of more uses of this especially when it comes to functions. However, you'd usually see this #define being used in defining constants.
I hope this has been useful to you in learning C programming. I'll discuss the so-called "operators" next time and combined with your knowledge of variables and constants, you will soon find yourself writing some simple but useful programs.