sizeof Operator
// sizeof , 不是函數,是 operator
The sizeof operator yields the size of its operand with respect to the size
of type char. The result of the sizeof operator is of type size_t, an
integral type defined in the include file STDDEF.H. The operand to sizeof can
be one of the following:
A type name.
To use sizeof with a type name, the name must be enclosed in
parentheses.
// sizeof(int);
An expression. When used with an expression, sizeof can be specified with or
without the parentheses.
// sizeof(2*3);
or
sizeof 2*3;
以上,都 OK
The expression is not evaluated.
When the sizeof operator is applied to an object of type char, it yields 1.
When the sizeof operator is applied to an array, it yields the total number
of bytes in that array. For example:
// sizeof 2*3;
sizeof(2*3);
int no;
sizeof(no);
#include <iostream.h>
void main()
{
char szHello[] = "Hello, world!";
cout << "The size of the type of " << szHello << " is: "
<< sizeof( char ) << "\n";
cout << "The length of " << szHello << " is: "
<< sizeof szHello << "\n";
}
※ 引述《femininesex (femininesex)》之銘言:
: 開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
: VC++
: 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
: none
: 問題(Question):
: 不清楚為何
: 'a'佔了記憶體4 bytes?
: 0377777777777 佔了 7 bytes?(total 11 7s)
: 077777777777 卻只佔了 1 byte?(total 11 7s)
: 餵入的資料(Input):
: none
: 預期的正確結果(Expected Output):
: sizeof(61)=1
: sizeof(37777777777)=4
: sizeof(37777777777)=5
: sizeof(37777777777)=5
: 錯誤結果(Wrong Output):
: sizeof(61)=4
: sizeof(37777777777)=4
: sizeof(37777777777)=7
: sizeof(37777777777)=1
: 程式碼(Code):(請善用置底文網頁, 記得排版)
: http://codepad.org/7ZeNKbHa
: 補充說明(Supplement):
: none
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 223.139.18.217