Difference between strlen() and sizeof() for string in C Program

CServer Side ProgrammingProgramming

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.Keystrlen()sizeof()
1Definitionstrlen() 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.
2Implementationstrlen 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).
3Null handlingstrln 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).
raja
Published on 13-Jan-2020 05:35:30
Advertisements