C++基礎資料型別
阿新 • • 發佈:2021-08-17
/* 浮點型,float 和double型別,float佔4個位元組,double佔8個位元組 檢視資料型別或者變數佔用的記憶體用關鍵字sizeof(); */ float speed = 100.3f; //float型別小數點後加f,因為小數預設為double型別,需要轉化為float,省去了這步驟 double force = 12.5; cout << "速度是:" <<sizeof(speed) << endl; cout << "力是:" << sizeof(force) << endl; //科學計數法 float sce1 = 3e2f; cout << "sce1=" << sce1 << endl; //e2表示10^2 float sce2 = 4e-2f; cout << "sce2=" << sce2 << endl; //e-2表示10 /* 字元資料型別char,佔一個位元組,計算機在處理字元的時候,會將其先轉化為ASCII碼 然後再將對應的ASCII碼轉為0、1二進位制串。 */ charch = '3'; cout << "字串佔的記憶體是:" << sizeof(ch) << endl; /* 轉義字元,\n,\\,\t:水平製表符 */ cout << "GoodGirl\n" << endl; cout << "GooGirl\tabc" << endl; cout << "BGirl\tabc" << endl; /* 字串型別 C風格和C++風格 */ char str1[] = "good"; cout << str1 << endl; string str2 = "bad"; cout << str2 << endl; /* 布林型別,佔用一個位元組的記憶體空間 */ bool success = true; cout << success << endl; /* 鍵盤輸入:cin>> */ int num = 1; cout << "請輸入一個整數:"<<endl; cin >> num; cout << num << endl;