1. 程式人生 > 實用技巧 >《C++Primer Plus》 | 處理資料

《C++Primer Plus》 | 處理資料

基本資料型別:整型和浮點數

int

1.使用變數標識儲存的資料
2.寬度(width)用來描述儲存資料時用的記憶體量,char,short,int ,long ,long long (C++11新增)寬度遞增,每種又都含無符號數與有符號數。

3.標頭檔案 climits 定義了符號常量來表示型別的限制,包含如語句#define INT_MAX 2147483647

#include<iostream>
#include<climits>
int main()
{
    using namespace std;
    int n_int=INT_MAX ;//n 表示整數值
    short n_short=SHRT_MAX;
    long n_long=LONG_MAX;
    long long n_llong=LLONG_MAX;
       cout <<"int 是"<<sizeof(n_int) <<"位元組" <<endl;
       cout <<"short 是"<<sizeof(n_short) <<"位元組" <<endl;
       cout <<"long 是"<<sizeof(n_long) <<"位元組" <<endl;
       cout <<"long long 是"<<sizeof(n_llong) <<"位元組" <<endl;
       cout <<"最大表示的值" <<endl;
       cout <<"int :"<<n_int <<endl;
       cout <<"short :"<<n_short  <<endl;
       cout <<"long :"<<n_long <<endl;
       cout <<"long long :"<<n_llong <<endl;


}

結果:

int 是4位元組
short 是2位元組
long 是4位元組
long long 是8位元組
最大表示的值
int :2147483647
short :32767
long :2147483647
long long :9223372036854775807

4.int a(5);C++的特性(允許這樣的初始化),int{5}或int ={5}(C++11)可用於單值變數,為了防止型別轉化錯誤。
5.unsigned 是 unsigned int 的縮寫,整型在重置點是會像里程錶一樣在範圍的另一端取值。


6.cout << hex;像cout物件傳送已對應進位制列印的訊息,在using namespace std時hex 位於名稱空間中,變數名不能是hex


常量無後綴是預設已int儲存。

char

#include <iostream>

using namespace std;

int main()
{   char ch;
    cout <<"請輸入字元" << endl;
    cin  >>ch;
    cout << "你輸入的字元是:" <<ch<< endl;
    return 0;
}

g++ -g -o c c.cpp


rsi暫存器中顯示0x61即為96為a對應的ASCLL值

chen@ubuntu:~/Desktop$ ./c
請輸入字元
a
你輸入的字元是:a

1.cin將a轉化成0x61,cout將0x61轉化為a.C++對字元使用單引號,字串選用雙引號,值的型別會引導cout顯示相應的顯示方式。
2.cout.put()列印字元

符號常量

1.C++儘量使用const定義符號常量。
2.cout會刪除浮點數結尾的0