2.1 변수와 데이터 타입
예제 2.1: 정수 65를 메모리에 저장하고 출력
#include <iostream>
using namespace std;
int main() {
int value;
value = 65;
cout << value << endl;
return 0;
}
2.1.1 변수와 데이터 타입 개념
컴퓨터의 메모리는 데이터와 프로그램을 저장하는 공간입니다. 기본 단위는 바이트(1Byte=8bit)이며, 다음과 같은 단위들이 사용됩니다:
- 1KB(Kilobyte) = 1024Byte
- 1MB(Megabyte) = 1024KB
- 1GB(Gigabyte) = 1024MB
- 1TB(Terabyte) = 1024GB
- 1PB(Petabyte) = 1024TB
- 1EB(Exabyte) = 1024PB
- 1ZB(Zettabyte) = 1024EB
- 1YB(Yottabyte) = 1024ZB
- 1BB(Brontobyte) = 1024YB
고급 언어에서는 변수명을 통해 메모리 위치를 식별하며, 데이터 타입을 통해 해당 공간의 크기를 결정합니다. 예를 들어, int형은 4바이트 공간을 차지하며 -2147483648~2147483647 범위의 정수를 저장할 수 있습니다.
2.1.2 변수명 규칙
- 문자, 숫자, 밑줄(_)만 사용 가능
- 첫 문자는 숫자 불가
- C++ 키워드 사용 금지
- 대소문자 구분
2.1.3 변수 선언
변수를 사용하기 전에 타입과 이름을 명시하여 메모리 공간을 할당해야 합니다.
예제 2.4: 실수 65.5를 변수에 저장하고 출력
#include <iostream>
using namespace std;
int main() {
float value;
value = 65.5;
cout << value << endl;
return 0;
}
예제 2.5: 문자 'A'를 변수에 저장하고 출력
#include <iostream>
using namespace std;
int main() {
char character;
character = 'A';
cout << character << endl;
return 0;
}
2.2 할당문과 수학 표현식
예제 2.7: 반지름 7cm인 원의 넓이 계산
#include <iostream>
using namespace std;
int main() {
float radius = 7;
float area = 3.1415926 * radius * radius;
cout << "원의 넓이 = " << area << endl;
return 0;
}
2.2.1 할당문
변수에 값을 저장하는 문장으로, 기본 할당(=), 복합 할당(+=, -= 등)이 있습니다.
예제 2.8: 간단한 할당 연습
#include <iostream>
using namespace std;
int main() {
int count = 5;
cout << count << endl;
count = count + 2;
cout << count << endl;
count = count + 5;
cout << count << endl;
return 0;
}
2.2.2 증감 연산자
변수의 값을 1씩 증가/감소시키는 ++, -- 연산자가 있습니다. 증감 위치에 따라 동작이 달라집니다.
예제 2.11: 증감 연산자 차이 확인
#include <iostream>
using namespace std;
int main() {
int num1, num2 = 5;
num2++;
cout << num2 << endl;
++num2;
cout << num2 << endl;
num1 = num2++;
cout << "num1=" << num1 << " num2=" << num2 << endl;
num1 = ++num2;
cout << "num1=" << num1 << " num2=" << num2 << endl;
return 0;
}
2.3 데이터 타입 변환
예제 2.15: 삼각형의 밑변 23, 높이 51인 경우 넓이 계산
#include <iostream>
using namespace std;
int main() {
int base = 23, height = 51;
float area = base * height / 2.0;
cout << area << endl;
return 0;
}
2.3.3 문자형과 정수형 변환
문자형 데이터는 ASCII 코드로 저장됩니다. 'A'는 65, 'B'는 97로 저장됩니다.
예제 2.17: 문자를 정수형 변수에 할당
#include <iostream>
using namespace std;
int main() {
int code1, code2;
code1 = 'A';
code2 = 'B';
cout << code1 << " " << code2 << endl;
return 0;
}
2.4 데이터 입력
예제 2.19: 키보드로 세 자리 수를 입력받아 역순으로 출력
#include <iostream>
using namespace std;
int main() {
int input, digit1, digit2, digit3, reversed;
cin >> input;
digit1 = input / 100;
digit2 = (input - digit1 * 100) % 10;
digit3 = input % 10;
reversed = digit3 * 100 + digit2 * 10 + digit1;
cout << reversed << endl;
return 0;
}
2.5 C 언어 스타일 입출력
C++에서는 C 언어의 scanf, printf 함수도 사용할 수 있습니다. 특히 대량의 데이터 처리에는 cin/cout보다 효율적입니다.
예제 2.22: 다양한 형식의 정수 출력
#include <cstdio>
using namespace std;
int main() {
printf("%d%d%d\n", 9/8, 4*(6+3)%5, (4*6+3)%5);
printf("%d %d %d\n", 9/8, 4*(6+3)%5, (4*6+3)%5);
printf("9/8=%d 4*(6+3)%%5=%d (4*6+3)%%5=%d\n", 9/8, 4*(6+3)%5, (4*6+3)%5);
printf("%d %d %d\n", 41%6, 41%(-6), (-41)%6);
return 0;
}
2.6 순차 구조 프로그래밍 예제
예제 2.28: 닭과 토끼 문제
#include <iostream>
using namespace std;
int main() {
int heads = 35, legs = 94;
int chickens = 2 * heads - legs / 2;
int rabbits = legs / 2 - heads;
cout << "닭: " << chickens << ", 토끼: " << rabbits << endl;
return 0;
}