As we know that in programming string can be defined as the collection of characters. Now for the requirement of finding how many characters are being used to create a string, C provides two approaches which are strlen() and sizeof().
As mentioned in above point both of these methods are used to find out the length of target operand but on the basis of their internal implementation following are some basic differences between both.
Sr. No. | Key | strlen() | sizeof() |
---|---|---|---|
1 | Definition | strlen() is a predefined function defined in a Header file named string.h in C. | On other hand sizeof() is a Unary operator and not a predefined function. |
2 | Implementation | strlen is internally implemented as it primarily counts the numbers of characters in a string excluding null values, i.e returns the length of null terminating string. | While sizeof is implemented in such way that it calculates actual size of any type of data (allocated) in bytes (including the null values). |
3 | Null handling | strln excludes null and do not include it in the total computation of length of string. | On other hand sizeof doesn't care about the values of variable and compute actual size of any type of data (allocated) in bytes (including the null values). |