Difference between strlen() and sizeof() for String in C

Question: What is Difference Between Determining Length of a String and Character Array using sizeof Operator and strlen() Function in C Programming Language?

Answer: Let’s recall, firstly, concept of sizeof operator and strlen() functions before we implement them with character arrays or strings.

sizeof operator returns amount of memory allocated, in bytes, of the argument passed to it. It works with string as well as with non-string data. In particular, it works with any type of data. While strlen function works only with character strings. Undefined results when ‘Non-String’ data is passed to strlen. Let’s consider a C program to unravel the behaviour of these two functions.

Let’s write a C program to test working of sizeof and strlen functions with arrays and strings, for example:

/*
 * diff_sizeof_strlen.c -- program shows differences between using sizeof
 * and strlen with arrays and strings
 */
#include <stdio.h>
#include <string.h>
 
int main(void)
{
    char char_arr[100] = {0};    /* 'char_arr' array string declared */
    char str[100] = "strlen returns actual no. of characters in the string"
                    " but NULL byte";
    int ranking[50];             /* 'ranking' an array of 50 integers */
    float avg_marks[10];         /* 'avg_marks' an array of 10 floats */
    char *str_lit = "I'm string literal declared and initialized using "
                    "ptr-to-char";
 
    puts("\n**Program differentiates use of sizeof and strlen with arrays "
         "and strings**\n");
    puts("\nUsing sizeof...\n");
    printf("Amount of mem. allocated to \"char char_arr[100]\" is %d "
           "bytes.\n", sizeof(char_arr));
    printf("Amount of mem. allocated to \"char str[100]\" is %d bytes.\n", 
           sizeof(str));
    printf("Amount of mem. allocated to \"int ranking[50]\" is %d bytes."
           "\n", sizeof(ranking));
    printf("Amount of mem. allocated to \"float avg_marks[10]\" is %d "
           "bytes.\n", sizeof(avg_marks));
    printf("Amount of mem. allocated to \"char *str_lit\" is %d bytes.\n", 
           sizeof(str_lit));
 
    puts("\nUsing strlen...\n");
    printf("No. of characters returned in \"char char_arr[100]\" is %d."
           "\n", strlen(char_arr));
    printf("No. of characters returned in \"char str[100]\" is %d.\n", 
           strlen(str));
    printf("No. of characters returned in \"int ranking[50]\" is %d.\n", 
           strlen(ranking));
    printf("No. of characters returned in \"float avg_marks[10]\" is %d."
           "\n", strlen(avg_marks));
    printf("No. of characters returned in \"char *str_lit\" is %d.\n", 
           strlen(str_lit));
    puts("");
    return 0;
}

When above program compiled with warnings enabled, result is,

advertisement
advertisement
sizeof_stlen.c: In function ‘main’:
sizeof_stlen.c:23:2: warning: passing argument 1 of ‘strlen’ from
incompatible pointer type [enabled by default]
/usr/include/string.h:399:15: note: expected ‘const char *’ but argument
is of type 'int *’
sizeof_stlen.c:24:2: warning: passing argument 1 of ‘strlen’ from
incompatible pointer type [enabled by default]
/usr/include/string.h:399:15: note: expected ‘const char *’ but argument
is of type ‘float *’

and when run with warnings allowed, resulted as:

**Program differentiates use of sizeof and strlen with arrays and strings**
 
Using sizeof...
 
Amount of mem. allocated to "char char_arr[100]" is 100 bytes.
Amount of mem. allocated to "char str[100]" is 100 bytes.
Amount of mem. allocated to "int ranking[50]" is 200 bytes.
Amount of mem. allocated to "float avg_marks[10]" is 40 bytes.
Amount of mem. allocated to "char *str_lit" is 8 bytes.
 
Using strlen...
 
No. of characters returned in "char char_arr[100]" is 0.
No. of characters returned in "char str[100]" is 66.
No. of characters returned in "int ranking[50]" is 5.
No. of characters returned in "float avg_marks[10]" is 6.
No. of characters returned in "char *str_lit" is 61.

Analysing the output below,

Function strlen has prototype as:

    size_t strlen(char const*);

that is it handles character strings only. Further, it returns actual no. of characters in the string not counting on NULL. Notice char_arr[100] = {0} is allocated 100 bytes but returns 0 characters.

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

advertisement
If you wish to look at all C Tutorials, go to C Tutorials.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.