Difference between Strlen () and Sizeof (); Difference between strlensizeof
The strlen () function is used to calculate the actual length of a string. It is obtained from the beginning to the First '\ 0'. If you only define that it is not assigned an initial value, this result is not fixed. It will be searched from the first address until it encounters '\ 0 '. The strlen results can be calculated only at runtime.
Sizeof () returns the amount of memory occupied after the variable declaration, not the actual length. In addition, sizeof is not a function, but an operator, and strlen is a function. Sizeof is calculated during compilation.
Example:
1. char xs = "123456789 ";
Sizeof (s) // 4, s is a character pointer to a String constant
Strlen (s) // 9, which can only be used to obtain the length of this string
2. char s [] = "123456789 ";
Sizeof (s) // 10, s is an array, calculated to the '\ 0' position, so 9 + 1
Strlen (s) // 9, strlen is a function internal implementation is to use a loop to calculate until '\ 0'
3. char s [100] = "123456789 ";
Sizeof (s) // 100, s indicates the memory size of 100 × 1
Strlen (s) // 9, strlen is a function internal implementation is to use a loop to calculate until '\ 0'
4. int s [100] = "0123456789 ";
Sizeof (s) // 400, s indicates the memory size of 100x4
Strlen (s) // error. The strlen parameter can only be char * and must end with '\ 0'