C Variables

A variable is a name given to a particular area in memory that stores a value, which can later be reused. When creating a variable in C it is necessary to specify the type of data to be stored within the variable, for example, whether it is a character, integer or floating-point number. The value of a variable can be changed as much as is necessary once it has been created.

When naming variables in C it should be noted that the names are case sensitive. Variable names must start with a letter or underscore, but can then be followed by either letters, numbers or underscores and should not exceed 31 characters in length. One further restriction is that a variable name cannot be the same as a reserved word. Reserved words, or keywords, as they are sometimes referred to, are those that have a specific purpose in C and therefore cannot be used for anything else.

When declaring a variable, it takes the following format.

variable_type variable_name;

Variable name is the name given to the variable in memory, which can be used to reference it later. Variable type refers to the type of data that can be stored in a variable. The most commonly used data types in C are ‘char’, ‘_Bool’, ‘int’, ‘float’ and ‘double’, where ‘char’ contains a single character, ‘_Bool’, short for Boolean, is either ‘true’ or ‘false’, represented by 1 for true and 0 for false, ‘int’ is a whole number, ‘float’ refers to a number that can contain decimal places, such as 1.5, and ‘double’, which is similar to ‘float’ but can contain numbers with greater precision.

Below are examples of variable declarations.

char firstInitial;
_Bool result;
int age;
float price;
double total;

Once a variable has been declared, a value can be assigned to it.

firstInitial = 'F';
result = 1;
age = 30;
price = 12.99f;
total = 12.999999;

It should be noted that if the header file ‘stdbool.h’ is included, then a Boolean variable can be declared using the ‘bool’ variable type, instead of ‘_Bool’ and initialised with the words ‘true’ or ‘false’. A Boolean variable still stores a 1 or 0 even if it is initialised with ‘true’ or ‘false’.

bool result;
result = true;

Instead of declaring a variable and then assigning a value to it on a separate line, the two operations can be combined, so the above can be re-written as follows.

char firstInitial = 'F';
bool result = true;
int age = 30;
float price = 12.99f;
double total = 12.999999;

It is also possible to declare variables of the same type on one line, separated by commas.

char firstInitial = 'F', lastInitial = 'B';

Unlike in a lot of other programming languages, there is no variable type for a string of characters. If a string of characters is required to be stored, then a character array must be used. Both arrays and strings are discussed in more detail separately.

The examples shown here are just a small number of the types of variables that are available.

Format Specifiers

Format specifiers allow variables to be displayed as output, for example, to the console. A ‘printf’ statement can be used to display formatted output to the console, incorporating format specifiers to display variable values where needed. All format specifiers start with a percent sign and are followed by one or more characters. The example below incorporates the value of the variable ‘firstInitial’, which is of type ‘char’, into a ‘printf’ statement.

printf("The variable 'firstInitial' = %c", firstInitial);

The format specifier varies depending on the type of data that the variable holds. In the above case, the variable ‘firstInitial’ is of type ‘char’, so the format specifier ‘%c’ is required. The format specifier is replaced with the value of the variable included after the comma. The resulting output to the console can be seen below.

The variable 'firstInitial' = F

Here are more examples for the other types of variables show above.

printf("The variable 'result' = %i\n", result);
printf("The variable 'age' = %d\n", age);
printf("The variable 'price' = %.2f\n", price);
printf("The variable 'total' = %.6f\n", total);

It should be noted that, for the last two examples above, where the variables incorporated are of type ‘float’ and ‘double’ respectively, the number of decimal places have been specified, with the ‘.2’ between the percent sign and the ‘f’ for the price variable, signifying two decimal places and ‘.6’ specifying six decimal places for the ‘total’ variable. The ‘\n’ is an escape sequence that forces a new line at the end of the message.

The variable 'result' = 1
The variable 'age' = 30
The variable 'price' = 12.99
The variable 'total' = 12.999999

Two variables can be incorporated by separating them with a comma.

printf("The variables 'firstInitial' and 'age' = %c and %d\n", firstInitial, age);

This produced the following output.

The variables 'firstInitial' and 'age' = F and 30