내장 자료형 (built-in data types) : 기본 자료형, 복합 자료형
사용자 정의 자료형 : Enumerated (열거형), 클래스
C++에서는 short int(또는 short), int, long int(또는 long)(4바이트 혹은 8바이트의 크기를 가짐)의 3개의 정수 자료형 존재
int main ()
{
cout << "Size of short int is " << sizeof (short int) << " bytes." << endl;
cout << "Size of int is " << sizeof (int) << " bytes." << endl;
cout << "Size of long int is " << sizeof (long int) << " bytes." << endl;
return 0;
}
Run:
Size of short int: 2 bytes.
Size of int: 4 bytes.
Size of long int: 4 bytes.
어떤 자료형의 값을 고정하여 사용할 때 이를 리터럴이라고 부름. 이는 값이 변경되지 않음
1234 // The system uses signed integer
1234U // The system uses unsigned integer
1234L // The system uses signed long int
1234UL // The system uses unsigned long int
C++의 문자 자료형의 크기는 1바이트이며 부호가 없음
char
을 사용하여 선언
문자는 2가지 형태로 리터럴로 사용 가능
1. ASCII 테이블에 정의된 글자를 작은 따옴표 안에 넣어 사용
2. ASCII 테이블에 정의된 정수 값을 사용 (0~127)
int main () {
// Defining and initializing some variables of char type
char first = 'A';
char second = 65;
char third = 'B';
char fourth = 66;
// Printing values cout << "Value of first: " << first << endl;
cout << "Value of second: " << second << endl;
cout << "Value of third: " << third << endl;
cout << "Value of fourth: " << fourth;
return 0;
}
Run:
Value of first: A
Value of second: A
Value of third: B
Value of fourth: B
char에 아무런 값을 할당하지 않으면 널문자(‘\0’, NULL, 0)로 초기화 된다.
이는 “빈문자열”이 아니다.
널문자는 문자열의 끝을 나타내는데 사용됩니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
char a = '\0';
char b = 0;
char c = NULL;
// 3가지 다 ASCII 값 0을 뜻하므로 동문자임
string d = "";
cout << "hello";
if (a == b)
cout << 1;
if (b == c)
cout << 2;
// 빈 문자열은 아니다.
if (string(1, a) == d)
cout << 3;
return 0;
}
hello12
일부 특수문자등을 표현하기 위해서는 이스케이프 문자()를 활용한다.
불은 1바이트(8비트)로 메모리에 저장된다.
C++에서는 소숫점을 갖는 숫자를 부동 소숫점(floating-point)라고 한다.
계산을 효율적으로 할 수 있게 float, double, long double 이라는 3가지 종류의 부동 소숫점을 정의한다.
모두 부호가 있는 숫자이다.
void는 값이 없음을 나타내는 특별한 자료형이다.
Null 문자로 끝나는 문자들의 집합이다.
- "John" => 'J' 'o' 'h' 'n' '/0'
C++은 사용자 정의 자료형인 클래스로 새로운 문자열 자료형을 제공한다. => <string>