Do you think IIT Guwahati certified course can help you in your career?
No
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.
strlen() - To find the number of characters in a string until it encounters a null character.
sizeof() - To find the size of a given variable’s value, including strings.
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).
#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;
}
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.
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.
#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;
}
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 Arrays, Pointers, Memory management, Structures and Unions in C.
Looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc? Have a look at theproblems,interview experiences, andinterview bundle for placement preparations. Take a look at our paidcourses to give your career an edge over others!
Upvote our blogs if you find them helpful and engaging! Happy Learning!
Live masterclass
Switch to top management roles with Online MBA
by Coding Ninjas
20 Jan, 2025
11:30 AM
Transition to SDE Career with Online PG Certification by IIT
by Coding Ninjas
20 Jan, 2025
03:30 PM
Transition to data career with Online PG Certification by IIT
by Coding Ninjas
21 Jan, 2025
01:30 PM
Switch to top management roles with Online MBA
by Coding Ninjas
20 Jan, 2025
11:30 AM
Transition to SDE Career with Online PG Certification by IIT