C Data Types & Sizes Guide – int, long, struct, sizeof

1. Introduction

The C language is a programming language used in a wide range of applications, including embedded systems and system programming. Among these, “data types” and “sizes” have a major impact on a program’s memory usage and performance. Especially when running programs in different environments (32bit / 64bit, Windows / Linux, etc.), the size of data types can vary, making it important to choose the correct type. In this article, we will explain the sizes of basic C data types, differences across environments, and key points for selecting appropriate types. We also introduce practical information such as how to use the sizeof operator and how to leverage fixed-width integer types (stdint.h). This will deepen your understanding of C types and enable more efficient programming.

2. Basic Data Types and Sizes in C

2.1 Integer Types Size List

Data Type32‑bit environment64‑bit environmentMinimum size
char1 byte1 byte1 byte
short2 bytes2 bytes2 bytes
int4 bytes4 bytes2 bytes
long4 bytes8 bytes4 bytes
long long8 bytes8 bytes8 bytes

2.2 Floating‑Point Types Size List

Data Type32‑bit environment64‑bit environment
float4 bytes4 bytes
double8 bytes8 bytes
long double8–16 bytes16 bytes
侍エンジニア塾

3. Platform-Dependent Data Type Sizes

3.1 Differences in Size by OS and Compiler

Data TypeWindows (LLP64)Linux (LP64)32-bit Environment
int4 bytes4 bytes4 bytes
long4 bytes8 bytes4 bytes
long long8 bytes8 bytes8 bytes

4. Verify data type sizes with the sizeof operator

4.1 Basics of sizeof

#include <stdio.h>

int main() {
    printf("Size of int: %zu bytesn", sizeof(int));
    printf("Size of double: %zu bytesn", sizeof(double));
    return 0;
}

5. Fixed-width integer types (using stdint.h)

#include <stdio.h>
#include <stdint.h>

int main() {
    int8_t a = 100;
    uint16_t b = 50000;
    int32_t c = -123456;
    uint64_t d = 1000000000ULL;

    printf("Size of int8_t: %zu bytesn", sizeof(a));
    printf("Size of uint16_t: %zu bytesn", sizeof(b));
    printf("Size of int32_t: %zu bytesn", sizeof(c));
    printf("Size of uint64_t: %zu bytesn", sizeof(d));

    return 0;
}

6. Struct Size and Alignment

6.1 Struct Memory Layout

#include <stdio.h>

struct example {
    char a;
    int b;
    double c;
};

int main() {
    printf("Size of struct: %zu bytesn", sizeof(struct example));
    return 0;
}

7. Choosing Appropriate Data Types

7.1 Choosing Integer Types

Data TypeSizeRange
int8_t1B-128 – 127
uint8_t1B0 – 255
int32_t4Bapproximately -2.1B – 2.1B
uint32_t4B0 – 4.2B

8. FAQ (Frequently Asked Questions)

Q1: Why is the result of sizeof a unsigned int?

A: In C, the return value of sizeof is of type size_t, which is an unsigned integer type. Because a negative size cannot exist, it is defined as a safe type.
size_t size = sizeof(int);
printf("Size of int: %zu bytesn", size);

9. Summary

9.1 Key Points of This Article

Data type sizes are platform-dependentUse sizeof to check the actual sizeLeverage fixed-width integer typesStruct sizes are affected by memory alignmentChoosing the appropriate data type
侍エンジニア塾