char, int, long long, float, double and unsigned: what each holds and how many bytes sizeof says it uses.
C · beginner · 7 min · 11 steps · free
What you'll learn
Pick a type for a value
Use sizeof
Know float is less precise than double
types_sizeof.c
#include <stdio.h>
int main(void) {
char c = 'z';
int count = 100000;
long long big = 9000000000LL;
float ratio = 0.1f;
double precise = 0.1;
unsigned int u = 4000000000u;
int bytes_int = (int)sizeof(int);
int bytes_double = (int)sizeof(double);
printf("%d %d %lld %u\n", bytes_int, bytes_double, big, u);
return 0;
}
Step by step
char: one byte, enough for one character.
count is created on the stack with the value 100000.
long long holds very large whole numbers. 9 billion does not fit in an int.
float: a decimal number in 4 bytes, about 7 digits of precision.
double: 8 bytes, about 15 digits. Note 0.1 cannot be stored exactly in either.
unsigned: no negative numbers, so the positive range doubles.
sizeof gives a type's size in bytes: 4 for int here.
bytes_double is created on the stack with the value 8.
It prints 4 8 9000000000 4000000000.
The method returns 0.
int is 4 bytes and double is 8. Types decide size, range and precision.