0
0
CHow-ToBeginner · 3 min read

How to Find Size of Data Type in C: Simple Guide

In C, you can find the size of any data type using the sizeof operator. For example, sizeof(int) returns the size in bytes of the int type on your system.
📐

Syntax

The sizeof operator returns the size in bytes of a data type or variable.

  • sizeof(type): Returns size of the specified data type.
  • sizeof expression: Returns size of the variable or expression.

The result is of type size_t, an unsigned integer type.

c
size_t size = sizeof(type_or_variable);
💻

Example

This example shows how to print the size of common data types using sizeof.

c
#include <stdio.h>

int main() {
    printf("Size of char: %zu byte(s)\n", sizeof(char));
    printf("Size of int: %zu byte(s)\n", sizeof(int));
    printf("Size of float: %zu byte(s)\n", sizeof(float));
    printf("Size of double: %zu byte(s)\n", sizeof(double));
    return 0;
}
Output
Size of char: 1 byte(s) Size of int: 4 byte(s) Size of float: 4 byte(s) Size of double: 8 byte(s)
⚠️

Common Pitfalls

  • Using sizeof on a pointer returns the size of the pointer itself, not the data it points to.
  • Parentheses around variables when using sizeof are optional but recommended for clarity.
  • Sizes can vary between systems and compilers, so sizeof helps write portable code.
c
#include <stdio.h>

int main() {
    int *ptr;
    printf("Size of pointer: %zu byte(s)\n", sizeof(ptr)); // size of pointer, not int
    printf("Size of int: %zu byte(s)\n", sizeof(*ptr));   // size of int, pointed data
    return 0;
}
Output
Size of pointer: 8 byte(s) Size of int: 4 byte(s)
📊

Quick Reference

Here is a quick reference for typical sizes on a 64-bit system, but remember these can vary:

Data TypeTypical Size (bytes)
char1
int4
float4
double8
pointer8

Key Takeaways

Use the sizeof operator to get the size in bytes of any data type or variable in C.
sizeof returns the size of the type or variable as a size_t value, which is an unsigned integer.
Be careful: sizeof(pointer) gives the pointer size, not the size of the data it points to.
Sizes returned by sizeof can vary by system and compiler, so use it for portable code.
Parentheses around the type in sizeof are required, but optional around variables.