Difference between strlen() and sizeof() for string in C
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.
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():
Strlen() | Sizeof() |
---|---|
Strlen() is a predefined function, which is defined in a header file known as string.h. | Sizeof() is a unary operator or compile-time expression that calculates the amount of the memory occupied by a variable. |
This method is used to find the length of an array of string or character. | This method is used to find the actual size of data (allocated) in bytes. |
It counts the numbers of characters in a string eliminating null values. | It returns the size of an operand, not the string length (including null values). |
It allocates memory at the runtime and look for the null value of variable. | Sizeof() does not care about the variable value. |
Strlen() function works only with character strings | Sizeof() function returns the amount of memory allocated in bytes. |
Return value in strlen() is long int. | Return value in sizeof() is unsigned int. |
There are only one syntax of strlen() which is int strlen(const char *str); | There are three syntaxes of sizeof() 1) sizeof(type), 2) sizeof(variable-name), and 3) sizeof(expression): |