C Variables & Constants Tutorial

In this C tutorial we learn about simple data containers in C, called variables and constants, that temporarily store our program's data while it runs.

We cover how variables are mutable and can be changed, but constants are immutable and cannot be changed at runtime.

Variables in C

A variable is a data container that will store the program’s temporary data in a system’s memory (RAM) while it runs.

When the application quits, the data is cleared from memory and is made available to the system again.

Variables are used all over an application. As an example, let’s consider a calculator.

Let’s say we want to do simple addition.

Example:
1 + 5 = ?

The program will perform arithmetic on these numbers and return back the result. But, the program doesn’t necessarily know which numbers the user will input. The program also cannot contain each and every addition in existence.

The solution is to give the program the two unknown numbers and the logic of what it has to do with them.

To do this, we store the two unknown numbers in variables. We also give the program another variable to store the result in, just in case the user wants to use the result in a calculation.

Our simple calculation would then look like this.

Example:
num1 + num1 = result

When the application quits, the data that’s stored in the variables during our calculations is deleted. Variables aren’t a long term storage solution like a file on a hard drive.

How to declare a variable in C

To create a variable in C, we declare it. Variables in C can only hold a specific type of data. For example, a variable for our simple calculator will only be able to hold numbers.

A variable has to be declared with:

  • A type
  • A name

We specify the type first, followed by the name and then we terminate the statement with a semicolon.

Syntax:
type name;
Example:
#include <stdio.h>

int main()
{
    int number;

    return 0;
}

In the example above, we declare a variable called number of type int. This means that the variable will only be able to hold integers, or full numbers.

Our variable is now ready and waiting to accept data to be stored inside of it.

How to assign data to a variable in C

When a variable has been declared, we can store data inside of it. To do this, we assign data to it with the assignment operator.

The assignment operator is the = symbol and will put whatever data is on the right, into whatever container is on the left.

To assign data to an existing variable we write the variable name, followed by the = symbol and then the data we want to store. Lastly, the statement is terminated by a semicolon.

Syntax:
type name;

name = data_to_store;

Notice that we don’t specify the variable’s type again. A variable’s type is only specified when it’s declared, not when we access or refer to it.

Example:
#include <stdio.h>

int main()
{
    int number;
    number = 1;

    return 0;
}

In the example above, we declare a variable of type int called number. On the next line we assign the value of 1 to the variable.

At any time when we need to change the data in the variable, we assign a new value to it with the assignment syntax.

Example:
#include <stdio.h>

int main()
{
    int number;
    number = 1;
    number = 5;
    number = 10;

    return 0;
}

In the example above, we change the value of the variable with the same assignment syntax. The variable will hold the last value it was assigned.

How to access data in a variable in C

When we want to access the data inside a variable, or refer to it for any other reason, we use its identifier.

For example, let’s say we want to do some arithmetic on two numbers. We would use the variable names in the calculation to refer to their values.

Example:
#include <stdio.h>

int main()
{
    // declare variables
    int num1;
    int num2;
    int result;

    // assign values
    num1 = 1;
    num2 = 5;

    // calculate and store result
    result = num1 + num2;

    // print the value to the console
    printf("%d\n", result);

    return 0;
}

In the example above, we add the two number variables and store the result in the variable named result.

When we run the example, the result is printed to the console.

How to initialize a variable in C

If we know the data we want to store into a variable beforehand, we can declare the variable and assign data to it in a single statement.

This process is known as initialization. To initialize a variable we combine the declaration and assignment syntax into one.

Syntax:
 type name = data_to_store;
Example:
#include <stdio.h>

int main()
{
    // initialization
    int number = 10;

    return 0;
}

In the example above we initialize a variable with data.

How to create more than one variable at once in C

It’s possible for us to declare or initialize multiple variables of the same type inline, that’s to say, in a single statement.

Syntax:
 type varName, varName, varName, etc...;

Instead of writing out each statement separately when we have variables of the same type, we write the type once and separate the variables themselves with a comma.

Example:
#include <stdio.h>

int main()
{
    int num1, num2;

    return 0;
}

In the example above, we only specify the int type once but declare two int variables inline.

It would be the same as writing the statements on separate lines.

Example:
#include <stdio.h>

int main()
{
    int num1;
    int num2;

    return 0;
}

The same syntax applies when we want to initialize multiple variables inline. We still separate the variables with commas, but we assign their values as well.

Example:
#include <stdio.h>

int main()
{
    int num1 = 10, num2;

    return 0;
}

In the example above, we initialize the first variable with a value, but only declare the second variable. Both can be initialized, or only one, or both can be declared.

This is simply a shorter way to write declarations and initializations.

Constants in C

A constant is also a simple data type that can store temporary data in memory while the application runs.

The difference between a variable and a constant is that the value of a constant cannot be changed at runtime, that’s to say while the program is running.

How to initialize a constant in C

We need a way to tell the compiler that we want to treat a specific variable as an immutable constant.

To do this we add the const keyword in front of the type when declaring a constant.

Syntax:
 const type NAME;

Remember the convention of writing the identifiers of constants in all uppercase.

Example:
#include <stdio.h>

int main()
{
    const float PI;

    return 0;
}

In the example above, we declare a constant called PI. However, we won’t be able to change the value of PI at runtime, so we won’t be able to assign data to it.

For that reason, a constant has to be initialized with data. There is no rule that says we must, but what would be the purpose of an empty unusable constant.

To initialize a constant, we use basic initialization syntax.

Syntax:
 const type NAME = value;
Example:
#include <stdio.h>

int main()
{
    const float PI = 3.14;

    return 0;
}

The value of PI won’t be able to be changed at runtime.

How to access data in a constant in C

We can access data inside a constant by referring to its name when we need to.

Example:
#include <stdio.h>

int main()
{
    const float PI = 3.14;

    float circumference;
    float diameter = 4;

    circumference = PI * diameter;

    printf("C = %.2f", circumference);

    return 0;
}

In the example above, we use PI to calculate the circumference of a circle. In the calculation logic, we simply refer to the name of the constant PI.

Summary: Points to remember

  • Variables and constants are data containers that store our program’s temporary data through runtime.
    • Variables are mutable, their values may be changed at runtime.
    • Constants are immutable, their values cannot change during runtime.
  • A constant should be initialized with a value instead of being declared empty.
  • A constant must use the keyword const for the compiler to recognize it as a constant.
  • A constant is conventionally named with all capital letters.