2023.10.17 TIL
double에 int를 담는것 : O
int에 double을 담는것 : 버림
short(2Byte)에 int(4Byte)를 담는것 : △
short(2Byte)에 char(4Byte)를 담는것 : △
string에 int, double, short을 담는것 : 컴파일 X
char는 숫자인데, ASCII rule에 따라 문자로 변환해준다.
char c = 'A';
int num = c;
cout << c << endl; // A
cout << num << endl; // 65
int b = 10;
int a;
int c = a = b; // a = 10, c = 10
int num = 10;
double d = 100.0;
short s = 78;
char c = 4;
cout << num + d << endl; // 110.9
cout << num + s << endl; // 88
cout << num + c << endl; // 14
cout << s + c << endl; // int + int -> 82
암시적 형변환 (Implicit Type Conversion)
→ 작은 데이터타입을 큰 데이터타입으로 변환
명시적 형변환 (Explicit Type Conversion)
int a = 100;
int b = 200;
cout << b / a << endl; // 0
cout << static_cast<double>(a) / b << endl; // 0.5
cout << static_cast<double>(a / b) << endl; // 0
cout << (double)a / b << endl; // 0.5
int num = 100;
bool result1;
bool result2;
result1 = (num > 100);
result2 = (num <= 100);
cout << result1 << endl; // 0
cout << result2 << endl; // 1
int num = 100;
bool result1;
bool result2;
cout << boolalpha;
result1 = (num > 100);
result2 = (num <= 100);
cout << result1 << endl; // false
cout << result2 << endl; // true
short-circuit evaluation
bool result1 = true;
bool result2 = false;
bool result3 = false;
result1 || result2 || result3; // 컴퓨터가 result1 || result2 까지만 연산
(result1 || ((result2 = true) && (result3 = false))); // result1이 true, or연산자니까 바로 끝나버림
bool result1 = true;
bool result2 = false;
bool result3 = true;
cout << boolalpha;
(result1 || ((result2 = true) && (result3 = false)));
cout << result1 << " " << result2 << " " << result3 << endl;
// no short-circuit
// true true false
// short-circuit
// true false true
int n = 11 << 4; // shift left
int m = 11 >> 4; // shift right
cout << n << endl; // 176
cout << m << endl; // 2
int age = 17;
int price = (age >= 13) ? 27000 : 24000;
cout << price << endl; // 27000
: Byte 단위로 크기 반환
double d = 8;
int age = 17;
short s = 2;
char a = 1;
int array[100] = {0};
cout << sizeof(d) << endl; // 8
cout << sizeof(age) << endl; // 4
cout << sizeof(s) << endl; // 2
cout << sizeof(a) << endl; // 1
cout << sizeof(array) << endl; // 400
함수의 종류
https://en.cppreference.com/w/
함수의 정의와 호출
void printSt(string st) {
cout << st << endl;
}
int sum(int a, int b) {
int result = a + b;
return result;
}
int main() {
printSt("Hello");
cout << sum(2, 7) << endl;
}
main() 함수는 컴퓨터가 자동으로 호출함수의 선언
**int sum(int ,int); // parameter 이름은 생략 가능**
int main() {
cout << sum(100, 200) << endl;
}
int sum(int a, int b) {
return a + B;
}
int sum(int a, int b) {
int result = a + b;
a = 400;
b = 500;
cout << a << " " << b << " " << result << endl; // 400 500 300
return result;
}
int main() {
int a = 100;
int b = 200;
int result = sum(a, b); **// a, b 값을 복사해서 전달!**
cout << a << " " << b << " " << result << endl; // 100 200 300
}
Default Argument
int calcTotal(int food, int delivery = 3000) {
return food + delivery;
}
int main() {
cout << calcTotal(29000) << endl;
cout << calcTotal(12000) << endl;
cout << calcTotal(11000, 4200) << endl;
}
int getArea(int length) {
return length * length;
}
int getArea(int length, int width) {
return length * width;
}
int getArea(int length, int width, int heigth) {
return length * width * height;
}
int main() {
cout << getArea(10) << endl;
cout << getArea(10, 20) << endl;
cout << getArea(10, 20, 30) << endl;
}