Shiklo Tools
PDF40
Image20
Calculators & Converters25
Text & Writing13
Developer26
Video14
Audio14
Document14
Data & Spreadsheet12
Web & SEO9
OCR & Scanning5
Archive & File12
Security & Privacy23
Health & Medical29
QR & Barcode5

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

TypeHoldsFormat
charone character (a small number)%c
shortsmall whole numbers%hd
intwhole numbers%d
longwhole numbers, at least as big as int%ld
long longbig whole numbers%lld
floatnumbers with a decimal point, about 7 digits%f
doublenumbers 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;
}
Output
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;
}
Output
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;
}
Output
A is number 65
The next letter is B

Practice exercises

  1. Print the sizes of unsigned int, unsigned char and long double.
  2. Store the population of India (about 1400000000) and the population of the world (about 8000000000) in variables of fitting types and print them.
  3. Print the characters for the numbers 72, 105 and 33 using %c.