物理の駅 by onsanai

Physics-station 研究で日々感じたことを忘れないための備忘録

文字型と整数型の上限値と下限値、各データ型のサイズ

参照: ATLAS Japan C++ Course -- Lesson 2

サンプルコード1

#include <iostream>
#include <cstdint>

using namespace std;
int main(int argc, char** argv) {

    cout 
        << "char min. = " << int(INT8_MIN)
        << " max. = " << int(INT8_MAX) << endl
        << "short int min. = " << INT16_MIN
        << " max. = " << INT16_MAX << endl
        << "long int min. = " << INT32_MIN
        << " max. = " << INT32_MAX << endl
        << "long long int min. = " << INT64_MIN
        << " max. = " << INT64_MAX << endl;

    cout 
        << "unsigned char max. = " << int(UINT8_MAX) << endl
        << "unsigned short int max. = " << UINT16_MAX << endl
        << "unsigned long int max. = " << UINT32_MAX << endl
        << "unsigned long long int max. = " << UINT64_MAX << endl;

    return 0;
}

サンプルコード2

#include <iostream>
#include <cstdint>

using namespace std;
int main(int argc, char** argv) {

    cout
        << "bytes of char = " << sizeof(char) << endl
        << "bytes of short = " << sizeof(short) << endl
        << "bytes of int = " << sizeof(int) << endl
        << "bytes of long = " << sizeof(long long) << endl;

    return 0;
}