Skip to main content Skip to navigation

2.2 Constants and variables

1. Data types

When declaring constants and variables, you will always have to declare the type. There are only a few basic datatypes in C

char a character
int an integer up to ±32767
long int a larger integer up to ±2147483647
float a floating point number
double a floating point number with greater precision

Note, the ranges quoted are for 32-bit architecture.

Back to top

2. Variable declarations

Variables must be declared in the code before they are used. The declaration syntax starts with the type, followed by one or more variables, with each variable separated by a comma, e.g.

int big, small, medium;

This declares three variables of integer type, called "big", "small" and "medium".

There are restrictions on what names you can use for variables in C. Your variable names should always start with a letter of the alphabet. You can't have whitespace in variable names, but you can use underscores. Try to avoid special characters. It is conventional to only use lower case letters in variable names, although there is nothing to stop you using a mix of uppercase and lowercase letters, such as variableName. C is case-sensitive though, so variable names ELEMENT, Element and element are all treated as different variable names.

Variables can also have values assigned to them at declaration time, just use an equals sign, e.g.

 float element = 1.3E2;

Variables declared at the start of the code (located outside any of the functions, including main) are known as global variables. they can be used by all functions (including main) in the code. they are typically declared straight after the preprocessor directives. Variables that are declared within a function are known as local variables. Local variables can only be used within that particular function, and will be destroyed when the code exits that particular function. Another type of local variable is the static variable, which differs from the local variable in the sense that the static variable is not destroyed on exit from the function, instead its value is preserved, and becomes available again when the function is next called. Static variables are declared as local variables, but the declaration is preceeded by the word static, e.g.

static int medium;

Character declarations are a little different. C doesn't have any inbuilt capability for dealing with strings. Instead, C treats a string as a character array. The length of this character array, e.g. a word, is always one more than the length of the word. This is because a terminating character needs to be added to the character array, to denote the end of the word. This character is called the NULL character '\0'.

To initialise the variable name to the string skywalker, we would have to use the following code fragment

        #include <stdio.h>
        char name[12] = "skywalker";

Notice that skywalker is only 10 characters long (including the NULL character), so you don't have to use all the space declared for the character array name.

Back to top

3. Constant declarations

One way to handle constants in C is to declare them using const. The syntax for declaring constants is very similar to declaring variables, you just have to prefix your declaration with the word const, and you assign a value to your constant using an equals sign; e.g.

const float pi=3.1415926;

Another way is to use the C-preprocessor directive #define. You have already seen usage of another C-preprocessor directive called #include. To define pi as a constant, in this case you could write #define pi 3.1415926. However, it is custom to write preprocessor constants using all capitals (PI instead of pi). In our case, add

#define PI 3.1415926

at the top of the file along with all the other C-preprocessor directives.

Putting it into Practice:

In this example, edit your existing code to include some variable and constant declarations. You might wish to print out the values of some of these variables and constants. Suppose you have a variable of float type called element, to print out the value of this variable, use a statement like the following;
      printf("The value of element is %f \n", element);
Make sure your code compiles and prints out the correct value.

Back to top

4. Reserved words

You should make sure that none of your variable or constant names are things like int, float, char and so on. Clearly these are reserved words in C and have a special purpose.

Back to top