c++ int 형 타입 정리(32bit, 64bit, MAX, MIN)
프로그래밍/c,c++ 2021. 12. 2. 17:35반응형
int 형 타입은 32bit, 64bit 에 따라, OS 에 따라 달라질수 있다.
가능하면 int32_t 등 bit 수를 명시하는 방법으로 코딩 하는 것도 괜찮은 방법.
32 bit linux | 64 bit linux |
char 1 bytes unsigned char 1 bytes ----------------------------------------------- short 2 bytes unsigned short 2 bytes ----------------------------------------------- int 4 bytes unsigned int 4 bytes ----------------------------------------------- long 4 bytes unsigned long 4 bytes ----------------------------------------------- long int 4 bytes unsigned long int 4 bytes ----------------------------------------------- long long 8 bytes unsigned long long 8 bytes ----------------------------------------------- long long int 8 bytes unsigned long long int 8 bytes ----------------------------------------------- int8_t 1 bytes uint8_t 1 bytes ----------------------------------------------- int16_t 2 bytes uint16_t 2 bytes ----------------------------------------------- int32_t 4 bytes uint32_t 4 bytes ----------------------------------------------- int64_t 8 bytes uint64_t 8 bytes CHAR_MIN 0 CHAR_MAX 255 ----------------------------------------------- UCHAR_MIN 0 UCHAR_MAX 255 ----------------------------------------------- SHRT_MIN -32768 SHRT_MAX 32767 ----------------------------------------------- USHRT_MIN 0 USHRT_MAX 65535 ----------------------------------------------- INT_MIN -2147483648 INT_MAX 2147483647 ----------------------------------------------- UINT_MIN 0 UINT_MAX 4294967295 ----------------------------------------------- LONG_MIN -2147483648 LONG_MAX 2147483647 ----------------------------------------------- ULONG_MIN 0 ULONG_MAX 4294967295 ----------------------------------------------- LLONG_MIN -9223372036854775808 LLONG_MAX 9223372036854775807 ----------------------------------------------- ULLONG_MIN 0 ULLONG_MAX 18446744073709551615 |
char 1 bytes unsigned char 1 bytes ----------------------------------------------- short 2 bytes unsigned short 2 bytes ----------------------------------------------- int 4 bytes unsigned int 4 bytes ----------------------------------------------- long 8 bytes unsigned long 8 bytes ----------------------------------------------- long int 8 bytes unsigned long int 8 bytes ----------------------------------------------- long long 8 bytes unsigned long long 8 bytes ----------------------------------------------- long long int 8 bytes unsigned long long int 8 bytes ----------------------------------------------- int8_t 1 bytes uint8_t 1 bytes ----------------------------------------------- int16_t 2 bytes uint16_t 2 bytes ----------------------------------------------- int32_t 4 bytes uint32_t 4 bytes ----------------------------------------------- int64_t 8 bytes uint64_t 8 bytes CHAR_MIN -128 CHAR_MAX 127 ----------------------------------------------- UCHAR_MIN 0 UCHAR_MAX 255 ----------------------------------------------- SHRT_MIN -32768 SHRT_MAX 32767 ----------------------------------------------- USHRT_MIN 0 USHRT_MAX 65535 ----------------------------------------------- INT_MIN -2147483648 INT_MAX 2147483647 ----------------------------------------------- UINT_MIN 0 UINT_MAX 4294967295 ----------------------------------------------- LONG_MIN -9223372036854775808 LONG_MAX 9223372036854775807 ----------------------------------------------- ULONG_MIN 0 ULONG_MAX 18446744073709551615 ----------------------------------------------- LLONG_MIN -9223372036854775808 LLONG_MAX 9223372036854775807 ----------------------------------------------- ULLONG_MIN 0 ULLONG_MAX 18446744073709551615 |
테스트 코드
#include <iostream>
#include <iomanip>
#include <climits>
int main() {
std::cout << std::left << std::setw(30) << "char" << sizeof(char) << " bytes" << std::endl;
std::cout << std::left << std::setw(30) << "unsigned char" << sizeof(unsigned char) << " bytes" << std::endl;
std::cout << "-----------------------------------------------" << std::endl;
std::cout << std::left << std::setw(30) << "short" << sizeof(short) << " bytes" << std::endl;
std::cout << std::left << std::setw(30) << "unsigned short" << sizeof(unsigned short) << " bytes" << std::endl;
std::cout << "-----------------------------------------------" << std::endl;
std::cout << std::left << std::setw(30) << "int" << sizeof(int) << " bytes" << std::endl;
std::cout << std::left << std::setw(30) << "unsigned int" << sizeof(unsigned int) << " bytes" << std::endl;
std::cout << "-----------------------------------------------" << std::endl;
std::cout << std::left << std::setw(30) << "long" << sizeof(long) << " bytes" << std::endl;
std::cout << std::left << std::setw(30) << "unsigned long" << sizeof(unsigned long) << " bytes" << std::endl;
std::cout << "-----------------------------------------------" << std::endl;
std::cout << std::left << std::setw(30) << "long int" << sizeof(long int) << " bytes" << std::endl;
std::cout << std::left << std::setw(30) << "unsigned long int" << sizeof(unsigned long int) << " bytes" << std::endl;
std::cout << "-----------------------------------------------" << std::endl;
std::cout << std::left << std::setw(30) << "long long" << sizeof(long long) << " bytes" << std::endl;
std::cout << std::left << std::setw(30) << "unsigned long long" << sizeof(unsigned long long) << " bytes" << std::endl;
std::cout << "-----------------------------------------------" << std::endl;
std::cout << std::left << std::setw(30) << "long long int" << sizeof(long long int) << " bytes" << std::endl;
std::cout << std::left << std::setw(30) << "unsigned long long int" << sizeof(unsigned long long int) << " bytes" << std::endl;
std::cout << "-----------------------------------------------" << std::endl;
std::cout << std::left << std::setw(30) << "int8_t" << sizeof(int8_t) << " bytes" << std::endl;
std::cout << std::left << std::setw(30) << "uint8_t" << sizeof(uint8_t) << " bytes" << std::endl;
std::cout << "-----------------------------------------------" << std::endl;
std::cout << std::left << std::setw(30) << "int16_t" << sizeof(int16_t) << " bytes" << std::endl;
std::cout << std::left << std::setw(30) << "uint16_t" << sizeof(uint16_t) << " bytes" << std::endl;
std::cout << "-----------------------------------------------" << std::endl;
std::cout << std::left << std::setw(30) << "int32_t" << sizeof(int32_t) << " bytes" << std::endl;
std::cout << std::left << std::setw(30) << "uint32_t" << sizeof(uint32_t) << " bytes" << std::endl;
std::cout << "-----------------------------------------------" << std::endl;
std::cout << std::left << std::setw(30) << "int64_t" << sizeof(int64_t) << " bytes" << std::endl;
std::cout << std::left << std::setw(30) << "uint64_t" << sizeof(uint64_t) << " bytes" << std::endl;
std::cout << std::endl << std::endl << std::endl << std::endl;
std::cout << std::left << std::setw(15) << "CHAR_MIN" << std::left << std::setw(25) << CHAR_MIN << std::endl << std::left << std::setw(16) << "CHAR_MAX" << std::left << std::setw(26) << CHAR_MAX << std::endl;
std::cout << "-----------------------------------------------" << std::endl;
std::cout << std::left << std::setw(15) << "UCHAR_MIN" << std::left << std::setw(25) << 0 << std::endl << std::left << std::setw(16) << "UCHAR_MAX" << std::left << std::setw(26) << UCHAR_MAX << std::endl;
std::cout << "-----------------------------------------------" << std::endl;
std::cout << std::left << std::setw(15) << "SHRT_MIN" << std::left << std::setw(25) << SHRT_MIN << std::endl << std::left << std::setw(16) << "SHRT_MAX" << std::left << std::setw(26) << SHRT_MAX << std::endl;
std::cout << "-----------------------------------------------" << std::endl;
std::cout << std::left << std::setw(15) << "USHRT_MIN" << std::left << std::setw(25) << 0 << std::endl << std::left << std::setw(16) << "USHRT_MAX" << std::left << std::setw(26) << USHRT_MAX << std::endl;
std::cout << "-----------------------------------------------" << std::endl;
std::cout << std::left << std::setw(15) << "INT_MIN" << std::left << std::setw(25) << INT_MIN << std::endl << std::left << std::setw(16) << "INT_MAX" << std::left << std::setw(26) << INT_MAX << std::endl;
std::cout << "-----------------------------------------------" << std::endl;
std::cout << std::left << std::setw(15) << "UINT_MIN" << std::left << std::setw(25) << 0 << std::endl << std::left << std::setw(16) << "UINT_MAX" << std::left << std::setw(26) << UINT_MAX << std::endl;
std::cout << "-----------------------------------------------" << std::endl;
std::cout << std::left << std::setw(15) << "LONG_MIN" << std::left << std::setw(25) << LONG_MIN << std::endl << std::left << std::setw(16) << "LONG_MAX" << std::left << std::setw(26) << LONG_MAX << std::endl;
std::cout << "-----------------------------------------------" << std::endl;
std::cout << std::left << std::setw(15) << "ULONG_MIN" << std::left << std::setw(25) << 0 << std::endl << std::left << std::setw(16) << "ULONG_MAX" << std::left << std::setw(26) << ULONG_MAX << std::endl;
std::cout << "-----------------------------------------------" << std::endl;
std::cout << std::left << std::setw(15) << "LLONG_MIN" << std::left << std::setw(25) << LLONG_MIN << std::endl << std::left << std::setw(16) << "LLONG_MAX" << std::left << std::setw(26) << LLONG_MAX << std::endl;
std::cout << "-----------------------------------------------" << std::endl;
std::cout << std::left << std::setw(15) << "ULLONG_MIN" << std::left << std::setw(25) << 0 << std::endl << std::left << std::setw(16) << "ULLONG_MAX" << std::left << std::setw(26) << ULLONG_MAX << std::endl;
return 0;
}
반응형