정수형

Jaemyeong Lee·2024년 7월 31일
0

FastCampusC++

목록 보기
2/78

정수형 데이터 타입 및 연산

C++에서 정수형 데이터 타입과 관련된 여러 가지 개념들을 다루는 프로그램입니다. 이 프로그램은 다양한 정수형 변수의 선언과 초기화, 리터럴, 자료형의 크기, 오버플로우와 언더플로우 등을 설명합니다.

#include <iostream>
#include <numeric> // 최대값, 최소값을 보기 위해 포함

using namespace std;

int main()
{
    {
        // 정수형 변수의 선언과 초기화
        int x;
        x = 0; // 변수 x를 0으로 초기화
        cout << x << endl; // x 값을 출력 (0)
    }

    {
        // 선언과 동시에 초기화
        // C 스타일 초기화
        int y = 1; // y를 1로 초기화
        cout << y;

        // C++ 스타일 초기화
        int z(2); // z를 2로 초기화
        cout << z << endl;
        // y와 z 값이 개행 없이 연달아 출력됨 (12)
    }

    {
        // 정수형의 소수점 버림
        int num0 = 1;
        int num1 = num0 * 1.5; // 1.5는 1로 변환되어 1
        int num2 = num0 / 2; // 0.5는 0으로 변환되어 0
        int num3 = 2.5; // 2.5는 2로 변환되어 2

        cout << "num0 " << num0 << endl; // 1
        cout << "num1 " << num1 << endl; // 1
        cout << "num2 " << num2 << endl; // 0
        cout << "num3 " << num3 << endl; // 2
    }

    {
        // 정수형 리터럴
        int num0 = 11;   // 일반 정수 리터럴
        int num1 = 011;  // 8진수 정수 리터럴 (8 + 1 = 9)
        int num2 = 0b11; // 2진수 정수 리터럴 (2 + 1 = 3)
        int num3 = 0x11; // 16진수 정수 리터럴 (16 + 1 = 17)

        cout << "num0 " << num0 << endl; // 11
        cout << "num1 " << num1 << endl; // 9
        cout << "num2 " << num2 << endl; // 3
        cout << "num3 " << num3 << endl; // 17
    }

    {
        // 자료형의 사이즈 출력
        // 환경에 따라 다른 결과 출력
        int intNum = 0;
        long longNum = 0;
        int* intPointer = &intNum;

        cout << sizeof(intNum) << endl; // int 크기 (4)
        cout << sizeof(longNum) << endl; // long 크기 (4 또는 8)
        cout << sizeof(intPointer) << endl; // 포인터 크기 (4 또는 8)
    }

    {
        // 고정폭 정수형
        int8_t num00 = 0;
        int16_t num11 = 0;
        int32_t num22 = 0;

        cout << sizeof(num00) << endl; // 1
        cout << sizeof(num11) << endl; // 2
        cout << sizeof(num22) << endl; // 4
    }

    {
        // 리터럴 접미사를 이용한 정수형 세부 타입
        int intNum_ = 11;
        unsigned int uintNum = 11u;
        long int longNum_ = 0L;
        long long longLongNum = 0LL;
        unsigned long long ulongLongNum = 0uLL;
    }

    {
        // unsigned 정수형의 최대값 출력
        cout << (int)numeric_limits<uint8_t>::max() << endl; // 255
        cout << numeric_limits<uint16_t>::max() << endl; // 65535
        cout << numeric_limits<uint32_t>::max() << endl; // 4294967295
        cout << numeric_limits<uint64_t>::max() << endl; // 18446744073709551615
    }

    {
        // signed 정수형의 최대값 출력
        cout << (int)numeric_limits<int8_t>::max() << endl; // 127
        cout << numeric_limits<int16_t>::max() << endl; // 32767
        cout << numeric_limits<int32_t>::max() << endl; // 2147483647
        cout << numeric_limits<int64_t>::max() << endl; // 9223372036854775807
    }

    {
        // signed 정수형의 최소값 출력
        cout << (int)numeric_limits<int8_t>::min() << endl; // -128
        cout << numeric_limits<int16_t>::min() << endl; // -32768
        cout << numeric_limits<int32_t>::min() << endl; // -2147483648
        cout << numeric_limits<int64_t>::min() << endl; // -9223372036854775808
    }

    {
        // unsigned 정수형의 오버플로우
        uint32_t unum32 = numeric_limits<uint32_t>::max();
        uint32_t unum32_ = unum32 + 1; // 오버플로우

        cout << unum32 << endl;  // 4294967295
        cout << unum32_ << endl; // 0
    }

    {
        // signed 정수형의 오버플로우
        int32_t num32 = numeric_limits<int32_t>::max();
        int32_t num32_ = num32 + 1; // 오버플로우

        cout << num32 << endl;  // 2147483647
        cout << num32_ << endl; // -2147483648
    }

    {
        // unsigned 정수형의 언더플로우
        uint32_t unum32 = numeric_limits<uint32_t>::min();
        uint32_t unum32_ = unum32 - 1; // 언더플로우

        cout << unum32 << endl;  // 0
        cout << unum32_ << endl; // 4294967295
    }

    {
        // signed 정수형의 언더플로우
        int32_t num32 = numeric_limits<int32_t>::min();
        int32_t num32_ = num32 - 1; // 언더플로우

        cout << num32 << endl;  // -2147483648
        cout << num32_ << endl; // 2147483647
    }
}

요약 및 이해

  1. 정수형 변수의 선언과 초기화:

    • int x; x = 0; 변수를 선언하고 초기화합니다.
    • int y = 1; int z(2); 선언과 동시에 초기화합니다.
  2. 소수점 버림:

    • 정수형 변수는 소수점을 버리고 정수 부분만 저장합니다.
  3. 정수형 리터럴:

    • 다양한 기수법(10진수, 8진수, 2진수, 16진수)으로 정수를 표현할 수 있습니다.
  4. 자료형의 크기:

    • sizeof 연산자를 사용하여 자료형의 크기를 확인할 수 있습니다.
  5. 고정폭 정수형:

    • int8_t, int16_t, int32_t 등 고정된 비트 수를 가지는 정수형을 사용할 수 있습니다.
  6. 리터럴 접미사:

    • u, L, LL 등의 접미사를 사용하여 정수형의 세부 타입을 지정할 수 있습니다.
  7. 최대값과 최소값:

    • numeric_limits를 사용하여 정수형의 최대값과 최소값을 확인할 수 있습니다.
  8. 오버플로우와 언더플로우:

    • 정수형 변수의 값이 범위를 벗어날 때 발생하는 현상을 오버플로우(overflow)와 언더플로우(underflow)라고 합니다.
profile
李家네_공부방

0개의 댓글