Data Types and sizeof

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

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

  1. char: one byte, enough for one character.
  2. count is created on the stack with the value 100000.
  3. long long holds very large whole numbers. 9 billion does not fit in an int.
  4. float: a decimal number in 4 bytes, about 7 digits of precision.
  5. double: 8 bytes, about 15 digits. Note 0.1 cannot be stored exactly in either.
  6. unsigned: no negative numbers, so the positive range doubles.
  7. sizeof gives a type's size in bytes: 4 for int here.
  8. bytes_double is created on the stack with the value 8.
  9. It prints 4 8 9000000000 4000000000.
  10. The method returns 0.
  11. int is 4 bytes and double is 8. Types decide size, range and precision.

Common mistakes

Best practices

Key takeaways

Check yourself

Which holds 5 billion?

Answer: long long. It needs more than 4 bytes.

unsigned int can store?

Answer: 0 and above only. No sign bit.

·

More in C Basics