Question
I am confused about size_t in C. I know that it is the type returned by the sizeof operator, but what exactly is it? Is it a built-in data type?
For example, consider this loop:
for (i = 0; i < some_size; i++)
Should i be declared as int i; or size_t i;?
Short Answer
By the end of this page, you will understand what size_t is in C, why sizeof returns it, and when it is better to use size_t instead of int. You will also learn how size_t behaves in loops, array sizes, and memory-related code, plus the common mistakes beginners make when mixing signed and unsigned integer types.
Concept
size_t is an integer type used in C to represent sizes and counts of objects in memory. It is not a keyword like int or char, but a type alias provided by the standard library.
In practice:
sizeofreturns a value of typesize_t- Functions related to memory and arrays often use
size_t - It is designed to be able to store the size of the largest object the system can handle
A common definition looks conceptually like this:
typedef /* some unsigned integer type */ size_t;
The exact underlying type depends on the platform and compiler. On one system it might be unsigned int, and on another it might be unsigned long or something else.
Why does C use size_t?
C needs a type specifically for sizes because:
- sizes cannot be negative
- memory sizes may be larger than what a plain
intcan hold - the best integer type depends on the machine architecture
That is why exists: it gives C a portable way to talk about memory sizes and element counts.
Mental Model
Think of size_t as the language's official measuring tape for memory.
intis a general-purpose number typesize_tis the type C uses when measuring how big something is
If you ask, "How many bytes does this object use?" C answers with a size_t.
If you ask, "How many elements are in this memory block?" size_t is usually the right type.
So if int is a regular calculator number, size_t is the ruler used for memory-related measurements.
Syntax and Examples
Basic syntax
You commonly see size_t after including a standard header such as:
#include <stddef.h>
It is also made available by other headers like stdio.h, stdlib.h, and string.h in many common situations.
Declaring a size_t
#include <stddef.h>
size_t count = 25;
Using sizeof
#include <stdio.h>
int main(void) {
int numbers[5];
size_t bytes = sizeof(numbers);
size_t elements = sizeof(numbers) / sizeof(numbers[]);
(, bytes);
(, elements);
;
}
Step by Step Execution
Consider this example:
#include <stdio.h>
#include <stddef.h>
int main(void) {
int values[] = {5, 8, 13};
size_t count = sizeof(values) / sizeof(values[0]);
for (size_t i = 0; i < count; i++) {
printf("i = %zu, value = %d\n", i, values[i]);
}
return 0;
}
Step by step
valuesis an array with 3 integers.sizeof(values)returns the total byte size of the whole array.- If
intis 4 bytes, this would be12.
- If
sizeof(values[0])returns the byte size of one element.- That would be
4.
- That would be
Real World Use Cases
size_t appears frequently in real C programs because many tasks involve sizes, lengths, and indexes.
Common use cases
- Array lengths
- counting how many elements are in an array
- Memory allocation
- passing byte counts to
malloc,calloc, andrealloc
- passing byte counts to
- String and buffer handling
- working with lengths returned by functions like
strlen
- working with lengths returned by functions like
- File and network buffers
- tracking how many bytes were read or written
- Looping over collections
- iterating through arrays or buffers safely
Example: dynamic memory
#include <stdlib.h>
#include <stddef.h>
int main(void) {
size_t count = 100;
int *data = malloc(count * sizeof());
(data == ) {
;
}
(data);
;
}
Real Codebase Usage
In real codebases, developers use size_t anywhere a value represents a size, length, capacity, or non-negative index.
Common patterns
Validation before allocation
size_t count = get_count();
if (count == 0) {
return;
}
This makes intent clear: count is a size, not a general signed number.
Matching library function types
size_t len = strlen(input);
if (len >= buffer_size) {
return -1;
}
Using size_t avoids unnecessary conversions because standard library functions already return it.
Buffer processing loops
for (size_t i = 0; i < buffer_len; i++) {
buffer[i] = 0;
}
This is very common in parsing, file processing, and systems code.
Capacity and count fields in structs
typedef
*items;
count;
capacity;
} IntArray;
Common Mistakes
1. Using int for values that come from sizeof
Broken example:
int n = sizeof(double) * 100;
This may work for small values, but int is not the type returned by sizeof.
Better:
size_t n = sizeof(double) * 100;
2. Comparing int with size_t
Broken example:
size_t count = 10;
int i;
for (i = 0; i < count; i++) {
/* ... */
}
This often produces a signed/unsigned comparison warning.
Better:
size_t count = 10;
( i = ; i < count; i++) {
}
Comparisons
size_t vs int
| Feature | size_t | int |
|---|---|---|
| Purpose | Sizes, lengths, counts, indexes | General integer values |
| Signed or unsigned | Unsigned | Usually signed |
Returned by sizeof | Yes | No |
| Can represent negative values | No | Yes |
| Good for array sizes | Yes | Sometimes, but less ideal |
| Good for arithmetic that may go below zero | No | Yes |
When to choose each
Cheat Sheet
Quick reference
#include <stddef.h>
What size_t is
- An unsigned integer type
- Used for sizes and counts
- Returned by
sizeof - Often returned by library functions like
strlen
Basic usage
size_t n = sizeof(array);
size_t count = sizeof(array) / sizeof(array[0]);
Loop pattern
for (size_t i = 0; i < count; i++) {
/* ... */
}
Print with printf
printf("%zu\n", n);
Use size_t when
FAQ
Is size_t built into the C language?
size_t is a standard type name provided by the C library headers, not a keyword like int.
Why does sizeof return size_t?
Because sizeof reports the size of an object in bytes, and C uses size_t specifically for object sizes.
Should I always use size_t for loops?
Not always. Use it when looping over sizes, array indexes, or buffer lengths. If the loop variable needs to become negative, use a signed type instead.
Is size_t always the same as unsigned int?
No. Its exact underlying type depends on the platform and compiler.
Can size_t hold negative numbers?
No. It is an unsigned type.
What header defines size_t?
<stddef.h> is the standard header most directly associated with size_t, though other standard headers may also provide it.
Mini Project
Description
Build a small C program that analyzes an integer array using size_t for lengths and loop indexes. This project demonstrates the most common real use of size_t: working safely with array sizes and indexes that come from sizeof.
Goal
Create a program that calculates the number of elements in an array, prints each element, and computes the sum using size_t correctly.
Requirements
- Define an integer array with several values.
- Compute the number of elements using
sizeof. - Use a
forloop with asize_tindex to print every element. - Calculate the total sum of the array values.
- Print the element count with the correct
printfformat specifier.
Keep learning
Related questions
Building More Fault-Tolerant Embedded C++ Applications for Radiation-Prone ARM Systems
Learn practical C++ and compile-time techniques to reduce soft-error damage in embedded ARM systems exposed to radiation.
C printf Format Specifier for bool: How to Print Boolean Values
Learn how to print bool values in C with printf, why no %b/%B specifier exists, and the common patterns to print true/false or 0/1.
Calling C or C++ from Python: Building Python Bindings
Learn the quickest ways to call C or C++ from Python, including ctypes, C extensions, Cython, and binding tools with practical examples.