strlen() vs. sizeof() in C: Understanding Their Differences and Proper Usage
What is strlen()
In C, strlen() returns the number of characters in a null‑terminated string, excluding the terminating '\0'. It counts alphabetic characters, digits, symbols, and spaces.
This article explains the role of strlen(), sizeof(), and why they behave differently.
- Definition of
strlen() - Definition of
sizeof() - Syntax examples
- Key differences
What is sizeof()
The sizeof operator yields the size, in bytes, of its operand. It is evaluated at compile time for static types and at run time for variable-length arrays. It applies to all C data types, including primitives, pointers, structs, and unions. The result is of type size_t, an unsigned integer type guaranteed to be large enough to hold the size of the largest object.
Because the size of types such as int or double can vary between 32‑bit and 64‑bit systems, sizeof ensures portability.
Key Differences
strlen()measures the number of characters in a string;sizeof()measures the memory occupied by a variable or type.- Only
strlen()considers the string’s terminating'\0';sizeof()ignores content. - The return type of
strlen()issize_t(commonly a 64‑bit unsigned integer on modern systems);sizeof()also returnssize_t. strlen()is a runtime function defined instring.h;sizeofis a compile‑time unary operator.- Only
strlen()operates on character arrays;sizeofworks with any type.
Syntax of strlen()
size_t length = strlen(my_string);
Here, my_string is a character array or pointer to a null‑terminated string.
Example of strlen()
#include <stdio.h>
#include <string.h>
int main() {
char strng[] = "January";
printf("Length of given string is: %zu\n", strlen(strng));
return 0;
}
Output:
Length of given string is: 7
Syntax of sizeof()
1) sizeof(type)
sizeof(type) yields the size of the specified type.
#include <stdio.h>
#define my_sizeof(type) ((char*)(&type+1) - (char*)&type)
int main() {
double x;
printf("%zu", my_sizeof(x));
return 0;
}
Output:
8
2) sizeof(variable)
Pass a variable name to obtain its memory footprint.
#include <stdio.h>
int main() {
printf("sizeof(char) = %zu\n", sizeof(char));
return 0;
}
Output:
sizeof(char) = 1
3) sizeof(expression)
Evaluate an expression’s type size without computing its value.
#include <stdio.h>
int main() {
char p = 'S';
double q = 4.65;
printf("Size of variable p : %zu\n", sizeof(p));
printf("Size of an expression : %zu\n", sizeof(p+q));
int a = (int)(p+q);
printf("Size of explicitly converted expression : %zu\n", sizeof(a));
return 0;
}
Output:
Size of variable p : 1 Size of an expression : 8 Size of explicitly converted expression : 4
Visual Comparison

Below is a side‑by‑side summary of the two constructs.
| Feature | strlen() | sizeof() |
|---|---|---|
| Header file | string.h | No header required (built‑in operator) |
| Purpose | Compute string length (excluding null) | Determine memory size of a type or variable |
| Return type | size_t | size_t |
| Evaluation time | Runtime (may traverse the string) | Compile‑time (except for VLA) |
| Applicable to | Only character arrays or pointers to null‑terminated strings | All data types and expressions |
When to Use Which
- Use
strlen()when you need the logical length of a string, such as for buffer allocation, substring extraction, or input validation. - Use
sizeof()when you need the physical size of a data type or variable, for example when copying memory withmemcpyor allocating a block of memory for an array of structs. - Remember that
sizeof(char)is always 1 byte, which is the base unit of memory in C. - For string literals,
sizeof("hello")yields 6 (five characters plus the null terminator), whilestrlen("hello")yields 5.
Adhering to these guidelines ensures efficient, portable, and maintainable C code.
C Language
- Structures vs. Unions in C: A Practical Guide
- C vs. C++: Key Differences & When to Choose Each
- C vs Java: A Comprehensive Comparison of Features, History, and Applications
- Python vs JavaScript: Key Differences, Features, and When to Choose Each
- Python vs Ruby: A Comprehensive Comparison of Features, Advantages, and Use Cases
- Joining vs Welding: Key Differences Explained
- Welding vs Brazing: Key Differences Explained for Better Metal Joining
- Up vs Down Milling: Key Differences Explained
- Soldering vs. Brazing: Understanding the Critical Differences
- Key Differences Between Ships and Boats Explained