DevAcademia
C++C#CPythonJava
  • C Basics

  • Introduction to C
  • Getting Started with C
  • C Syntax
  • C Output
  • C Comments
  • C Variables
  • C Data Types
  • C Constants
  • C Operators
  • C Booleans
  • C If...Else Statements
  • C Switch Statement
  • C While Loops
  • C For Loops
  • C Break and Continue
  • C Strings
  • C User Input
  • C Memory Address
  • C Pointers
  • C Files
  • C Functions

  • C Functions
  • C Function Parameters
  • C Scope
  • C Function Declaration
  • C Recursion
  • C Math Functions
  • C Structures

  • C Structures
  • C Structs & Pointers
  • C Unions
  • C Enums

  • C Enums
  • C Memory

  • C Allocate Memory
  • C Access Memory
  • C Reallocate Memory
  • C Deallocate Memory
  • C Structs and Memory
  • C Memory Example
  • C Quiz

  • C Quiz
  • C Basics

  • Introduction to C
  • Getting Started with C
  • C Syntax
  • C Output
  • C Comments
  • C Variables
  • C Data Types
  • C Constants
  • C Operators
  • C Booleans
  • C If...Else Statements
  • C Switch Statement
  • C While Loops
  • C For Loops
  • C Break and Continue
  • C Strings
  • C User Input
  • C Memory Address
  • C Pointers
  • C Files
  • C Functions

  • C Functions
  • C Function Parameters
  • C Scope
  • C Function Declaration
  • C Recursion
  • C Math Functions
  • C Structures

  • C Structures
  • C Structs & Pointers
  • C Unions
  • C Enums

  • C Enums
  • C Memory

  • C Allocate Memory
  • C Access Memory
  • C Reallocate Memory
  • C Deallocate Memory
  • C Structs and Memory
  • C Memory Example
  • C Quiz

  • C Quiz

Loading C tutorial…

Loading content
C BasicsTopic 18 of 64
←PreviousPrevNextNext→

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

TypeTypical SizeTypical Range / Precision
signed char1 byte-128 to 127
unsigned char1 byte0 to 255
short2 bytes-32,768 to 32,767 (signed)
unsigned short2 bytes0 to 65,535
int4 bytes-2,147,483,648 to 2,147,483,647 (signed)
unsigned int4 bytes0 to 4,294,967,295
long4 or 8 bytesVaries by platform/ABI
long long8 bytes≈ −9.22e18 to +9.22e18 (signed)
float4 bytes≈6–7 decimal digits; ~1e−38 to ~1e38
double8 bytes≈15–16 decimal digits; ~1e−308 to ~1e308
long double8/12/16 bytesPrecision/range implementation-defined
void* (pointer)4 or 8 bytesN/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.

Example
#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;
}
Output
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 and .

Example
#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;
}
Output
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.

ℹ️ Note: In large programs with thousands of variables, selecting the correct size can save significant memory.

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 like int8_t, int16_t, int32_t, uint64_t.

• For true portability of ranges/precision, consult and rather than hard-coding values.

• Be aware of platform ABIs: on Windows/x64 (LLP64) long is 4 bytes; on Linux/macOS/x64 (LP64) long is 8 bytes.

Test your knowledge: Memory Size in C
Quiz Configuration
4 of 8 questions
Random
Previous allowed
Review enabled
Early close allowed
Estimated time: 8 min
C BasicsTopic 18 of 64
←PreviousPrevNextNext→