Skip to content

Names, Values and Types

The Course
•
30 min study

The last lesson printed fixed text. A real report prints numbers that change: today's minutes, this week's total, a rating. If you type 25 into every message that mentions the duration, changing it means finding and editing every copy, and missing one leaves the report contradicting itself. A variable solves this. It gives one storage location a name, so the value lives in one place and every line that needs it reads it from there.

int minutes = 25; does two things. It declares a variable named minutes that holds an int (a whole number), and it initializes it with 25. Later, minutes = 30; is an assignment: it replaces the stored value. Read = as "store into", not as mathematical equality. minutes = minutes + 5; makes no sense as algebra, but as an instruction it is clear: read the current value, add 5, and store the result back.

Predict and run

Predict both lines. Which value does the second printf see?

Predict before running C
#include <stdio.h>

int main(void) {
    int minutes = 25;
    double hours = 0.5;
    char grade = 'A';
    printf("%d %.1f %c\n", minutes, hours, grade);
    minutes = 30;
    printf("%d\n", minutes);
    return 0;
}
Check your prediction
Text
25 0.5 A
30

The first printf ran before the assignment, so it saw 25. The assignment happened afterward and only affects statements that run later. Change the starting value to 40 and run again: the first line changes, the second does not, because 30 is stored before the second print reads minutes.

Every variable has a type

C needs to know what kind of value each variable holds, because the type decides how many bytes it uses and which operations make sense. Three types cover this course's first days:

  • int holds whole numbers such as 25, 0 and -3. Use it for counts and minutes.
  • double holds numbers with a fractional part, such as 0.5 or 1.25. It is approximate, so use it for measurements and averages, not for counting.
  • char holds one character, written in single quotes: 'A'. Text in double quotes, such as "A", is a different thing called a string, which you will meet on Day 4.

The type is fixed when you declare the variable. Assigning a fractional value to an int does not turn it into a double; the fraction is simply dropped.

Placeholders must match their arguments

printf reads its first argument, the format, from left to right. Each placeholder consumes the next argument and says how to print it:

  • %d prints an int.
  • %f prints a double; %.1f prints it with one digit after the decimal point.
  • %c prints a char as a character.

The number of placeholders must match the number of arguments, and each type must match. printf("%d\n", hours); passes a double where an int was promised. That is not a conversion. printf has no way to check the type it received, so it reads the wrong kind of data, and the C standard calls the result undefined behavior: the language makes no promise about what happens. Compilers warn about it, and a local build with this course's flags (-Wall -Wextra -Werror, shown on Day 7) refuses to build. The browser editor builds without those flags, so here the program runs and prints a meaningless number. Don't rely on the editor to catch it: when you see this warning anywhere, treat it as a real bug.

A char is a small integer underneath. The same variable can print as a character or as its numeric code, and adding 1 moves to the next character:

A char is a small number C
#include <stdio.h>

int main(void) {
    char grade = 'A';
    printf("%c %d\n", grade, grade);
    grade = grade + 1;
    printf("%c\n", grade);
    return 0;
}
Text
A 65
B

The number 65 is the character code for A on practically every system you will use. You will rely on this idea on Day 6, when you turn the character '7' into the number 7.

Initialize before you read

C does not give an ordinary local variable a starting value for you. After int total; the variable exists, but its contents are indeterminate: whatever bytes happened to be there. Reading it before storing a value is a bug, and it is another case the standard does not define. The program might print 0 one day and a huge number the next, or the compiler might optimize the code in ways that make no sense if you assumed a particular value.

C
#include <stdio.h>

int main(void) {
    int total;
    printf("%d\n", total);
    return 0;
}

With warnings turned on (-Wall, as in a local build), the compiler warns that total is uninitialized when used here; the browser editor builds without that flag and just prints whatever was in memory. The fix is simple and worth making a habit: give every variable a value in the same statement that declares it, as in int total = 0;.

Update a stored value C
#include <stdio.h>

int main(void) {
    int total = 0;
    total = total + 25;
    total = total + 10;
    printf("Total: %d\n", total);
    return 0;
}
Text
Total: 35

Each assignment read the current total and stored a larger one. The variable started from a known value, so the result is the same on every run.

Practice

Exercise

Your turn

Declare a char topic code 'C', an int of 20 minutes and a double rating of 4.5. Print the first line below, then increase the minutes by 15 with an assignment and print the second line using the same variable.

Text
Topic C: 20 minutes, rating 4.5
Topic C: 35 minutes, rating 4.5
Your workspace C
#include <stdio.h>

int main(void) {
    // Write your solution here.
    return 0;
}
Hint 1: the idea

One format string can hold three placeholders. Their order must match the order of the arguments: character, integer, double.

Hint 2: a first step

Declare and initialize all three variables first, then write the first printf with %c, %d and %.1f.

Show one solution
C
#include <stdio.h>

int main(void) {
    char topic = 'C';
    int minutes = 20;
    double rating = 4.5;
    printf("Topic %c: %d minutes, rating %.1f\n", topic, minutes, rating);
    minutes = minutes + 15;
    printf("Topic %c: %d minutes, rating %.1f\n", topic, minutes, rating);
    return 0;
}
Text
Topic C: 20 minutes, rating 4.5
Topic C: 35 minutes, rating 4.5

The two printf lines are identical; only the stored value of minutes changed between them. That is the point of a variable: one name, read wherever it is needed. Next you will calculate new values from stored ones, and see why 95 / 60 is not what it first appears.

Was this article helpful?

Your response is saved on this device.