Skip to content
  • Data Types & Sizes · C

Data Types & Sizes

LearnViaNotes @learnvianotes· 3 min read

C has a handful of built-in arithmetic types whose sizes are implementation-defined — the standard only guarantees minimum ranges. Always measure with sizeof.

The basic types

TypeGuaranteed minimumTypical size (64-bit Linux/macOS)Format
char8 bits1 byte%c / %d
short16 bits2 bytes%hd
int16 bits4 bytes%d
long32 bits8 bytes (4 on 64-bit Windows)%ld
long long64 bits8 bytes%lld
float~6 decimal digits4 bytes (IEEE 754 single)%f
double~10 digits (15 in practice)8 bytes (IEEE 754 double)%f / %lf
long double≥ double16 bytes x86-64 Linux, 8 on Apple arm64%Lf
_Bool / boolholds 0 or 11 byte%d
voidno value——

Measure with sizeof

C — output from a 64-bit Apple arm64 machine
#include <stdio.h>
#include <stdbool.h>

int main(void) {
    printf("char        %zu\n", sizeof(char));
    printf("short       %zu\n", sizeof(short));
    printf("int         %zu\n", sizeof(int));
    printf("long        %zu\n", sizeof(long));
    printf("long long   %zu\n", sizeof(long long));
    printf("float       %zu\n", sizeof(float));
    printf("double      %zu\n", sizeof(double));
    printf("long double %zu\n", sizeof(long double));
    printf("bool        %zu\n", sizeof(bool));
    printf("pointer     %zu\n", sizeof(void *));
    return 0;
}
Output
char        1
short       2
int         4
long        8
long long   8
float       4
double      8
long double 8
bool        1
pointer     8

sizeof is an operator, not a function: it is evaluated at compile time (except for VLAs), and its result has type size_t — print it with %zu. Parentheses are only needed around a type: sizeof x works for a variable.

signed and unsigned

TypeRange with 32-bit int
int (= signed int)−2 147 483 648 … 2 147 483 647
unsigned int0 … 4 294 967 295
signed char−128 … 127
unsigned char0 … 255 (the type for raw bytes)
plain chareither — signed on x86 and Apple arm64, unsigned on Linux ARM
C
#include <stdio.h>

int main(void) {
    unsigned int u = 0;
    u = u - 1;                    /* unsigned arithmetic wraps modulo 2^32 — well defined */
    printf("%u\n", u);

    unsigned char byte = 250;
    byte += 10;                   /* 260 mod 256 */
    printf("%d\n", byte);
    return 0;
}
Output
4294967295
4

Floating point

C
#include <stdio.h>

int main(void) {
    double a = 0.1 + 0.2;
    printf("%.17f\n", a);
    printf("%s\n", a == 0.3 ? "equal" : "not equal");

    float f = 16777217.0f;        /* 2^24 + 1 cannot be stored in a float */
    printf("%.1f\n", f);
    printf("%f %f\n", 7 / 2.0, (double)(7 / 2));
    return 0;
}
Output
0.30000000000000004
not equal
16777216.0
3.500000 3.000000

Characters are small integers

C
#include <stdio.h>

int main(void) {
    char c = 'A';
    printf("%c %d %c %d\n", c, c, c + 2, '0');
    printf("digit value: %d\n", '7' - '0');
    printf("sizeof 'A' = %zu\n", sizeof 'A');   /* a char literal has type int in C */
    return 0;
}
Output
A 65 C 48
digit value: 7
sizeof 'A' = 4

What is the size of int in C?

Is char signed or unsigned?

What does sizeof('A') return in C and in C++?

Free for students and teachers

Write notes like this one

Cards for every idea, quizzes and flashcards, read aloud — on the web and your phone.

Something wrong with this note? Open it to report