C++ - Data type (C++軟體開發 - 資料型態 概念與實例)

◎基本概念 



Type
Byte
Range
char
1
-128~127  /  0~255
bool
1
0~1
int
4
-2147483648 ~ 2147483647
Short int
2
-32768~32767
Long int
8
-9223372036854775808 ~ 9223372036854775807
float
4
(~7 digits)
double
8
(~15 digits)
long double
8
(~15 digits)
Wchar_t
2 / 4


關於float、double最大值最小值以及精準度,比較複雜不在這篇介紹,可以參考
 C++ - Floating-Point Precision,float,double (C++軟體開發 - 浮點數精度 概念與實例)

 ◎簡單實例


依照complier還有電腦環境,可以與上面表格不同,可以用利用下列程式碼確認大小。

#include "stdafx.h"
#include <iostream>
#include <limits>  

using namespace std;

int main() {

    cout << "Size of char : " << sizeof(char) << endl;
    cout << "min:" << (numeric_limits<char>::min)() << " ";
    cout << "MAX:" << (numeric_limits<char>::max)() << endl;
    cout << "Size of bool : " << sizeof(bool) << endl;
    cout << "min:" << (numeric_limits<bool>::min)() << " ";
    cout << "MAX:" << (numeric_limits<bool>::max)() << endl;
    cout << "Size of int : " << sizeof(int) << endl;
    cout << "min:" << (numeric_limits<int>::min)() << " ";
    cout << "MAX:" << (numeric_limits<int>::max)() << endl;
    cout << "Size of short int : " << sizeof(short int) << endl;
    cout << "min:" << (numeric_limits<short int>::min)() << " ";
    cout << "MAX:" << (numeric_limits<short int>::max)() << endl;
    cout << "Size of long int : " << sizeof(long int) << endl;
    cout << "min:" << (numeric_limits<long int>::min)() << " ";
    cout << "MAX:" << (numeric_limits<long int>::max)() << endl;
    cout << "Size of float : " << sizeof(float) << endl;
    cout << "min:" << (numeric_limits<float>::min)() << " ";
    cout << "MAX:" << (numeric_limits<float>::max)() << endl;
    cout << "Size of double : " << sizeof(double) << endl;
    cout << "min:" << (numeric_limits<double>::min)() << " ";
    cout << "MAX:" << (numeric_limits<double>::max)() << endl;
    cout << "Size of long double : " << sizeof(long double) << endl;
    cout << "min:" << (numeric_limits<long double>::min)() << " ";
    cout << "MAX:" << (numeric_limits <long double > ::max)() << endl;
    cout << "Size of wchar_t : " << sizeof(wchar_t) << endl;
    cout << "min:" << (numeric_limits<wchar_t>::min)() << " ";
    cout << "MAX:" << (numeric_limits <wchar_t > ::max)() << endl;

    return 0;
}

留言