Difference between strlen() and sizeof() for string in C - GeeksforGeeks

08 Aug, 2019
sizeof()
Sizeof operator is a compile time unary operator which can be used to compute the size of its operand.
The result of sizeof is of unsigned integral type which is usually denoted by size_t.
sizeof can be applied to any data-type, including primitive types such as integer and floating-point types, pointer types, or compound datatypes such as Structure, union etc.
strlen()
strlen() is a predefined function in C whose definition is contained in the header file “string.h”.
strlen() accepts a pointer to an array as argument and walks through memory at run time from the address we give it looking for a NULL character and counts up how many memory locations it passed before it finds one.
The main task of strlen() is to count the length of an array or string.sizeof vs strlen()Type: Sizeof operator is a unary operator whereas strlen() is a predefined function in CData types supported: Sizeof gives actual size of any type of data (allocated) in bytes (including the null values) whereas get the length of an array of chars/string.Evaluation size: sizeof() is a compile-time expression giving you the size of a type or a variable’s type. It doesn’t care about the value of the variable.Strlen on the other hand, gives you the length of a C-style NULL-terminated string.Summary: The two are almost different concepts and used for different purposes.In context of C++: In C++, you do not need any of them as such.strlen() in C-style strings can be replaced by C++ std::strings.sizeof() in C is as an argument to functions like malloc(), memcpy() or memset() can be replaced by C++ (use new, std::copy(), and std::fill() or constructors).// C program to demonstrate difference // between strlen() and sizeof()#include<stdio.h>#include<string.h>  int main(){    char str[] = "November";    printf("Length of String is %lu\n", strlen(str));    printf("Size of String is %lu\n", sizeof(str));}Output:Length of String is 8
Size of String is 9
Since size of char in C is 1 byte but then also we find that strlen() gave one less value than sizeof().Explanation : We know, that every string terminates with a NULL character (“\0”).strlen() searches for that NULL character and counts the number of memory address passed, So it actually counts the number of elements present in the string before the NULL character, here which is 8.sizeof() operator returns actual amount of memory allocated for the operand passed to it. Here the operand is an array of characters which contains 9 characters including Null character and size of 1 character is 1 byte. So, here the total size is 9 bytes.Try to guess the output of following program:#include <iostream>#include <string.h>using namespace std;  int main(){        char a[] = {"Geeks for"};        char b[] = {'G','e','e','k','s',' ','f','o','r'};        cout << "sizeof(a) = " << sizeof(a);        cout << "\nstrlen(a) = "<< strlen(a);        cout<<  "\nsizeof(b) = " << sizeof(b);        cout<<  "\nstrlen(b) = " << strlen(b);                  return 0;}The strlen function looks for a null character and behaves abnormally if it doesn’t find it.Output:sizeof(a) = 10
strlen(a) = 9
sizeof(b) = 9
strlen(b) = 11
This article is contributed by Aditya Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.My Personal Notes
arrow_drop_upSave
sizeof vs strlen()
Type: Sizeof operator is a unary operator whereas strlen() is a predefined function in CData types supported: Sizeof gives actual size of any type of data (allocated) in bytes (including the null values) whereas get the length of an array of chars/string.Evaluation size: sizeof() is a compile-time expression giving you the size of a type or a variable’s type. It doesn’t care about the value of the variable.Strlen on the other hand, gives you the length of a C-style NULL-terminated string.Summary: The two are almost different concepts and used for different purposes.In context of C++: In C++, you do not need any of them as such.strlen() in C-style strings can be replaced by C++ std::strings.sizeof() in C is as an argument to functions like malloc(), memcpy() or memset() can be replaced by C++ (use new, std::copy(), and std::fill() or constructors).
Type: Sizeof operator is a unary operator whereas strlen() is a predefined function in C
Data types supported: Sizeof gives actual size of any type of data (allocated) in bytes (including the null values) whereas get the length of an array of chars/string.
Evaluation size: sizeof() is a compile-time expression giving you the size of a type or a variable’s type. It doesn’t care about the value of the variable.Strlen on the other hand, gives you the length of a C-style NULL-terminated string.
Summary: The two are almost different concepts and used for different purposes.
In context of C++: In C++, you do not need any of them as such.strlen() in C-style strings can be replaced by C++ std::strings.sizeof() in C is as an argument to functions like malloc(), memcpy() or memset() can be replaced by C++ (use new, std::copy(), and std::fill() or constructors).
// C program to demonstrate difference // between strlen() and sizeof()#include<stdio.h>#include<string.h>  int main(){    char str[] = "November";    printf("Length of String is %lu\n", strlen(str));    printf("Size of String is %lu\n", sizeof(str));}
Output:
Length of String is 8
Size of String is 9

Since size of char in C is 1 byte but then also we find that strlen() gave one less value than sizeof().
Explanation : We know, that every string terminates with a NULL character (“\0”).strlen() searches for that NULL character and counts the number of memory address passed, So it actually counts the number of elements present in the string before the NULL character, here which is 8.sizeof() operator returns actual amount of memory allocated for the operand passed to it. Here the operand is an array of characters which contains 9 characters including Null character and size of 1 character is 1 byte. So, here the total size is 9 bytes.Try to guess the output of following program:
#include <iostream>#include <string.h>using namespace std;  int main(){        char a[] = {"Geeks for"};        char b[] = {'G','e','e','k','s',' ','f','o','r'};        cout << "sizeof(a) = " << sizeof(a);        cout << "\nstrlen(a) = "<< strlen(a);        cout<<  "\nsizeof(b) = " << sizeof(b);        cout<<  "\nstrlen(b) = " << strlen(b);                  return 0;}
The strlen function looks for a null character and behaves abnormally if it doesn’t find it.Output:
sizeof(a) = 10
strlen(a) = 9
sizeof(b) = 9
strlen(b) = 11

This article is contributed by Aditya Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
abhikulshrestha1
C Language
C++
Difference Between
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Multidimensional Arrays in C / C++
rand() and srand() in C/C++
fork() in C
Left Shift and Right Shift Operators in C/C++
Command line arguments in C/C++
Vector in C++ STL
Inheritance in C++
Initialize a vector in C++ (6 different ways)
Map in C++ Standard Template Library (STL)
C++ Classes and Objects