close

[學習]c++資料型態


  在網路上看到這個,覺得不錯,想偷偷放著,所以就移過來了。已經會的人不要偷笑我。


  以上的資料型態在記憶體中所佔有的大小依平台系統而有所差異,word的大小取決於機器,在32位元機器上通常一個word是4個位元組,如果想要知道這些資料型態在您所使用的平台上,所佔有的記憶體空間有多少,最好的作法是使用sizeof()運算子,它可以告訴您確實的記憶體大小,下面這個程式是個簡單的示範:

#include
using namespace std;

int main() {
    cout << "\n型態\t大小(bytes)";
    cout << "\nint\t" << sizeof(int);
    cout << "\nlong\t" << sizeof(long);
    cout << "\nfloat\t" << sizeof(float);
    cout << "\ndouble\t" << sizeof(double);
    cout << "\nchar\t" << sizeof(char);
    cout << "\n";

    return 0;
}




其中'\t'是跳格字元,它相當於在主控台中按下Tab鍵的效果,可以用來對齊下一個顯示位置;以下是執行結果:
型態    大小(bytes)
int         4
long      4
float      4
double  8
char       1












by兔

arrow
arrow
    全站熱搜

    Melita 粉兔 發表在 痞客邦 留言(0) 人氣()