What is strlen()
strlen() is a function to find the length of a string. It counts total characters which are presented in a string, eliminating the null character. The total number of characters in string includes, alphabets, special characters, and numbers, with blank spaces.
In this difference between strlen() and sizeof() for string in C tutorial, you will learn:
What is strlen()
What is sizeof()
Syntax of strlen()
Syntax of sizeof()
Difference between strlen() and sizeof()
What is sizeof()
The sizeof() is a function that is used to calculate the size of its operand. It returns the size of particular variable. This function can be applied to any data type, containing primitive types like integer and pointer types, floating-point types, structure, or union. The output of the program containing sizeof() may be different on the 32-bit system and 64-bit system.
KEY DIFFERENCES:
- Strlen method is used to find the length of an array whereas sizeof() method is used to find the actual size of data.
- Strlen() counts the numbers of characters in a string while sizeof() returns the size of an operand.
- Strlen() looks for the null value of variable but sizeof() does not care about the variable value.
- Return value in strlen() is long int on the other hand return value in sizeof() is unsigned int.
Syntax of strlen()
strcount = strlen(my_string);
Here, my_string is a character array variable.
Example of strlen()
In the below C program, we have declared string variable of type char. Strng variable is a passed as an argument of strlen() function to find the length of string.
#include<stdio.h> #include<string.h> int main() { char strng[] = "January"; printf("Length of given string is: %lu\n", strlen(strng)); }
Output:
Length of given string is: 7
Syntax of sizeof()
Syntax 1)
sizeof(type): Type= referenced type
Example of sizeof(type):
In the below code, &type gives the address of the variable (double x). It is incremented with 1 which gives the address where you can store the next variable of type x.
Typecasting x into char* and taking the difference will enables you to know total number of variables of type char stored in memory. We have used getchar() to read character.
#include<stdio.h> #define my_sizeof(type) (char *)(&type+1)-(char*)(&type) int main() { double x; printf("%ld", my_sizeof(x)); getchar(); return 0; }
output:
8
Syntax 2)
sizeof(variable-name): Variable-name= name of the variable you like to determine size.
In the below C program, we are printing the size char data type. Printf statement contains sizeof function with argument char.
Example of sizeof(variable-name):
#include<stdio.h> int main() { printf("sizeof(char) = %d\n\n", sizeof(char)); return 0; }
Output:
sizeof(char) = 1
Syntax 3)
sizeof(expression): Expression= Expression you have to evaluate.
Example of sizeof(expression):
In the below program, we are first calculating and printing the size of variable. After this, we are evaluating expression, store it in variable a, and displaying the result in printf statement.
#include<stdio.h> int main() { char p = 'S'; double q = 4.65; printf("Size of variable p : %d\n",sizeof(p)); printf("Size of an expression : %d\n",sizeof(p+q)); int a = (int)(p+q); printf("Size of explicitly converted expression : %d\n",sizeof(a)); return 0; }
Output:
Size of variable p : 1 Size of an expression : 8 Size of explicitly converted expression : 4
Difference between strlen() and sizeof()
Here are the important differences between strlen() and sizeof():
0 Comments