New: 77 pattern programs with tested code in C, C++, Java and Python Explore patterns

C Program to Display the Size of Different Data Types

Read summary with AI tools
Table of contents
  1. C program to display the size of different data types
  2. How the program works
  3. Sizes on different platforms
  4. Frequently asked questions

Every variable in C occupies a fixed number of bytes in memory, and the sizeof operator tells you exactly how many. This program prints the size of all the basic data types in C, and the table below shows why the answer is not the same on every computer.

C program to display the size of different data types

#include <stdio.h>

int main(void) {
    printf("Size of char        : %zu byte\n", sizeof(char));
    printf("Size of short       : %zu bytes\n", sizeof(short));
    printf("Size of int         : %zu bytes\n", sizeof(int));
    printf("Size of long        : %zu bytes\n", sizeof(long));
    printf("Size of long long   : %zu bytes\n", sizeof(long long));
    printf("Size of float       : %zu bytes\n", sizeof(float));
    printf("Size of double      : %zu bytes\n", sizeof(double));
    printf("Size of long double : %zu bytes\n", sizeof(long double));
    printf("Size of a pointer   : %zu bytes\n", sizeof(void *));
    return 0;
}

Output (64-bit macOS on Apple Silicon):

Size of char        : 1 byte
Size of short       : 2 bytes
Size of int         : 4 bytes
Size of long        : 8 bytes
Size of long long   : 8 bytes
Size of float       : 4 bytes
Size of double      : 8 bytes
Size of long double : 8 bytes
Size of a pointer   : 8 bytes
Output of the C program that prints the size of char, short, int, long, float, double and a pointer
Real output of the program on a 64-bit Apple Silicon Mac.

How the program works

  • sizeof(type) is evaluated by the compiler and gives the size of the type in bytes.
  • The result has the type size_t, so it is printed with the %zu format specifier.
  • sizeof(char) is always 1 by definition; the other sizes depend on the platform.

Sizes on different platforms

The C standard only sets minimum sizes, so compilers choose sizes that suit each platform. These values come from the compiler for each target:

TypeLinux / Intel Mac (64-bit)Apple Silicon MacWindows 64-bitLinux 32-bit
char1111
short2222
int4444
long8844
long long8888
float4444
double8888
long double1688 (MSVC) / 16 (MinGW)12
pointer8884

The most common surprise is long: it is 8 bytes on 64-bit Linux and macOS but only 4 bytes on 64-bit Windows. If you need an exact size, use the fixed-width types from <stdint.h> such as int32_t and int64_t.

Frequently asked questions

Why is %zu used instead of %d?

sizeof returns a size_t, which is an unsigned type. %zu is the correct format specifier for it; %d may print wrong values or trigger compiler warnings.

Is the size of int always 4 bytes?

No. The standard only guarantees at least 16 bits. On today’s desktop systems it is almost always 4 bytes, but some embedded systems use 2 bytes.

How do I find the size of a variable?

Use sizeof on the variable itself, for example sizeof(x), or on an array to get its total size in bytes.

Authored by

Ghanendra Yadav runs Pattern Programs from Bangalore, India. The site started in 2020 to help students practise loops and logic through pattern programs in C, C++, Java and Python. Every program published here is compiled and run before it goes live, and the output shown is the real output.

Latest articles

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.