C has a library rich in functions, one of which is the strlen function. In simple words, Strlen in C is used to return the length of a string. While calculating the length, the null character is not considered.
Syntax of Strlen in C
size_t strlen(const char *s);
Also read
- Compare two different strings without using strcmp in C
- Learn Operator Precedence in C Programming
- Learn Various Aspects of Hindi Varnamala in English
Table of Contents
A Program Using Strlen to Calculate C string length
Now we will implement strlen in C.
Sample Code,
#include <stdio.h>
#include <string.h>
int main()
{
char x[30]=”Hello Guys!”; //Initialize string x
char y[30]={‘H’,’E’,’L’,’L’,’O’,’\0′}; //Initialize string y
printf(“First string length is = %d\n”,strlen(x)); //strlen function is used to calculate the length of string ‘x’
printf(“Second string length is = %d\n”,strlen(y));
return 0;
}
First string length is = 11
Second string length is = 5
Explanation
This program is very simple. Two strings called x and y are declared and strings “Hello Guys!” and “Hello” are stored in x and y respectively. Then the lengths of both the strings are printed to the screen using the strlen function containing arguments x and y.
Find out Length of String in C Without Strlen Function in C or Implement strlen Function in C.
Let us Find out the length of a string without using strlen.
Implementation of Strlen in C
#include <stdio.h>
int main()
{
char string[50] = “Hello people!”; //Initialize string
int x;
for (x=0;string[x]!=’\0′;++x);
printf(“Given string is: %s\n”, string);
printf(“String Length is: %d”, x);
return 0;
}
Output
Given string is: Hello people!
The length of the given string is: 13
Explanation
Initialize the string “Hello people!”.
A variable x of type integer is declared.
A for loop is started containing the conditions – x=0; string[0]!=’\0’; ++x. This means the string’s each character will be counted till the string’s end while excluding the null character ‘\0’. The string is then printed for the user and after that, the string length is returned to the screen.
Sizeof in C: What is Sizeof Operator?
The sizeof() is an operator in the c programming language which can be used to find the size of any given data type. The sizeof() operator is used to return the size of a given variable.
C Program to Calculate the Sizeof a Variable using Sizeof()
Now we will implement the sizeof() operator in a program.
Sample Code
#include <stdio.h>
int main()
{
int x=100; //Initialize Variable x;
printf(“Size of variable type int is: %d\n”,sizeof(x));
printf(“Size of data type float is: %d\n”,sizeof(float));
return 0;
}
Output
Size of variable type int is: 4
Size of data type float is: 4
Explanation
Using sizeof() operator is really simple. Declare a variable of any data type, for instance, x of type int. In the printf statement, use %d to get an integer value, and use the sizeof operator with argument x. Another way to get the size of a data type is to write the name of the data type itself as the argument, for instance, sizeof(float).
Implement Sizeof Function in C.
Firstly, we will calculate the size of an operator without using the sizeof() operator.
Sample Code
#include<stdio.h>
int main()
{
int *size=NULL;
size++;
printf(“Size of given variable is = %d\n”,size);
return 0;
}
Output
Size of the given variable is = 4
Explanation
We initialize a null pointer variable ‘size’. We increase the pointer variable by 1. When we increase ‘size’ value, it has an increment of 4 bytes. Similarly, you can calculate the length of string or some other data.
Difference Between Strlen Vs Sizeof in C
There are various functions in the C library. Do you know that you can use both strlen and sizeof functions to calculate the length of the string? But there is a small difference between them. The strlen function works only for strings and prints the length in numeric value.
The sizeof() function finds the size of any given data type and then prints it to the screen. There is one more difference between them which we will understand from the programming example
#include <stdio.h>
#include <string.h>
int main()
{
char str[]=”Hello”;
int len, size;
//Find out the length of string using strlen in C
len = strlen(str);
printf (“String length calulated using strlen %d\n”, len);
//Find out the length of string using sizeof C
size=sizeof(str);
printf (“String length calculated using sizeof:%d”, size);
return 0;
}
Output
String length calculated using strlen=5
String length calculated using sizeof=6
Explanation
As you see at the output that the C string length calculates the strlen function is 5 and by sizeof function is 6. Let’s understand why
H | E | L | L | O | ‘\0’ |
See, when we calculated the string length using the strlen function, it only counted the string characters, not counted ‘\0’ (null) character. While Sizeof also counted Null (‘\0’) character.