sizeof(
表示式)
是c/c++
關鍵字(不是函式)。
功能:返回
「表示式
」結果所佔機器「位元組
」的大小。
strlen(字串)
是c/c++
標準庫的函式(不是關鍵字),在標頭檔案
string.h
中宣告。
功能:計算「字串
」中的'/0'
之前的字元個數。
特別注意:strlen總是假定傳給它的引數是以null結束符'/0'結尾的,所以如果傳給strlen的引數不是以'/0'結尾的話,strlen會一直計算長度知道遇到'/0',所以在給strlen傳參時要特別注意。
很多人覺得
sizeof
和strlen
對於乙個字元「陣列
」運算後的結果「差不多」,那是因為在現在的
x86計算機中,乙個
ascii
字元剛好是乙個機器「位元組
」。name
strlen - calculate the length of a string
synopsis
#include
size_t strlen(const char *s);
description
thestrlen()functioncalculatesthelengthofthe string s, not
including the terminating '/0' character.
return value
the strlen() function returns the number of characters in s.
conforming to
svr4, 4.3bsd, c89, c99.
see also
string(3), wcslen(3), wcsnlen(3)
sizeof求位元組以及與strlen的區別
例子一 根據以下條件進行計算 1 結構體的大小等於結構體內最大成員大小的整數倍 2 結構體內的成員的首位址相對於結構體首位址的偏移量是其型別大小的整數倍,比如說double型成員相對於結構體的首位址的位址 偏移量應該是8的倍數。include include using namespace std ...
C語言中的strlen與sizeof的區別
sizeof與strlen是有著本質的區別,sizeof是求資料型別所佔的空間大小,而strlen是求字串的長度,字串以 0結尾。區別如下 1 sizeof是乙個c語言中的乙個單目運算子,而strlen是乙個函式,用來計算字串的長度。2 sizeof求的是資料型別所佔空間的大小,而strlen是求字...
C語言中的strlen與sizeof的區別
sizeof與strlen是有著本質的區別,sizeof是求資料型別所佔的空間大小,而strlen是求字串的長度,字串以 0結尾。區別如下 1 sizeof是乙個c語言中的乙個單目運算子,而strlen是乙個函式,用來計算字串的長度。2 sizeof求的是資料型別所佔空間的大小,而strlen是求字...