Memory Size in C
What is Memory Size?
Every data type in C consumes a specific amount of memory, measured in bytes.
The memory size determines the range of values that type can hold and how much storage is required.
Memory sizes are not fixed across all systems — they may vary depending on compiler and architecture (32-bit vs 64-bit).
Typical Memory Sizes
On many modern systems (but not guaranteed):
• char → 1 byte (by definition sizeof(char) == 1; number of bits is CHAR_BIT)
• short → typically 2 bytes
• int → typically 4 bytes
• long → 4 bytes on Windows/x64 (LLP64), 8 bytes on Linux/macOS/x64 (LP64)
• long long → typically 8 bytes
• float → typically 4 bytes
• double → typically 8 bytes
• long double → implementation-defined (often 8 or 16 bytes; sometimes 12)
• void* (pointer) → 4 bytes on 32-bit, 8 bytes on 64-bit
• size_t → pointer-sized unsigned integer
Type | Typical Size | Typical Range / Precision |
---|---|---|
signed char | 1 byte | -128 to 127 |
unsigned char | 1 byte | 0 to 255 |
short | 2 bytes | -32,768 to 32,767 (signed) |
unsigned short | 2 bytes | 0 to 65,535 |
int | 4 bytes | -2,147,483,648 to 2,147,483,647 (signed) |
unsigned int | 4 bytes | 0 to 4,294,967,295 |
long | 4 or 8 bytes | Varies by platform/ABI |
long long | 8 bytes | ≈ −9.22e18 to +9.22e18 (signed) |
float | 4 bytes | ≈6–7 decimal digits; ~1e−38 to ~1e38 |
double | 8 bytes | ≈15–16 decimal digits; ~1e−308 to ~1e308 |
long double | 8/12/16 bytes | Precision/range implementation-defined |
void* (pointer) | 4 or 8 bytes | N/A (address width, not numeric data) |
The sizeof Operator
To find the exact memory size of a data type on your system, use the sizeof operator.
Example: printf("%zu", sizeof(int)); prints the size of an int in bytes.
This is especially important in cross-platform programs where sizes may vary.
#include <stdio.h>
int main(void) {
printf("char: %zu bytes\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));
printf("long double: %zu bytes\n", sizeof(long double));
printf("void*: %zu bytes\n", sizeof(void*));
printf("size_t: %zu bytes\n", sizeof(size_t));
return 0;
}
Example (LP64, 64-bit Linux/macOS): char: 1 bytes short: 2 bytes int: 4 bytes long: 8 bytes long long: 8 bytes float: 4 bytes double: 8 bytes long double: 16 bytes void*: 8 bytes size_t: 8 bytes
Exact Ranges with <limits.h> and <float.h>
For portable, exact ranges and precision on your platform, query the standard macros from
#include <stdio.h>
#include <limits.h>
#include <float.h>
int main(void) {
printf("INT_MIN=%d, INT_MAX=%d\n", INT_MIN, INT_MAX);
printf("UINT_MAX=%u\n", UINT_MAX);
printf("LONG_MIN=%ld, LONG_MAX=%ld\n", LONG_MIN, LONG_MAX);
printf("ULLONG_MAX=%llu\n", (unsigned long long)ULLONG_MAX);
printf("FLT_DIG=%d, FLT_MIN=%e, FLT_MAX=%e\n", FLT_DIG, FLT_MIN, FLT_MAX);
printf("DBL_DIG=%d, DBL_MIN=%e, DBL_MAX=%e\n", DBL_DIG, DBL_MIN, DBL_MAX);
printf("LDBL_DIG=%d, LDBL_MIN=%Le, LDBL_MAX=%Le\n", LDBL_DIG, LDBL_MIN, LDBL_MAX);
return 0;
}
Sample output varies by platform; these macros report the exact limits and precision of your compiler/ABI.
Why Memory Size Matters
• Efficient memory usage is important in embedded systems with limited RAM.
• Choosing the wrong type can waste memory or cause overflow/underflow bugs.
• Example: Using int for values that always fit in unsigned char wastes 3 bytes per variable on typical systems.
Best Practices
• Use sizeof instead of assuming fixed sizes; remember sizeof(char) is always 1 byte, but the number of bits per byte is CHAR_BIT.
• Avoid using unnecessarily large types; choose the narrowest type that safely holds your values.
• Use unsigned when negative numbers are impossible to increase the positive range.
• In portable programs, prefer fixed-width types from
• For true portability of ranges/precision, consult
• Be aware of platform ABIs: on Windows/x64 (LLP64) long is 4 bytes; on Linux/macOS/x64 (LP64) long is 8 bytes.