Data Types Overview
Every variable in C has a data type that determines two things: how much memory it occupies, and how the bits stored in that memory are interpreted. C's type system is deliberately small and close to the hardware, which is part of why the language maps so directly onto how a CPU actually thinks about numbers and memory.
The fundamental types
C provides a handful of basic (scalar) types built directly into the language. Every other type — arrays, pointers, structures, unions — is built up from these fundamentals or from user-defined combinations of them.
Type | Typical use | Example literal |
|---|---|---|
| Whole numbers |
|
| Single-precision decimal numbers |
|
| Double-precision decimal numbers (default for real numbers) |
|
| A single byte, usually one character |
|
| "No type" — used for functions with no return value, or generic pointers | n/a |
#include <stdio.h>
int main(void) {
int age = 30;
float gpa = 3.8f;
double pi = 3.14159265358979;
char grade = 'A';
printf("age=%d gpa=%.1f pi=%.5f grade=%c\n", age, gpa, pi, grade);
return 0;
}age=30 gpa=3.8 pi=3.14159 grade=A
No built-in bool before C99
#include <stdbool.h>
#include <stdio.h>
int main(void) {
bool is_ready = true;
if (is_ready) {
printf("Ready!\n");
}
return 0;
}Derived and user-defined types (a preview)
On top of the fundamental types, C lets you build richer types that we will cover in detail in later sections:
Arrays — a fixed-size sequence of values of the same type.
Pointers — a variable that stores the address of another variable.
Structures (
struct) — a bundle of related values, possibly of different types, grouped under one name.Unions (
union) — like a struct, but all members share the same memory.Enumerations (
enum) — a set of named integer constants.
Checking a type's size with sizeof
C does not fix the exact size of most types in bytes — it only guarantees minimums (covered in depth in Integer Types and Type Sizes & limits.h). To find out how large a type actually is on your platform and compiler, use the sizeof operator.
#include <stdio.h>
int main(void) {
printf("sizeof(int) = %zu bytes\n", sizeof(int));
printf("sizeof(double) = %zu bytes\n", sizeof(double));
printf("sizeof(char) = %zu bytes\n", sizeof(char));
return 0;
}sizeof(int) = 4 bytes sizeof(double) = 8 bytes sizeof(char) = 1 bytes