Creating a Variable

Programs need to remember values: the score in a game, the price of a purchase, the number of pupils in a class. For that, they use variables. A variable is like a box with a label: it has a name, and it holds a value that the program can use later. The box is a small piece of the computer's memory.

In C, every variable also has a data type. It says what kind of values fit in the box: whole numbers, decimal numbers or single characters. You choose the type when you create the variable, and it can't change later.

You create a variable like this:

int age;
  • int is the data type. It means a whole number (from the English word integer).
  • age is the name of the variable. You choose it yourself.
  • The semicolon ; ends the statement, as it does after every printf.

Creating a variable is called a declaration. Now the box exists, but it has no value yet. You store a value in it with =:

age = 15;

Watch out: in programming, = doesn't mean “equals” as in math. Read it as “store”: 15 is stored in the variable age.

To use the value, write the name of the variable. In the previous lesson, you printed numbers with %d: printf("%d\n", 5 + 3);. A variable works the same way: write its name after the comma, and printf puts its value where %d is.

#include <stdio.h>

int main(void) {
    int age;
    age = 15;
    printf("I am %d years old.\n", age);
    return 0;
}

This prints:

I am 15 years old.

The name goes outside the quotes. Everything inside the quotes is printed exactly as it's written, so printf("age\n"); prints the word age, not 15.

Rules for the names of variables:

  • A name can contain letters, digits and the underscore _, but it can't start with a digit: player2 is fine, 2player isn't.
  • A name can't contain spaces. If it has more words, join them with underscores: apple_count, total_price. This style is called snake_case, because the words joined by underscores crawl along the line like a snake. This course uses it for all names.
  • Use only the plain letters a to z, without accents: cafe, not café. Clang would accept café, but not every compiler does, so programmers stick to plain letters.
  • Uppercase and lowercase letters matter: age and Age are two different names. Variable names are usually written in lowercase.
  • A name can't be a keyword, a word that already means something in C, such as int or return.

Choose names that say what the variable holds: apple_count tells you more than a or x.

You can use a variable only after you've declared it, and only under exactly the same name. If you get the name wrong, the program doesn't compile:

#include <stdio.h>

int main(void) {
    int age;
    age = 15;
    printf("%d\n", Age);
    return 0;
}

The compiler reports:

main.c:6:20: error: use of undeclared identifier 'Age'; did you mean 'age'?

The line with printf uses the name Age, but there is no variable with that name. An undeclared identifier is a name that nobody has declared. The compiler even suggests that you probably meant age.

Instructions

Below the printf, declare a variable of type int named apple_count.

On the next line, store the value 7 in it.

Below it, print the variable with %d, so that the program prints:

I am 15 years old.
I have 7 apples.

Start programming for free

By signing up, you agree to the Terms of Service and Privacy Policy.

Or sign up with:

1/11