Difference Between strlen and sizeof in C [Code Explained in Detail]

Difference Between strlen and sizeof in C [Code Explained in Detail]

sterlen() in C

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.

  • It counts all the characters present in the string.
  • This function is defined in the string.h header file.
  • You need to include 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 the string str1 is %d.", strlen(str1));
  return 0;
}

Output:

Length of the string str1 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 length, they can not be anagrams.

sizeof() in C

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 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. And 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 of the string str1 is %d.", sizeof(str1));
  return 0;
}

Output:

Size of the string str1 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.

How to find the length of the string using sizeof() operator in C?

If you are not sure about the size of the character, you can simply divide the size of the string (or array) by character. You will get the length of the string.

#include <stdio.h>
#include <string.h>
int main()
{
  char str1[] = "CSEstack Programming";
  printf("Size of the string str1 is %d.", sizeof(str1)/sizeof(char));
  return 0;
}

How to find number of elements in the array using sizeof Operator()?

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

Why is the size of the string one higher than the length of the same string?

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 requires 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 str1 is %d.", strlen(str1));
  printf("\nSize of the string str1 is %d.", sizeof(str1));
  return 0;
}

Output:

Length of the string str1 is 0.
Size of the string str1 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().

strlen() Vs sizeof()

Let’s compare these two builtin functions in C programming.

Difference Between strlen and sizeof in C Programming:

  • Function/Operation/Evaluation:
    The strlen() returns you the length of the string stored in the array, however, sizeof() returns the total allocated size assigned to the array.
  • Difference in Return Value:
    If you pass the same string as input to both functions 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.)
  • Dependency:
    Size of the string also depends on your system and design of your C compiler.
  • Data Type Support:
    Unlike strlen(), we can use the function sizeof() with any datatypes.
  • Return Type:
    The return type of the strlen() is the integer, whereas 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.

2 Comments

  1. My search (Duckduckgo) led me to believe this page talked about sizeof() and strlen() 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 so strlen(). I think. It’s tricky, there’s a right way to do it but I don’t do it often so I forgot.

Leave a Reply

Your email address will not be published. Required fields are marked *