Skip to content
Educora
Beginner15 min2 / 8

Variables, types and operators

Learn to declare variables in C, the types int, float, double and char, the sizeof operator, constants, integer division, the remainder and type casts.

Check yourself
In this lesson you will learn
  • Declare variables of different types and assign values to them
  • Find out with sizeof how many bytes a type takes in memory
  • Create constants with const and #define
  • Calculate correctly with integer division, the % remainder and casts

A supermarket till has to remember how many items you bought, their prices and the total. Programs also keep information in variables. But C is strict: for every variable you must say in advance what its type is — whether it holds a whole number, a fractional number or a character. In return, C uses memory very economically and runs very fast.

Variables and basic types

Definition
Variable

A named place in memory with a type. The type decides which values can be stored there and how many bytes are reserved for it.

A variable is declared like this: first the type, then the name, optionally a starting value after =, and a ; at the end. Names may contain letters, digits and _, but cannot start with a digit. age and Age are two different variables. After the declaration you can assign a new value — without writing the type again.

C
#include <stdio.h>

int main(void) {
    int apples = 7;
    double price = 0.85;
    char size = 'L';
    printf("Apples: %d\n", apples);
    printf("Price: %.2f AZN\n", price);
    printf("Total: %.2f AZN\n", apples * price);
    printf("Size: %c\n", size);
    apples = apples + 3;
    printf("Now: %d apples\n", apples);
    return 0;
}
Expected output
Apples: 7
Price: 0.85 AZN
Total: 5.95 AZN
Size: L
Now: 10 apples
TypeWhat it storesTypical sizeprintf
inta whole number, about −2.1 billion to +2.1 billion4 bytes%d
floata fractional number, about 6–7 exact digits4 bytes%f
doublea fractional number, about 15–16 exact digits8 bytes%f
charone character (really a small integer)1 byte%c
The sizes are typical for modern computers; the standard only guarantees that char is always 1 byte.

For fractional numbers you usually choose double: it takes twice as much space as float but is much more precise. The sizeof operator tells you how many bytes a type takes. Its result has the special type size_t and is printed with %zu.

C
#include <stdio.h>

int main(void) {
    printf("char:   %zu byte\n", sizeof(char));
    printf("int:    %zu bytes\n", sizeof(int));
    printf("float:  %zu bytes\n", sizeof(float));
    printf("double: %zu bytes\n", sizeof(double));
    return 0;
}
Expected output
char:   1 byte
int:    4 bytes
float:  4 bytes
double: 8 bytes
This is the result on a typical 64-bit computer (Windows, Linux, macOS).
Interactive
Loading simulation…
1 byte = 8 bits. Toggle the bits and see that one byte can hold numbers from 0 to 255. A 4-byte int has 32 bits.

A char is really a small integer: the computer stores every character as a code. In the ASCII table the code of 'A' is 65 and the code of 'B' is 66. So if you print a character with %d you see its code, and if you add 1 to it you get the next letter.

C
#include <stdio.h>

int main(void) {
    char letter = 'A';
    printf("%c has code %d\n", letter, letter);
    printf("Next letter: %c\n", letter + 1);
    return 0;
}
Expected output
A has code 65
Next letter: B

Constants

Some values must never change while the program runs: the number π, the maximum score, a tax rate. There are two ways to make them constant. The const keyword makes a variable read-only: if you try to give it a new value, the compiler reports an error. #define is a preprocessor directive: before compiling, it simply replaces the name with the value in the text. Constant names are usually written in capital letters.

C
#include <stdio.h>
#define MAX_SCORE 100

int main(void) {
    const double PI = 3.14159;
    double r = 2.0;
    int score = 87;
    printf("Circle area: %.2f\n", PI * r * r);
    printf("Missing points: %d\n", MAX_SCORE - score);
    return 0;
}
Expected output
Circle area: 12.57
Missing points: 13
If you add the line PI = 3.0;, the program will not compile.

Arithmetic operators and integer division

C has the operators +, -, *, / and %. The most important detail: when you divide two integers, the result is an integer too — the fractional part is simply dropped, not rounded. The % operator gives the remainder of the division and works only with integers. If at least one operand is a fractional number, the division is done the normal way.

C
#include <stdio.h>

int main(void) {
    int a = 17, b = 5;
    printf("%d + %d = %d\n", a, b, a + b);
    printf("%d - %d = %d\n", a, b, a - b);
    printf("%d * %d = %d\n", a, b, a * b);
    printf("%d / %d = %d\n", a, b, a / b);
    printf("%d %% %d = %d\n", a, b, a % b);
    printf("17.0 / 5 = %f\n", 17.0 / 5);
    return 0;
}
Expected output
17 + 5 = 22
17 - 5 = 12
17 * 5 = 85
17 / 5 = 3
17 % 5 = 2
17.0 / 5 = 3.400000

Casts and shorthand operators

Suppose Murad wants to share 7 manats between 2 friends. 7 / 2 gives 3. To get the correct answer, you must turn one of the values into a double with a cast: (double) 7 / 2. Careful: if you write (double) (7 / 2), the integer division happens first and you get 3.0. In the other direction, (int) 9.99 drops the fractional part and gives 9.

C
#include <stdio.h>

int main(void) {
    int total = 7, people = 2;
    printf("%d\n", total / people);
    printf("%.1f\n", (double) total / people);
    printf("%.1f\n", (double) (total / people));
    printf("%d\n", (int) 9.99);
    int count = 10;
    count += 5;
    count++;
    printf("%d\n", count);
    return 0;
}
Expected output
3
3.5
3.0
9
16
count += 5 means count = count + 5, and count++ increases the variable by 1.

Key points

  • Every variable has a type: int holds whole numbers, double and float hold fractions, char holds one character.
  • sizeof gives the size of a type in bytes and is printed with %zu; sizeof(char) is always 1.
  • const makes a variable unchangeable; #define replaces a name with a value before compiling.
  • Dividing two integers gives an integer: 17 / 5 = 3, 17 % 5 = 2.
  • For a fractional result, cast before dividing: (double) a / b.

Check yourself

10 questions. Every correct answer earns XP.

1 / 10
Given int a = 17, b = 5;, what is a / b?