Table of contents
1.
Introduction
2.
strlen()
3.
sizeof()
4.
Differences between strlen() and sizeof()
5.
FAQs 
5.1.
What is the use of the null character \0?
5.2.
What is size_t in C?
6.
Conclusion
Last Updated: Mar 27, 2024

Difference between strlen() and sizeof()

Author Yashesvinee V
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Finding the length of a string, which is also an array of characters in C, is an important operation. This value is used in operations involving string manipulation. The length of a string can be determined using two functions.

  1. strlen() - To find the number of characters in a string until it encounters a null character.
     
  2. sizeof() - To find the size of a given variable’s value, including strings.
     

Also see : C Static Function, Tribonacci Series

strlen()

strlen() is a predefined function in C contained in the string.h header file. It accepts a pointer to a character array as its argument. Upon locating the array in the memory, it traverses the array of characters and counts them until it finds the null character \0. The null character denotes the end of a string. The strlen() function returns the length of a character array (string).

Syntax: 

strlen(const char *str)
You can also try this code with Online C Compiler
Run Code

 

Example:

#include<stdio.h>
#include<string.h>

int main()
{
    char str[] = "Length of a string";
    
    int length = strlen(str);

    printf("length of string str is %d", length);
    
    return 0;
}
You can also try this code with Online C Compiler
Run Code

 

Output: 

Length of string str is 18

sizeof()

The sizeof() function calculates the length of any value it receives as an argument. The arguments may be integer, float, character or double data type. When it receives a character array or string as an argument, it computes the size occupied by it in the memory. Since characters occupy 1 byte of memory, the number of bytes occupied by the string characters is equivalent to the length of a string. The sizeof() function returns a value of unsigned integer type usually denoted by size_t. Note that the sizeof() function counts the null character \0 as part of the string’s length. 

Syntax:

sizeof(argument)
You can also try this code with Online C Compiler
Run Code

 

The sizeof() function accepts data types, variables and expressions as arguments.

Example:

#include<stdio.h>

int main()
{
    char str[] = "Length of a string";
    
    int length = sizeof(str);

    printf("length of string str is %d", length);
    
    return 0;
}
You can also try this code with Online C Compiler
Run Code

 

Output:

Length of string str is 19

Differences between strlen() and sizeof()

The strlen() and sizeof() functions are used for the same purpose but return different values. This is mainly because strlen() does not count the null character \0, but sizeof() does. The strlen() and sizeof() functions differ in the following ways.

strlen()

sizeof()

It is a predefined function present in the string.h header file. A function that performs a unary operation to return the size occupied by an operand’s value.
It returns the length of a string. It returns the space occupied by a variable’s value counted in bytes.
It does not count null characters. It counts null characters.
It works only on string values stored in the form of a character array. It works on values of any data type.
The returned value is of long int type. The returned value is of unsigned int type.
It is independent of the system and design of the C compiler. It is dependent on the system and design of the C compiler.

 

Also check out “strcat() function in C and  Short int in C Programming

 

#include<stdio.h>
#include<string.h>

int main()
{
    char str[] = "HelloWorld";
    int length_sizeof, length_strlen;

    length_sizeof = sizeof(str);
    length_strlen = strlen(str);

    printf("sizeof() of string \"HelloWorld\" returns %d and strlen() returns %d.", length_sizeof, length_strlen);
    
    // sizeof() also considers \0 as a part of the string
    // Hence, for a string s: sizeof(s) = strlen(s) + 1 

    return 0;
}
You can also try this code with Online C Compiler
Run Code

 

Output:

sizeof() of string "HelloWorld" returns 11 and strlen() returns 10.

FAQs 

What is the use of the null character \0?

The Null character in the C is used to terminate character strings. It represents the end of the string or end of an array by default. The end of the character string is ‘\0’ or simply NULL. It occupies 1 byte of memory space.

What is size_t in C?

It represents the size of objects in bytes and is therefore used as the return type by the sizeof() function. It can contain the size of the biggest object in the host system. The maximum permissible size depends on the compiler. For a 32 bit compiler, it represents unsigned int; for 64 bit it represents unsigned long long. The size_t data type is never negative.

Conclusion

This article extensively discusses the strlen() and sizeof() functions in C. We hope that this blog has helped you differentiate between the strlen() and sizeof() functions. If you would like to learn more, check out our articles on  Basics of C and Functions in C. Learn more about ArraysPointersMemory managementStructures and Unions in C.

Difference Between IOT and M2M

Check out this problem - Multiply Strings

Looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc? Have a look at the problems, interview experiences, and interview bundle for placement preparations. Take a look at our paid courses to give your career an edge over others!

Upvote our blogs if you find them helpful and engaging! Happy Learning!

Live masterclass