Open In App

sizeof() vs strlen() vs size() in C++

Last Updated : 22 Mar, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The string is a sequence of characters or an array of characters. The declaration and definition of the string using an array of chars are similar to the declaration and definition of an array of any other data type. This article focuses on discussing the three string functions:

  1. sizeof()
  2. strlen()
  3. size()

The use of these three functions to calculate the size of the string depends on the declaration type in the code. Let’s discuss each of these functions in detail.

sizeof()

The 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 the 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 data types such as structure, union, etc.

Syntax:

sizeof(arr)

 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 an argument and walks through memory at run time from the address we give it looking for a NULL character and counting 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.

Syntax:

strlen(arr)

size()

size() function is used to return the length of the string in terms of bytes. It defines the actual number of bytes that conform to the contents of the String object.

Syntax:

str.size()  

sizeof() vs strlen() vs size()

There are the following points of difference between the three functions:

  1. Type: Sizeof operator is a unary operator, strlen() is a predefined function in C whereas the size() is the member function of the string class.
  2. Data Types Supported: sizeof() gives the actual size of any type of data (allocated) in bytes (including the null values), strlen() is used to get the length of an array of chars/string whereas size() returns the number of the characters in the string.
  3. Evaluation Size: sizeof() is a compile-time expression that gives 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 the length of a C-style NULL-terminated string and size() also returns the length of the specified string.
  4. Difference in Return Value: If the same value is passed as input to sizeof(), strlen(), and size(), then the sizeof() will give one value higher than the length of the string.
S No. sizeof() strlen() size()
1.  Compile-time unary operator. The predefined function of the char array. A member function of string.
2. Return the actual size of any type of data (allocated) in bytes. It looks for a NULL character and returns a number of elements present in the string before the NULL character. Return length of the string in bytes.
3. Used in case of char array where the elements are separated by a comma. Used in case of char array where the elements are not separated by a comma. Used in case of a string.
4. Example:
char arr[] = {‘G’,’e’,’e’,’k’}
Example:
char arr[] = “Geek”
Example:
string str = “Geek”

Below is the C++ program to implement the above concepts:

C++




// C++ program to implement
// the above approach
#include <cstring>
#include <iostream>
using namespace std;
 
// Driver code
int main()
{
    char str1[] = { 'G', 'e', 'e', 'k' };
    char str2[] = "Geek";
    string str3 = "Geek";
 
    // Finding the length of the
    // string using different methods
    // This method works fine
    cout << "The length of str1 using sizeof(): "
         << sizeof(str1) << "\n";
 
    // This method behaves abnormally
    cout << "The length of str1 using strlen(): "
         << strlen(str1) << "\n"
         << endl;
 
    // Gives length of string + 1
 
    cout << "The length of str2 using sizeof(): "
         << sizeof(str2) << "\n";
 
    // This method Works fine
    cout << "The length of str2 using strlen(): "
         << strlen(str2) << "\n"
         << endl;
 
    // This method always gives 32
    cout << "The length of str3 using sizeof(): "
         << sizeof(str3) << "\n";
 
    // This method works fine
    cout << "The length of str3 using size(): "
         << str3.size() << "\n"
         << endl;
}


 
 

Output

The length of str1 using sizeof(): 4
The length of str1 using strlen(): 6

The length of str2 using sizeof(): 5
The length of str2 using strlen(): 4

The length of str3 using sizeof(): 32
The length of str3 using size(): 4

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads