#include <iostream> // #include <stdio.h> //c style
using namespace std;
int main() {
cout << "Hello, World!" << endl; // printf("Hello, World!\n"); //c style
int a = 7, b = 3;
cout << "a = " << a << ", b = " << b << endl;
return 0;
}
cout
: 뒤에 " " 안에 있는 거 콘솔창에 출력하는 역할
endl
: 줄바꿈
std::
를 붙여줘야 문장이 정상적으로 컴파일 됨주석은 c랑 똑같다
//
랑 /* */
변수 이름 짓는 규칙
- 변수 이름은 영문자, 숫자, 언더바(__)만 사용가능
- 숫자를 변수 이름 맨앞에 쓸 수 없음. ex)1num; 안됨
- 타입 키워드 사용 안됨. ex)int float; 안됨
- 변수 이름은 대소문자를 구별한다. ex)mul;과 Mul;은 서로 다른 변수
- 변수명의 길이는 상관 없음. 그대로 붙여써도, 언더바를 활용해도 ok
(음의 정수, 0, 양의 정수)
#include <iostream>
#include <climits>
using namespace std;
int main() {
int n_int = INT_MAX;
short n_short = SHRT_MAX;
long n_long = LONG_MAX;
long long n_llong = LLONG_MAX;
cout << "int는 " << sizeof n_int << "바이트이다." << endl;
cout << "이 바이트의 최대값은 " << n_int << " 이다." << endl;
cout << endl;
cout << "short은 " << sizeof n_short << "바이트이다." << endl;
cout << "이 바이트의 최대값은 " << n_short << " 이다." << endl;
cout << endl;
cout << "long은 " << sizeof n_long << "바이트이다." << endl;
cout << "이 바이트의 최대값은 " << n_long << " 이다." << endl;
cout << endl;
cout << "long long은 " << sizeof n_llong << "바이트이다." << endl;
cout << "이 바이트의 최대값은 " << n_llong << " 이다." << endl;
return 0;
}
=================================
[결과]
int는 4바이트이다.
이 바이트의 최대값은 2147483647 이다.
short은 2바이트이다.
이 바이트의 최대값은 32767 이다.
long은 4바이트이다.
이 바이트의 최대값은 2147483647 이다.
long long은 8바이트이다.
이 바이트의 최대값은 9223372036854775807 이다.
short n_short = SHRT_MAX;
cout << "short은 " << sizeof n_short << "바이트이다." << endl;
cout << "이 바이트의 최대값은 " << n_short << " 이다." << endl;
[결과]
short은 2바이트이다.
이 바이트의 최대값은 32767 이다.
===============================
- short 자료형의 범위는 (최소값: -32768) ~ (최대값: 32767)이다. cf) window10 기준
- short 뿐만 아니라 int, long, long long 모두 '(음의 정수) 값을 저장할 수 없는 unsigned 개념'을 가지고 있다.
===============================
unsigned int a;
unsigned short b; // 0~65535 (음수쪽에 있던게 더해짐)
unsigned long c;
unsigned long long d;
unsigned short b = -1;
cout << b << endl; // 65535
unsigned short b = -2;
cout << b << endl; // 65534
- unsigned가 자료형 앞에 키워드로서 붙게 되면 음의 값을 저장할 수 없다.(= 저장할 필요 x)
- 이로 인해 변수형이 저장할 수 있는 최대값을 늘릴 수 있다.
- unsigned가 붙지 않은 short형 변수의 최대값은 32767이지만
unsigned가 붙은 short형 변수의 범위는 0 ~ 65535이다.
(-32768 ~ -1)만큼의 메모리 영역을 양의 영역에 더해준 것
signed와 unsigned
(음의 정수 값을 저장할 수 없는 unsigned 개념)
- unsigned를 통해 보다 넓은 범위의 정수값을 저장할 수 있음
- unsigned가 자료형 앞에 키워드로서 붙게 되면 음의 값을 저장할 수 x (저장할 필요 x)
: 소수부가 있는 수
#include <climits>
float a = 3.14; // 3.14
int b = 3.14; // 3 (실수 부분은 버려짐)
cout << a << " " << b << endl;
----------------
[결과]
3.14 3
// char : 작은 문자형
int a = 77;
char b = a;
cout << b << endl;
[결과]
M
-------------------
char b = 'a'; // 작은 따옴표(' ')로 써줘야함
cout << b << endl;
[결과]
a
bool a = 0;
bool b = 1;
bool c = 10;
cout << a << " " << b << " " << c << endl; // 0 1 1
bool
: 0 또는 1로만 저장한다 (0=false, 1=true)#include <iostream>
// #define PI = 3.1415926535 //c style
using namespace std;
int main(){
const float PI = 3.1415926535;
int r = 3;
float s = r * r * PI;
cout << s << endl; // 28.2743
}
const 자료형 상수이름 = 값;
상수이름은 암묵적으로 대문자
로 함
- 특정 데이터형의 변수에 다른 데이터형의 값을 대입했을 때
- 수식에 데이터형을 혼합하여 사용했을 때
- 함수에 매개변수를 전달할 때
// 데이터형 변환 (자동)
int a = 3.141592;
cout << a << endl; // 3
// 데이터형 변환 (강제)
// typeName(a) (typeName)a
char ch = 'M'; // 아스키 코드
cout << (int)ch << " " << int(ch) << endl; // 77 77
// C++에서의 데이터형 변환 연산자 (더 제한적)
// static_cast<typeName>
cout << static_cast<int>(ch) << endl; // 77 (위랑 동일함)
typeName(a) 또는 (typeName)a
static_cast<typeName>(a)
%: 부동 소수점(실수)은 사용 x
int a = 3 + 2;
cout << a << endl; // 5
int a = 10;
int b = 3;
int add = a + b;
int sub = a - b;
int mul = a * b;
int div = a / b;
int rem = a % b;
cout << "add: " << add << endl;
cout << "sub: " << sub << endl;
cout << "mul: " << mul << endl;
cout << "div: " << div << endl;
cout << "rem: " << rem << endl;
----------------------------------
[결과]
add: 13
sub: 7
mul: 30
div: 3
rem: 1
: 자동으로 변수형을 결정해줌
auto n = 100; // int
auto x = 1.5; // float
auto y = 1.3e12L; // long long
(C++의 auto가 java에서 var랑 비슷한 느낌인듯..?)