C++ 자료형

Dohyun Park·2023년 9월 26일
0

C++

목록 보기
3/3

Arithmetic Types

TypeBytesRangeFixed-width types
bool1true, false
char1implementation defined
signed char1-128 to 127int8_t
unsigned char10 to 255uint8_t
short2-2^15 to 2^15-1int16_t
unsigned short20 to 2^16-1uint16_t
int4-2^31 to 2^31-1int32_t
unsigned int40 to 2^32-1uint32_t
long int4/8int32_t/int64_t
long unsigned int4/8uint32_t/uint64_t
long long int8-2^63 to 2^63-1int64_t
long long unsigned int80 to 2^64-1uint64_t
float (IEEE 754)4±1.18 × 10^(-38) to ±3.4 × 10^(+38)
double (IEEE 754)8±2.23 × 10^(-308) to ±1.8 × 10^(+308)

Short Name

Signed TypeShort Name ⭐Unsigned TypeShort Name ⭐
signed char/unsigned char/
signed short intshortunsigned short intunsigned short
signed intintunsigned intunsigned
signed long intlongunsigned long intunsigned long
signed long long intlong longunsigned long long intunsigned long long

Suffix and Prefix(리터럴)

⚠️ unsigned int = 3; 과 같이, 리터럴을 붙이지 않는 경우 형변환이 일어나 int형의 3이 unsigned int형의 3으로 변환되게 되므로 주의해야한다.

TypeSUFFIXExampleNotes
int2리터럴 없음(작성 불필요)
unsigned intu, U3u
long inti, L8L
long unsignedul, UL2ul
long long intll, LL4ll
long long unsigned intull, ULL7ULL
floatf, F3.0fonly decimal numbers(10진법)
double3.0only decimal numbers(10진법)

.

RepresentationPREFIXExample
Binary(이진법) C++140b0b010101
Octal(팔진법)00307
Hexadecimal(16진법)0x or 0X0xFFA010

[참고] C++14는 가독성 개선을 위해 숫자 구분 기호를 넣는 것을 허용함

float pi = 3.141'592'654'5f;
//3단위 씩 끊어서 쓸 수 있음.

void type

void는 값이 없는 불완전한(정의되지 않은) 유형이다.

  • void는 반환 유형이 없거나 매개변수가 없는 함수도 나타낸다.
    - e.g: void f(), f(void)
    1. void 반환 유형(Return Type): C++에서 void는 함수가 값을 반환하지 않음을 나타내는 리턴 타입을 나타낸다.

    2. 매개변수 없는 함수(No Parameters): 매개변수를 가지지 않을 때

      void example_1() {
          // this function does not return any value!
      }
      
      int example_2() {
      		// this function does not require any parameter!
      }
  • C(gcc)에서 sizeof(void) == 1이지만, C++에선 sizeof(void)가 컴파일되지 않으니 주의해야함
    int main() {
    		//sizeof(void); //compile error
    }

nullptr keyword

C언어

int* p = NULL;

C++

int* p = nullptr;

참고

int* p1 = NULL;           // ok, equal to int* p1 = 0l
int* p2 = nullptr;        // ok, nullptr is a pointer not a number

int n1 = NULL;            // ok, we are assigning 0 to n1
// int n2 = nullptr;      // compile error we are assigning
//                           a null pointer to an integer variable

int *p2 = true ? 0 : nullptr;    // compile error
								 // incompatible types
profile
공부한 내용을 기록하고 생각을 정리합니다.

0개의 댓글