|
| 1 | +/* |
| 2 | + 홍정모 연구소 https://honglab.co.kr/ |
| 3 | +*/ |
| 4 | + |
| 5 | +#include <iostream> |
| 6 | + |
| 7 | +using namespace std; |
| 8 | + |
| 9 | +int main() |
| 10 | +{ |
| 11 | + // 변수를 정의할 때 자료형을 미리 지정해야 합니다. |
| 12 | + // 자료형은 바꿀 수 없습니다. |
| 13 | + |
| 14 | + // 내부적으로 메모리를 이미 갖고 있습니다. |
| 15 | + int i; // 변수 정의 |
| 16 | + i = 123; // 변수에 값 지정 (객체 레퍼런스 아님) |
| 17 | + |
| 18 | + // sizeof 소개 |
| 19 | + cout << i << " " << sizeof(i) << endl; // 추측해보세요. |
| 20 | + |
| 21 | + float f = 123.456f; // 마지막 f 주의 |
| 22 | + double d = 123.456; // f 불필요 |
| 23 | + |
| 24 | + cout << f << " " << sizeof(f) << endl; // 123.456 4 |
| 25 | + cout << d << " " << sizeof(d) << endl; // 123.456 8 |
| 26 | + |
| 27 | + // C++는 글자 하나와 문자열을 구분합니다. |
| 28 | + char c = 'a'; |
| 29 | + |
| 30 | + cout << c << " " << sizeof(c) << endl; // a 1 |
| 31 | + |
| 32 | + // 그 외에도 다양한 자료형이 존재합니다. |
| 33 | + |
| 34 | + // 형변환 |
| 35 | + i = 987.654; // double을 int에 강제로 저장 |
| 36 | + |
| 37 | + cout << "int from double " << i << endl; // 추측해보세요. |
| 38 | + |
| 39 | + f = 567.89; // 이것도 형변환 |
| 40 | + |
| 41 | + // 기본 연산자 |
| 42 | + |
| 43 | + // i = 987; |
| 44 | + i += 100; // i = i + 100; |
| 45 | + i++; // i = i + 1; |
| 46 | + |
| 47 | + cout << i << endl; // 추측해보세요. |
| 48 | + |
| 49 | + // 불리언 |
| 50 | + bool is_good = true; |
| 51 | + is_good = false; |
| 52 | + |
| 53 | + cout << is_good << endl; // 0 |
| 54 | + |
| 55 | + cout << boolalpha << true << endl; // true |
| 56 | + cout << is_good << endl; // false |
| 57 | + cout << noboolalpha << true << endl; // true |
| 58 | + |
| 59 | + // 논리 연산 몇 가지 소개 (참고 문서 사용) |
| 60 | + // https://en.cppreference.com/w/cpp/language/operator_precedence |
| 61 | + |
| 62 | + cout << boolalpha; |
| 63 | + cout << (true && true) << endl; // 추측해보세요. |
| 64 | + cout << (true || false) << endl; // 추측해보세요. |
| 65 | + |
| 66 | + // 비교 |
| 67 | + |
| 68 | + cout << (1 > 3) << endl; |
| 69 | + cout << (3 == 3) << endl; |
| 70 | + cout << (i >= 3) << endl; |
| 71 | + cout << ('a' != 'c') << endl; |
| 72 | + cout << ('a' != 'c') << endl; |
| 73 | + |
| 74 | + // 영역(scope) |
| 75 | + |
| 76 | + i = 123; // 더 넓은 영역 |
| 77 | + |
| 78 | + { |
| 79 | + int i = 345; // <- 더 좁은 영역의 다른 변수 |
| 80 | + cout << i << endl; // 추측해보세요. |
| 81 | + } |
| 82 | + |
| 83 | + cout << i << endl; // 추측해보세요. |
| 84 | + |
| 85 | + return 0; |
| 86 | +} |
0 commit comments