Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

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

  1. 1

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

  2. 2

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

  3. 3

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

    I am 15 years old.
    I have 7 apples.
Back

1/13

  1. 1. Creating a Variable
  2. 2. Initialization and Assignment
  3. 3. Several Values in One printf
  4. 4. Decimal Numbers
  5. 5. Characters
  6. 6. Calculating
  7. 7. Whole-Number Division
  8. 8. Quiz: Whole-Number Division
  9. 9. Getting a Decimal Result
  10. 10. Quiz: A Decimal Result
  11. 11. Changing Variables
  12. 12. Constants
  13. 13. Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.