C Data Types
A variable's data type decides what values it can hold, how much memory it uses and which operations make sense. C has a small set of basic types; everything else is built from them.
The basic types
| Type | Holds | Format |
|---|---|---|
char | one character (a small number) | %c |
short | small whole numbers | %hd |
int | whole numbers | %d |
long | whole numbers, at least as big as int | %ld |
long long | big whole numbers | %lld |
float | numbers with a decimal point, about 7 digits | %f |
double | numbers with a decimal point, about 15 digits | %f |
Use int for whole numbers and double for decimals unless you have a reason not to. float saves memory but is less precise.
How big is each type? sizeof
The size of a type depends on the computer and the compiler. The sizeof operator gives the size in bytes. Its result has the type size_t, which is 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: 4 bytes long long: 8 bytes float: 4 bytes double: 8 bytes
These are the sizes in our online compiler, which works like a 32-bit computer. On a 64-bit Linux computer long usually has 8 bytes. That is why you should use sizeof instead of assuming a size.
Range and overflow
A 4-byte int holds values from -2147483648 to 2147483647. The header limits.h names these limits INT_MIN and INT_MAX. Going past the limit is called overflow. For signed types like int it is a bug with no defined result, so choose a type that is big enough: long long goes up to about 9 × 10^18.
#include <limits.h>
#include <stdio.h>
int main(void) {
printf("int: %d to %d\n", INT_MIN, INT_MAX);
printf("long long: up to %lld\n", LLONG_MAX);
long long big = 3000000000LL; // too big for an int
printf("big = %lld\n", big);
return 0;
}int: -2147483648 to 2147483647 long long: up to 9223372036854775807 big = 3000000000
signed and unsigned
Whole-number types are signed by default: they hold negative and positive values. unsigned int holds only zero and positive values, but up to about twice as high (4294967295 for 4 bytes). Mixing signed and unsigned values in comparisons gives surprising results, so use unsigned only when you need it.
char is a small number
A char stores a character as its number in the ASCII table: 'A' is 65, 'a' is 97, '0' is 48. You can print it either way:
#include <stdio.h>
int main(void) {
char letter = 'A';
printf("%c is number %d\n", letter, letter);
printf("The next letter is %c\n", letter + 1);
return 0;
}A is number 65 The next letter is B
Practice exercises
- Print the sizes of
unsigned int,unsigned charandlong double. - Store the population of India (about 1400000000) and the population of the world (about 8000000000) in variables of fitting types and print them.
- Print the characters for the numbers 72, 105 and 33 using
%c.