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.

Syntax of strlen()

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. Output:

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. output:

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): Output:

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. Output:

Difference between strlen() and sizeof()

Here are the important differences between strlen() and sizeof():