首先看一MSDN上如何對(duì)sizeof進(jìn)行定義的:sizeof Operatorsizeof eXPRessionThe 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.然后再看一下對(duì)strlen是如何定義的: strlenGet the length of a string.Routine Required Header:strlen <string.h>size_t strlen( const char *string );Parameterstring:Null-terminated string LibrariesAll versions of the C run-time libraries.Return ValueEach of these functions returns the number of characters in string, excluding the terminal NULL. No return value is reserved to indicate an error.RemarksEach of these functions returns the number of characters in string, not including the terminating null character. wcslen is a wide-character version of strlen; the argument of wcslen is a wide-character string. wcslen and strlen behave identically otherwise.二、由幾個(gè)例子說(shuō)開去。
3.sizeof可以用類型做參數(shù),strlen只能用char*做參數(shù),且必須是以///////////////'///////////////'////////////////0///////////////'///////////////'結(jié)尾的。sizeof還可以用函數(shù)做參數(shù),比如: short f();printf("%d////////////////n", sizeof(f()));輸出的結(jié)果是sizeof(short),即2。