What is size_t in C?

In this tutorial, you will learn the fundamentals of size_t in C, including what it is, why it exists, and how and when to use it. You will also explore its role in memory management, array indexing, and standard library functions.

What is size_t in C?

In C, size_t is an unsigned integer type that represents the size of objects in memory. The standard library headers, such as <stddef.h> and <stdio.h>, define this type. It commonly appears as the return type of functions like sizeof, strlen(), and memory allocation functions like malloc().

Syntax:

size_t variableName;

Where is size_t Defined?

size_t is a typedef, so it serves as a shorthand for an underlying unsigned integer type. The compiler defines it like this (in simplified form):

typedef unsigned int size_t; // On 32-bit systems
typedef unsigned long long size_t; // On 64-bit systems

You don’t need to define it yourself. Just include the appropriate header:

advertisement
#include <stddef.h> // or
#include <stdio.h>  // or
#include <stdlib.h>

Why Use size_t?

  • Portability: size_t works on any system. It adjusts to the computer’s architecture, so your code doesn’t need changes to run elsewhere.
  • Safety: size_t is an unsigned type. This means it can’t be negative, which makes sense because sizes and array positions are never negative.
  • Standard Compliance: Many standard functions use size_t. These include sizeof, strlen, and memory functions like malloc(). Using size_t keeps your code in line with common practices.

Common Use Cases of size_t

Here are the most common scenarios where you’ll encounter or use size_t:

Example 1: Return Type of sizeof Operator

#include <stdio.h>
 
int main()
{
    size_t size = sizeof(int);
    printf("Size of int: %zu bytes\n", size);
    return 0;
}

Output:

Note: Join free Sanfoundry classes at Telegram or Youtube
Size of int: 4 bytes

This program shows how to find the size of an int in C. It uses the sizeof operator and stores the result in a size_t variable. Then it prints the size using printf() with the %zu format. This tells us how many bytes an int takes in memory.

Example 2: Array Indexing and Loop Counters

Since arrays are memory blocks, using size_t for indexing is safer and matches the way C internally handles sizes.

#include <stdio.h>
 
int main() {
    int marks[] = {85, 90, 75, 88, 92};
    size_t count = sizeof(marks) / sizeof(marks[0]);
 
    for (size_t index = 0; index < count; ++index) {
        printf("marks[%zu] = %d\n", index, marks[index]);
    }
    return 0;
}

Output:

marks[0] = 85
marks[1] = 90
marks[2] = 75
marks[3] = 88
marks[4] = 92

This program prints the elements of an array using a loop. It creates an int array called marks with five values. Then it uses sizeof to find how many elements are in the array and stores that in a size_t variable called count. A for loop runs through the array, and printf() prints each index and value using the correct format for size_t.

Example 3: Functions Like strlen(), malloc(), etc.

advertisement
#include <stdio.h>
#include <string.h>
 
int main() {
    const char* quote = "Sanfoundry Rocks!";
    size_t length = strlen(quote); // Calculates number of characters
    printf("Length: %zu\n", length);
    return 0;
}

Output:

Length: 17

This program finds the length of a string. It stores the text “Sanfoundry Rocks!” in a const char* called quote. Then it uses strlen() to count the number of characters and saves the result in a size_t variable named length. Finally, it prints the length using printf() with the %zu format.

Common Pitfalls When Using size_t in C

1. Mismatched Format Specifier in printf

Problem: Using %d instead of %zu when printing a size_t variable.

size_t len = 10;
printf("%d", len);  // Wrong!

Fix:

printf("%zu", len);  // Correct!

2. Signed vs Unsigned Comparison

Problem: Comparing size_t (unsigned) with int (signed) can lead to unexpected behavior.

int i = -1;
size_t len = 5;
 
if (i < len) {  // i is promoted to unsigned → becomes large number
    printf("Inside if block\n");
}

Fix: Make sure both variables are of the same type.

size_t i = 0;

3. Negative Indexing Confusion

Problem: size_t can’t store negative numbers, so this won’t work:

for (size_t i = len - 1; i >= 0; i--) {  // Infinite loop!
    printf("%zu\n", i);
}

Fix: Use a signed type like int or handle the loop differently.

for (int i = len - 1; i >= 0; i--) {
    printf("%d\n", i);
}

4. Don’t Assume size_t Is Always 4 Bytes: On 64-bit systems, size_t is usually 8 bytes. If you assume it’s always 4, your code might not work correctly on other systems.

5. Be Careful with Pointer Math: Mixing int and size_t in pointer calculations can lead to warnings or bugs. Always use the right types to keep your code safe and clear.

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

If you wish to look at all C Tutorials, go to C Tutorials.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.