C Data Types
Every variable in C has a data type. The type tells the compiler how many bytes to reserve, how to interpret those bytes (whole number, decimal, character), and which operations make sense. Choosing the right type matters more in C than in many languages, because there is no automatic "big number" type that grows as needed.
The basic types
| Type | Typical size | Range / precision | Format specifier |
|---|---|---|---|
char |
1 byte | -128 to 127 (or 0 to 255) | %c (as character), %d (as number) |
short |
2 bytes | -32,768 to 32,767 | %hd |
int |
4 bytes | about ±2.1 billion | %d |
long |
8 bytes on 64-bit Linux/macOS, 4 on Windows | at least ±2.1 billion | %ld |
long long |
8 bytes | about ±9.2 × 10¹⁸ | %lld |
float |
4 bytes | about 6–7 significant digits | %f |
double |
8 bytes | about 15–16 significant digits | %f / %lf in scanf |
long double |
10–16 bytes | more precision than double |
%Lf |
The C standard only guarantees minimum sizes, so the exact numbers can differ between platforms. You can ask the compiler with the sizeof operator, which gives a size in bytes as a size_t value (printed with %zu):
#include <stdio.h>
int main(void) {
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("long long: %zu bytes\n", sizeof(long long));
printf("float: %zu bytes\n", sizeof(float));
printf("double: %zu bytes\n", sizeof(double));
return 0;
}char: 1 byte
short: 2 bytes
int: 4 bytes
long: 8 bytes
long long: 8 bytes
float: 4 bytes
double: 8 bytes
(This is the result on the 64-bit Linux machine that runs these playgrounds.)
Signed and unsigned integers
Integer types are signed by default: they can hold negative numbers. Adding the unsigned keyword gives up negatives and doubles the positive range instead.
#include <stdio.h>
#include <limits.h>
int main(void) {
printf("int range: %d to %d\n", INT_MIN, INT_MAX);
printf("unsigned int max: %u\n", UINT_MAX);
printf("long long max: %lld\n", LLONG_MAX);
unsigned int counter = 0;
counter = counter - 1; // wraps around!
printf("0u - 1 = %u\n", counter);
return 0;
}int range: -2147483648 to 2147483647
unsigned int max: 4294967295
long long max: 9223372036854775807
0u - 1 = 4294967295
Unsigned arithmetic wraps around like a car odometer. Signed overflow, on the other hand, is undefined behaviour — never rely on what INT_MAX + 1 does.
Tip: The header
<limits.h>defines constants likeINT_MAX, and<stdint.h>provides exact-width types such asint32_t,uint8_tandint64_twhen you need a precise size.
Floating-point types
float and double store decimal numbers in binary scientific notation. They are approximations — most decimal fractions such as 0.1 cannot be stored exactly.
#include <stdio.h>
int main(void) {
float f = 1.0f / 3.0f;
double d = 1.0 / 3.0;
printf("float: %.20f\n", f);
printf("double: %.20f\n", d);
double sum = 0.1 + 0.2;
printf("0.1 + 0.2 = %.17f\n", sum);
return 0;
}float: 0.33333334326744079590
double: 0.33333333333333331483
0.1 + 0.2 = 0.30000000000000004
float becomes inaccurate after about 7 digits; double holds about 16. In practice, use double unless memory is very tight. Write f after a literal (2.5f) to make it a float; without it, 2.5 is a double.
Characters
A char is a one-byte integer that usually holds an ASCII character code. That means you can do arithmetic on characters:
#include <stdio.h>
int main(void) {
char c = 'a';
printf("%c has code %d\n", c, c);
printf("uppercase: %c\n", c - 32);
printf("'9' - '0' = %d\n", '9' - '0');
return 0;
}a has code 97
uppercase: A
'9' - '0' = 9
The trick '9' - '0' converts a digit character to its numeric value, because digit codes are consecutive.
Booleans and void
- Since C99,
<stdbool.h>providesbool,trueandfalse(see C Booleans). voidmeans "no type": a function that returns nothing isvoid, andvoid *is a generic pointer.
Which type should I pick?
| Need | Use |
|---|---|
| Counting, indexes, ordinary whole numbers | int |
| Sizes and array lengths | size_t |
| Very large whole numbers | long long or int64_t |
| Decimal values, measurements | double |
| Single characters / raw bytes | char / unsigned char |
| Money | an integer number of cents (floating point rounding errors add up!) |
Test yourself
Which operator tells you how many bytes a type uses?
- length
- sizeof
- size()
- bytes
What does an unsigned int holding 0 become after subtracting 1?
- -1
- 0
- The maximum unsigned int value
- The program crashes
Which type is the usual default for decimal numbers in C?
- float
- double
- int
- char