Loading
0x20Lesson 3 of 15

Variables and types

Store values in named boxes of memory, and learn why every box in C has a type and a size.

12 min 6-question quiz 1 code exercise
By the end of this lesson you can
  • Declare and initialize variables of the basic types
  • Choose between int, double and char for a value
  • Use sizeof and explain why types have sizes

A variable is a named piece of memory that holds a value. In C you must say what kind of value each variable holds - its type - before you use it. The type tells the compiler how many bytes to set aside and how to interpret them.

variables.c
1#include <stdio.h>
2
3int main(void) {
4    int age = 30;
5    double height = 1.75;
6    char grade = 'A';
7
8    printf("Age: %d\n", age);
9    printf("Height: %.2f m\n", height);
10    printf("Grade: %c\n", grade);
11    return 0;
12}
Output
Age: 30
Height: 1.75 m
Grade: A

Declaring and initializing

int age = 30; does two things: it declares a variable named age of type int, and initializes it with the value 30. You can also declare first and assign later:

int score;      // declared, but holds garbage until assigned!
score = 100;    // assignment
score = score + 5;

Variable names can use letters, digits and underscores, cannot start with a digit, and are case-sensitive: total and Total are different variables. Pick names that say what the value means: student_count, not n.

The basic types

| Type | Holds | Typical size | printf format | | --- | --- | --- | --- | | char | one character (really a small integer) | 1 byte | %c | | int | whole numbers | 4 bytes | %d | | long | bigger whole numbers | 8 bytes on most 64-bit systems | %ld | | float | decimal numbers, ~7 digits of precision | 4 bytes | %f | | double | decimal numbers, ~15 digits of precision | 8 bytes | %f |

Integer types can be unsigned too (unsigned int), which trades negative numbers for twice the positive range. For decimals, prefer double over float unless you have a reason - the extra precision is almost always worth it.

Sizes and sizeof

Every type occupies a fixed number of bytes, and the sizeof operator tells you how many. Sizes can differ between platforms (the C standard only guarantees minimums), which is exactly why sizeof exists: never hard-code a size you can ask the compiler for. sizeof produces a value of type size_t, which you print with %zu.

sizes.c
1#include <stdio.h>
2
3int main(void) {
4    printf("char:   %zu byte\n", sizeof(char));
5    printf("int:    %zu bytes\n", sizeof(int));
6    printf("double: %zu bytes\n", sizeof(double));
7    return 0;
8}
Output
char:   1 byte
int:    4 bytes
double: 8 bytes

Characters are numbers

A char is a one-byte integer. Character literals in single quotes, like 'A', are just a friendly way of writing that character's code - 65 for 'A' in ASCII. That means you can do arithmetic on characters:

chars.c
1#include <stdio.h>
2
3int main(void) {
4    char letter = 'A';
5    printf("%c is %d\n", letter, letter);
6    printf("Next letter: %c\n", letter + 1);
7    return 0;
8}
Output
A is 65
Next letter: B

Constants

If a value should never change, say so with const: const double PI = 3.14159;. The compiler will refuse any later assignment to it, which documents your intent and catches mistakes. You will also see #define PI 3.14159 in older code - a preprocessor text substitution with no type. Prefer const for values.

Key takeaways

  • Every variable has a type that fixes its size and meaning. Declare it, and initialize it - uninitialized locals hold garbage.

  • Use int for whole numbers, double for decimals, char for single characters (which are small integers).

  • Ask for sizes with sizeof (printed with %zu) instead of assuming them, and mark values that never change const.

Lesson quiz

6 questions · pass with 5 correct · up to 50 XP

Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.

Practice: write real C

These run for real. Write the program, press Run tests, and read what the compiler tells you - learning to read compiler messages is half of learning C.

Exercise 1

Profile card

+25 XP

Create variables for a person and print them. Use an int named age set to 21, a double named gpa set to 3.8, and a char named initial set to 'K'. Print exactly:

Initial: K
Age: 21
GPA: 3.8

Print the GPA with one digit after the decimal point (%.1f).

  • Prints the profile
main.c
Loading editor…

Your code is compiled with gcc 14 (-Wall -Wextra) and run on Compiler Explorer (godbolt.org), a free public service.

Questions about this lesson

Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.

Sign in to ask questions, help other learners, and earn XP for taking part.

Loading posts…

Did you like the lesson? 😆👍
Consider a donation to support our work: