Blog



C language tutorial: Constants and variables
Jacobus Erasmus, 30 July 2018

Let us look at constants and variables in C, and how to use them. We will begin with constants.

We may understand a constant as a shortcut for a certain piece of code. It is a word you can use in your code as a substitute for something else, such as a number, string, or math calculation. Consider, for example, a simple Hello World program:

#include <stdio.h>
#include <stdlib.h>
int main()
{
  printf("Hello, world.\n");
}

Let us alter the above code by defining a constant called MESSAGE to stand in place of the string "Hello, world.\n":

#include <stdio.h>
#include <stdlib.h>
#define MESSAGE "Hello, world.\n"
int main()
{
  printf(MESSAGE);
}

It is a convention to write a constant in all caps, and to define a constant at the top of the file under the '#include ...' lines. As in our example, defining a constant has the following format:

#define NAME_OF_CONSTANT code_to_symbolise

Not only can you define strings as constants, but also numbers and math calculations:

...
#define STRING "Hello, world.\n"
#define NUMBER 24
#define CALC (56 * 45) / 3
...

When should you use a constant? Whenever you use a value multiple times throughout the code, you should define the value as a constant. This makes it easier for you to change the value in the future, if need be. For example, suppose you use a very specific ratio of 1.5 in numerous equations throughout the system. Now, suppose that it turns out the ratio should actually be 1.6. Then, in this case, if you had defined a RATIO constant, then you would simply have to change RATIO to 1.6 as opposed to searching through all the code to replace 1.5 with 1.6.

Let us turn now to variables. A variable is a container that references a value stored in memory. A variable is similar to a constant, but its value can change. However, there are certain types of variables, and if you declare a variable as a certain type, then it can only hold a value of that type. The four most important types are: int (stores integers), float (stores real numbers), double (stores huge or tiny real numbers), and char (stores a single character).

You declare a variable in one of the following two ways:

type variable_name;
type variable_name = variable_value;

The following code illustrates the use variables:

#include <stdio.h>
#include <stdlib.h>
int main()
{
  int i = 3;
  float f = 78.0;
  double d = 78900.0002;
  char c = 'k';
  printf("The integer value is: %d\n", i);
  printf("The float value is: %f\n", f);
  printf("The double value is: %f\n", d);
  printf("The char value is: %c\n", c);
}

The above code should be self-explanatory. Take special note of how to insert a variable into the output string in the printf function. In that function, %d denotes where in the string an integer goes, %f denote a float or double, and %c denotes a character.


Exercises

1. Create a constant CONST with value 8. In the main function, try set CONST to the value 49. What happens when you try to compile and run this code?

2. Define two int variables called x and y, and give them the values 5 and 9, respectively. Output the value of x multiplied by y.