Table of Contents
The strlen()
is a predefined function in C programming languages. It returns the number of characters in the string. When we pass the pointer of the string to the function, it goes through the complete string, until it finds the NULL-terminated character.
string.h
header file before using this function in your C code.Example Code for strlen():
#include <stdio.h> #include <string.h> int main() { char str1[] = "CSEstack Programming"; printf("Length of string is %d.", strlen(str1)); return 0; }
Output:
Length of string is 20.
From the above output of the program, you can see, the function also counts the space and other special characters in the string.
Use: The strlen()
function is very useful for solving many of the coding questions. For example: check if the two given strings are anagram, where you have to find the length of both strings. If the two strings have different lengths, they can not be anagrams.
The sizeof()
is one of the most useful operators in C programming. As it takes one input operand, it is the unary operator.
There are different data types in C programming. You can pass any data type object as an operand including primitive types (such as int
, float
, char
…) and compound types objects (such as structure, union)
Return Type: sizeof()
returns the total size assigned to the given operand. The output data type is represented as size_t
.
If we run the same program as above to print the size of the same string, here is an output.
#include <stdio.h> int main() { char str1[] = "CSEstack Programming"; printf("Size string is %d.", sizeof(str1)); return 0; }
Output:
Size of string is 21.
Note: Here we are considering the size of the character is one byte. It can be different based on the system and compiler designing.
You can also pass the data type to the sizeof
operator.
#include <stdio.h> int main() { printf("Size of integere is %d.", sizeof(int)); printf("Size of float is %d.", sizeof(float)); printf("Size of character is %d.", sizeof(char)); return 0; }
Remember, the output of the above program may be different based on the system and compiler that you are using.
If you are not sure about the size of the character, you can divide the size of the string (or array) by the character. You will get the length of the string.
#include <stdio.h> #include <string.h> int main() { char str1[] = "CSEstack Programming"; printf("Size of string is %d.", sizeof(str1)/sizeof(char)); return 0; }
In the case of an integer array, you need to divide the size of the array by the size of integer to get the number of elements in the array.
#include <stdio.h> #include <string.h> int main() { char arr[] = [24,5,6,7,3,54,7]; printf("Number of elements in the array:", sizeof(arr)/sizeof(int)); return 0; }
Output:
Number of elements on the array: 7
When you declare a string, ‘\0’ NULL-terminated character will be added as the last character as a delimiter to represent the end of the string.
Comparing strlen and sizeof:
Function strlen()
represents the number of characters in the string excluding delimiter '\0'
. Whereas, operator sizeof()
returns the memory size allocated to save the string. It also counts the memory size required to save the character ‘\0'
.
For an empty string, the length of the string is zero but the size of the string is one.
#include <stdio.h> #include <string.h> int main() { char str1[] = ""; printf("Length of the string is %d.", strlen(str1)); printf("\nSize of the string is %d.", sizeof(str1)); return 0; }
Output:
Length of the string is 0.
Size of the string is 1.
So, the size of the string will be one higher than the length of the string.
Usage of sizeof() Operator in C Programming:
The sizeof()
operator is mostly used for memory management calls like malloc()
, memset()
, memcpy()
.
Let’s compare these two built-in functions in C programming.
strlen()
returns the length of the string stored in the array, however, sizeof()
returns the total allocated size assigned to the array.strlen()
and sizeof()
, the size of the string will be one higher than the length of the string. (Considering the size of the character in one byte.)strlen()
, we can use the function sizeof()
with any datatypes.strlen()
is the integer, whereas the return type of the sizeof()
operator is size_t
.This is all about the difference between strlen()
and sizeof()
in C / C++ programming. If you have any queries, let’s discuss this in the comment section.
My search (Duckduckgo) led me to believe this page talked about
sizeof()
andstrlen()
relative to Unicode.If you want to malloc/calloc memory to store an array of char you want
sizeof()
.But when you want to copy the data in you want
strncpy()
so you want a count of chars sostrlen()
. I think. It’s tricky, there’s a right way to do it but I don’t do it often so I forgot.Thanks, Alan for sharing your thought. This tutorial is to oppose misconception beginners have about
strlen()
andsizeof()
. I hope they don’t think the same after reading this article.