From the perspective of external features:
# Include <stdio. h> </P> <p> # include <string> </P> <p> int main () </P> <p >{< br/> char array [] = "12345"; </P> <p> int n = sizeof (array ); // n is 6 </P> <p> int M = strlen (array); // m is 5 </P> <p> array [3] = 0; </P> <p> N = sizeof (array); // n is 6 </P> <p> M = strlen (array ); // m is 3 </P> <p> return 0; </P> <p >}</P> <p>
The essential difference between the two is that sizeof is an operator, while strlen is a function.
Their definitions in msdn are as follows:
Sizeof Operator
Sizeof expression
The sizeof keyword gives the amount of storage, in bytes, associated with a variable or a type (including aggregate types). This keyword returns a value of Type size_t.
The expression is either an identifier or a type-cast expression (a type specifier enclosed in
Parentheses ).
When applied to a structure type or variable, sizeof returns the actual size, which may include Padding Bytes inserted for alignment. when applied to a statically dimensioned array, sizeof returns the size of the entire array. the sizeof operator cannot
Return the size of dynamically allocated arrays or external arrays.
Zookeeper -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Strlen
Get the length of a string.
Routine required header:
Strlen <string. h>
Size_t strlen (const char * string );
Parameter
String: null-terminated string
Libraries
All versions of the C run-time libraries.
Return Value
Each of these functions returns the number of characters in string, excluding the terminal
Null. No return value is reserved to indicate an error.
Remarks
Each of these functions returns the number of characters in string, not including
Terminating null character. wcslen is a wide-character version of strlen; the argument
Wcslen is a wide-character string. wcslen and strlen behave identically otherwise.
It can be seen that strlen is for string, and sizeof is only a keyword.
Most compilers calculate the sizeof value during compilation, while strlen isProgramCalculated during execution.
Sizeof calculates the memory size occupied by this type, while strlen calculates the actual length of the string.
Evaluating sizeof expr does not evaluate the expression.
The result of applying sizeof depends in part on the type involved:
L sizeof Char or an expression of type char is guaranteed to be 1
L sizeof a reference type returns the size of the memory necessary to contain an object of the referenced type
L sizeof a pointer returns the size needed hold a pointer; to obtain the size of the object to which the pointer pointers, the pointer must be dereferenced
L sizeof an array is equivalent to taking the sizeof the element type times the number of elements in the array