Lesson 17 min read

Variables & Data Types

Label every shelf with its exact size

The Strict Librarian

Imagine a librarian who runs the tightest ship in town. Before you can put anything on a shelf, you must tell her exactly what kind of item it is and how much space it needs. A paperback? Shelf A. An encyclopedia? Shelf B. No guessing, no surprises.

That's C. Unlike Python or JavaScript where you just toss values around and the language figures it out, C demands you declare the type up front. This strictness is what makes C fast β€” the compiler knows exactly how many bytes to reserve for each variable.

The Core Data Types

C gives you a handful of fundamental types, each with a specific size in memory:

  • char β€” 1 byte. Holds a single character like 'A' or a small integer (-128 to 127).
  • int β€” Typically 4 bytes. Your go-to for whole numbers.
  • float β€” 4 bytes. Decimal numbers with ~6-7 digits of precision.
  • double β€” 8 bytes. Decimal numbers with ~15-16 digits of precision. The default for floating-point math.
  • short β€” Typically 2 bytes. A smaller integer when memory is tight.
  • long β€” Typically 8 bytes (on 64-bit systems). For when int isn't big enough.

You can also add unsigned in front of integer types to say "no negative numbers allowed" β€” which doubles the positive range.

Declaring Variables

#include <stdio.h>
int main() {
int age = 25;
float temperature = 98.6f;
double pi = 3.14159265358979;
char grade = 'A';
short small_num = 100;
long big_num = 1000000000L;
unsigned int positive_only = 42;
printf("Age: %d\n", age);
printf("Temp: %.1f\n", temperature);
printf("Pi: %.14f\n", pi);
printf("Grade: %c\n", grade);
printf("Small: %hd\n", small_num);
printf("Big: %ld\n", big_num);
printf("Positive: %u\n", positive_only);
return 0;
}
Output
Age: 25
Temp: 98.6
Pi: 3.14159265358979
Grade: A
Small: 100
Big: 1000000000
Positive: 42

sizeof β€” Measuring the Shelves

Ever wonder exactly how many bytes each type takes? The sizeof operator tells you. This is important because sizes can vary between systems β€” an int might be 2 bytes on an old embedded device or 4 bytes on your laptop.

Using sizeof()

#include <stdio.h>
int main() {
printf("char: %zu byte\n", sizeof(char));
printf("short: %zu bytes\n", sizeof(short));
printf("int: %zu bytes\n", sizeof(int));
printf("long: %zu bytes\n", sizeof(long));
printf("float: %zu bytes\n", sizeof(float));
printf("double: %zu bytes\n", sizeof(double));
// sizeof works on variables too
int score = 100;
printf("score: %zu bytes\n", sizeof(score));
return 0;
}
Output
char:     1 byte
short:    2 bytes
int:      4 bytes
long:     8 bytes
float:    4 bytes
double:   8 bytes
score:    4 bytes

Type Limits & Overflow

Every type has a range. An int can hold roughly -2.1 billion to +2.1 billion. What happens if you go past the limit? The value wraps around β€” like an odometer rolling past 999,999 back to 000,000. This is called overflow, and it's a silent bug that C won't warn you about.

Type Limits and Overflow

#include <stdio.h>
#include <limits.h>
int main() {
printf("int range: %d to %d\n", INT_MIN, INT_MAX);
printf("unsigned int max: %u\n", UINT_MAX);
printf("char range: %d to %d\n", CHAR_MIN, CHAR_MAX);
// Overflow in action!
int max = INT_MAX; // 2,147,483,647
printf("max: %d\n", max);
printf("max + 1: %d\n", max + 1); // Wraps to negative!
unsigned char byte = 255;
printf("byte: %u\n", byte);
byte = byte + 1;
printf("byte + 1: %u\n", byte); // Wraps to 0!
return 0;
}
Output
int range: -2147483648 to 2147483647
unsigned int max: 4294967295
char range: -128 to 127
max:     2147483647
max + 1: -2147483648
byte:     255
byte + 1: 0
Note: C has no built-in bool type (before C99). You can #include <stdbool.h> to get true and false, but under the hood they're just 1 and 0. There's also no string type β€” strings in C are arrays of char ending with a null character '\0'. This is a major difference from most modern languages!

Constants

Sometimes you want a variable that never changes β€” like the speed of light or the value of pi. Use const to make a variable read-only, or #define for a preprocessor constant.

Constants with const and #define

#include <stdio.h>
#define MAX_LIVES 3
int main() {
const double PI = 3.14159265;
const int DAYS_IN_WEEK = 7;
printf("Pi: %f\n", PI);
printf("Days: %d\n", DAYS_IN_WEEK);
printf("Max lives: %d\n", MAX_LIVES);
// PI = 3.0; // ERROR! Can't modify a const
return 0;
}
Output
Pi: 3.141593
Days: 7
Max lives: 3
Challenge

Quick check

What is the typical size of an int on a modern 64-bit system?
← Why Learn C?Operators & Expressions β†’