🚀Beginner

Find Size of int, float, double, char

Find size of data types using sizeof

The sizeof operator is a compile-time unary operator that returns the size (in bytes) of a data type or variable. It helps understand memory allocation and is essential for dynamic memory management.

📚 What You'll Learn

  • Using the sizeof operator
  • Memory sizes of different data types
  • Difference between 32-bit and 64-bit systems

📝 Syntax

sizeof(data_type) or sizeof(variable)

Returns: size_t (unsigned integer)

C Program to Find Size of Data Types

size_of_types.c
C
1#include <stdio.h>
2
3int main() {
4 // Character type
5 printf("Size of char: %lu byte\n", sizeof(char));
6
7 // Integer types
8 printf("Size of short: %lu bytes\n", sizeof(short));
9 printf("Size of int: %lu bytes\n", sizeof(int));
10 printf("Size of long: %lu bytes\n", sizeof(long));
11 printf("Size of long long: %lu bytes\n", sizeof(long long));
12
13 // Floating-point types
14 printf("Size of float: %lu bytes\n", sizeof(float));
15 printf("Size of double: %lu bytes\n", sizeof(double));
16
17 // Pointer size (system dependent)
18 printf("Size of pointer: %lu bytes\n", sizeof(int*));
19
20 return 0;
21}
Output

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 pointer: 8 bytes

📖 Code Explanation

CodeExplanation
sizeof(char)Returns size of char type (always 1 byte)
sizeof(int)Returns size of int (typically 4 bytes)
%luFormat specifier for unsigned long (size_t)
sizeof(int*)Size of a pointer (depends on system architecture)

📊 Complete Data Types Reference

TypeSize (bytes)RangeFormat
char1-128 to 127%c
short2-32,768 to 32,767%hd
int4-2.1B to 2.1B%d
long8±9.2 quintillion%ld
float4~6 digits precision%f
double8~15 digits precision%lf

⚠️ System Architecture Differences

32-bit System:

Pointer size = 4 bytes

long = 4 bytes

64-bit System:

Pointer size = 8 bytes

long = 8 bytes

🎯 Key Takeaways

char is always 1 byte
sizeof is evaluated at compile time
Pointer size depends on architecture
Use %lu to print sizeof result

Want to Learn More?

Explore our comprehensive tutorials for in-depth explanations of C programming concepts.

Browse Tutorials
Back to All Examples