소개
이 랩에서는 C++ 에서 변수와 데이터 유형을 사용하는 방법을 배우게 됩니다. 다양한 정수 변수 크기를 살펴보고, float 및 double 변수를 초기화하며, 문자 및 문자열 변수를 선언하고, 타입 캐스팅 (type casting) 을 수행하고, 상수를 정의하며, 부울 (boolean) 변수를 사용하고, 다양한 데이터 유형의 메모리 크기를 확인합니다. 또한 정수 오버플로우 (integer overflow) 상황을 처리하는 방법도 배우게 됩니다. 이 실습 경험은 C++ 프로그래밍 프로젝트에서 데이터를 관리하는 데 필요한 탄탄한 기반을 제공할 것입니다.
다양한 크기의 정수 변수 (short, int, long) 선언
이 단계에서는 C++ 에서 다양한 정수 변수 유형과 메모리 크기가 다른 변수를 선언하는 방법을 배우게 됩니다. C++ 는 데이터에 가장 적합한 저장 공간을 선택할 수 있도록 여러 정수 유형을 제공합니다.
WebIDE 를 열고 ~/project 디렉토리에 integer_variables.cpp라는 새 파일을 만듭니다.
touch ~/project/integer_variables.cpp
integer_variables.cpp 파일에 다음 코드를 추가합니다.
#include <iostream>
int main() {
// Declaring short integer (typically 2 bytes)
short smallNumber = 32767;
// Declaring standard integer (typically 4 bytes)
int regularNumber = 2147483647;
// Declaring long integer (typically 4 or 8 bytes)
long largeNumber = 9223372036854775807L;
// Printing the values of different integer types
std::cout << "Short Integer: " << smallNumber << std::endl;
std::cout << "Regular Integer: " << regularNumber << std::endl;
std::cout << "Long Integer: " << largeNumber << std::endl;
return 0;
}
정수 유형을 자세히 살펴보겠습니다.
short:- 가장 작은 정수 유형
- 일반적으로 2 바이트의 메모리를 사용합니다.
- 범위: -32,768 ~ 32,767
int:- 표준 정수 유형
- 일반적으로 4 바이트의 메모리를 사용합니다.
- 범위: -2,147,483,648 ~ 2,147,483,647
long:- 더 큰 정수 유형
- 시스템에 따라 4 또는 8 바이트일 수 있습니다.
- 범위: -9,223,372,036,854,775,808 ~ 9,223,372,036,854,775,807
프로그램을 컴파일하고 실행합니다.
g++ integer_variables.cpp -o integer_variables
./integer_variables
예시 출력:
Short Integer: 32767
Regular Integer: 2147483647
Long Integer: 9223372036854775807
기억해야 할 주요 사항:
- 저장해야 하는 값의 범위에 따라 정수 유형을 선택합니다.
- long 정수의
L접미사는 올바른 유형 해석을 보장합니다. - 시스템에 따라 이러한 유형의 메모리 크기가 약간 다를 수 있습니다.
정수 유형과 해당 범위를 이해하는 것은 C++ 프로그래밍의 기본입니다. 이러한 유형은 다양한 범위의 정수를 효율적으로 저장하기 위해 서로 다른 메모리 할당을 제공합니다. 이러한 유형이 더 넓은 C++ 유형 시스템에 어떻게 적합한지 시각화해 보겠습니다.
graph LR
A[C++ Data Types] --> B[Fundamental Types]
A --> C[Derived Types]
B --> D[Integer Types]
B --> E[Floating-Point Types]
B --> F[Character Types]
B --> G[Boolean]
D --> D1[short]
D --> D2[int]
D --> D3[long]
D --> D4[long long]
E --> E1[float]
E --> E2[double]
E --> E3[long double]
F --> F1[char]
F --> F2[wchar_t]
C --> H[Arrays]
C --> I[Pointers]
C --> J[References]
C --> K[std::string]
style A fill:#f9f,stroke:#333,stroke-width:2px
style B fill:#bbf,stroke:#333
style C fill:#bbf,stroke:#333
다음 단계에서는 부동 소수점 유형, 문자 유형 및 부울 유형에 대해 배우게 됩니다.
소수점이 있는 부동소수점 변수 (float, double) 초기화
이 단계에서는 C++ 의 부동 소수점 변수, 특히 소수를 사용할 수 있는 float 및 double 유형에 대해 배우게 됩니다. 이러한 유형은 분수 부분이 있는 숫자를 나타내는 데 필수적입니다.
WebIDE 를 열고 ~/project 디렉토리에 floating_point.cpp라는 새 파일을 만듭니다.
touch ~/project/floating_point.cpp
floating_point.cpp 파일에 다음 코드를 추가합니다.
#include <iostream>
#include <iomanip>
int main() {
// Declaring and initializing float variables
float smallDecimal = 3.14f; // 'f' suffix indicates float
float scientificNotation = 2.5e3f; // 2.5 × 10^3 = 2500.0
// Declaring and initializing double variables
double preciseDecimal = 3.14159265359;
double largeDecimal = 1.23456789e10; // 12,345,678,900.0
// Setting precision for decimal output
std::cout << std::fixed << std::setprecision(4);
// Printing float values
std::cout << "Float Values:" << std::endl;
std::cout << "Small Decimal: " << smallDecimal << std::endl;
std::cout << "Scientific Notation: " << scientificNotation << std::endl;
// Printing double values
std::cout << "\nDouble Values:" << std::endl;
std::cout << "Precise Decimal: " << preciseDecimal << std::endl;
std::cout << "Large Decimal: " << largeDecimal << std::endl;
return 0;
}
부동 소수점 유형을 자세히 살펴보겠습니다.
float:- 단정밀도 부동 소수점 유형
- 일반적으로 4 바이트의 메모리를 사용합니다.
- 덜 정확하며, 기본적인 소수 계산에 적합합니다.
- 초기화 시 'f' 접미사를 사용합니다.
double:- 배정밀도 부동 소수점 유형
- 일반적으로 8 바이트의 메모리를 사용합니다.
- 더 정확하며, 대부분의 소수 계산에 선호됩니다.
- 더 크고 정확한 소수를 나타낼 수 있습니다.
프로그램을 컴파일하고 실행합니다.
g++ floating_point.cpp -o floating_point
./floating_point
예시 출력:
Float Values:
Small Decimal: 3.1416
Scientific Notation: 2500.0000
Double Values:
Precise Decimal: 3.1416
Large Decimal: 12345678900.0000
기억해야 할 주요 사항:
- 작고 덜 정확한 소수의 경우
float를 사용합니다. - 더 정확한 계산의 경우
double을 사용합니다. - 'f' 접미사는 float 리터럴에 중요합니다.
- 과학적 표기법을 사용하면 매우 크거나 작은 숫자를 나타낼 수 있습니다.
std::fixed및std::setprecision()은 소수점 출력을 제어하는 데 도움이 됩니다.
싱글 쿼테이션으로 문자 변수 선언 및 초기화
이 단계에서는 C++ 의 문자 변수에 대해 배우게 됩니다. 문자 변수는 단일 문자를 저장하는 데 사용되며 작은따옴표를 사용하여 선언됩니다. 문자는 개별 문자, 숫자 또는 기호를 나타내는 기본적인 데이터 유형입니다.
WebIDE 를 열고 ~/project 디렉토리에 character_variables.cpp라는 새 파일을 만듭니다.
touch ~/project/character_variables.cpp
character_variables.cpp 파일에 다음 코드를 추가합니다.
#include <iostream>
int main() {
// Declaring and initializing character variables
char letter = 'A';
char number = '7';
char symbol = '$';
// Printing character values
std::cout << "Letter: " << letter << std::endl;
std::cout << "Number: " << number << std::endl;
std::cout << "Symbol: " << symbol << std::endl;
// Demonstrating character arithmetic
char nextLetter = letter + 1;
std::cout << "Next Letter: " << nextLetter << std::endl;
// ASCII value of characters
std::cout << "ASCII value of 'A': " << (int)letter << std::endl;
return 0;
}
문자 변수를 자세히 살펴보겠습니다.
선언:
char키워드를 사용합니다.- 항상 작은따옴표
''로 초기화됩니다. - 단일 문자를 저장할 수 있습니다.
문자 유형:
- 문자: 'A', 'b', 'Z'
- 숫자: '0', '7', '9'
- 기호: '$', '@', '#'
문자 산술:
- 정수 연산을 사용하여 문자를 조작할 수 있습니다.
- 각 문자는 기본 ASCII 값을 갖습니다.
프로그램을 컴파일하고 실행합니다.
g++ character_variables.cpp -o character_variables
./character_variables
예시 출력:
Letter: A
Number: 7
Symbol: $
Next Letter: B
ASCII value of 'A': 65
기억해야 할 주요 사항:
- 문자 리터럴에는 작은따옴표
''를 사용합니다. - 문자는 숫자 값 (ASCII) 으로 저장됩니다.
- 문자를 사용하여 산술 연산을 수행할 수 있습니다.
- 각 문자는 1 바이트의 메모리를 차지합니다.
std::string 을 사용하여 문자열 변수 생성
이 단계에서는 C++ 에서 std::string 클래스를 사용하여 문자열 변수를 생성하고 조작하는 방법을 배우게 됩니다. 문자열은 문자 배열에 비해 텍스트를 더 쉽게 처리할 수 있도록 해주는 문자 시퀀스입니다.
WebIDE 를 열고 ~/project 디렉토리에 string_variables.cpp라는 새 파일을 만듭니다.
touch ~/project/string_variables.cpp
string_variables.cpp 파일에 다음 코드를 추가합니다.
#include <iostream>
#include <string>
int main() {
// Declaring and initializing string variables
std::string greeting = "Hello, World!";
std::string name = "John Doe";
std::string empty_string;
// Printing string variables
std::cout << "Greeting: " << greeting << std::endl;
std::cout << "Name: " << name << std::endl;
// String concatenation
std::string welcome = greeting + " Welcome, " + name;
std::cout << "Welcome Message: " << welcome << std::endl;
// String length
std::cout << "Greeting length: " << greeting.length() << std::endl;
// Accessing individual characters
std::cout << "First character of name: " << name[0] << std::endl;
// Modifying strings
name = "Jane Smith";
std::cout << "Updated Name: " << name << std::endl;
return 0;
}
문자열 연산을 자세히 살펴보겠습니다.
선언:
- 문자열 기능을 사용하려면
#include <string>을 사용합니다. std::string키워드로 선언합니다.- 텍스트로 초기화하거나 비워둘 수 있습니다.
- 문자열 기능을 사용하려면
문자열 연산:
+연산자를 사용한 연결 (Concatenation).length()메서드를 사용하여 길이 가져오기[]인덱스를 사용하여 문자 접근
프로그램을 컴파일하고 실행합니다.
g++ string_variables.cpp -o string_variables
./string_variables
예시 출력:
Greeting: Hello, World!
Name: John Doe
Welcome Message: Hello, World! Welcome, John Doe
Greeting length: 13
First character of name: J
Updated Name: Jane Smith
기억해야 할 주요 사항:
std::string은 문자 배열보다 더 유연합니다.- 문자열은 쉽게 수정하고 조작할 수 있습니다.
- 문자열 크기를 얻으려면
.length()를 사용합니다. - 문자열은
+연산자로 연결할 수 있습니다.
타입 캐스팅을 사용하여 다양한 숫자 타입 변환
이 단계에서는 C++ 에서 다양한 숫자 유형 간에 값을 변환할 수 있게 해주는 타입 캐스팅에 대해 배우게 됩니다. 타입 캐스팅은 원래 값을 잃지 않고 변수의 데이터 유형을 변경해야 할 때 필수적입니다.
WebIDE 를 열고 ~/project 디렉토리에 type_casting.cpp라는 새 파일을 만듭니다.
touch ~/project/type_casting.cpp
type_casting.cpp 파일에 다음 코드를 추가합니다.
#include <iostream>
int main() {
// Implicit type casting (automatic conversion)
int intValue = 10;
double doubleValue = intValue; // Automatically converts int to double
std::cout << "Implicit Conversion (int to double): " << doubleValue << std::endl;
// Explicit type casting (manual conversion)
double pi = 3.14159;
int truncatedPi = (int)pi; // C-style cast, truncates decimal part
std::cout << "Explicit Conversion (double to int): " << truncatedPi << std::endl;
// Static cast for type conversion
float floatValue = 7.5f;
int roundedValue = static_cast<int>(floatValue);
std::cout << "Static Cast (float to int): " << roundedValue << std::endl;
// Conversion between numeric types with potential data loss
long largeNumber = 1000000;
short smallNumber = static_cast<short>(largeNumber);
std::cout << "Large to Small Type Conversion: " << smallNumber << std::endl;
// Mixing different numeric types in calculations
int a = 5;
double b = 2.5;
double result = a + b; // Implicit conversion of int to double
std::cout << "Mixed Type Calculation: " << result << std::endl;
return 0;
}
타입 캐스팅을 자세히 살펴보겠습니다.
암시적 캐스팅 (Implicit Casting):
- 호환 가능한 유형 간의 자동 변환
- 프로그래머의 개입 없이 발생합니다.
- 일반적으로 더 큰 유형으로 변환할 때 안전합니다.
명시적 캐스팅 (Explicit Casting):
- 수동 타입 변환
static_cast<>()또는 C 스타일(type)구문을 사용합니다.- 데이터 손실 또는 예상치 못한 결과를 초래할 수 있습니다.
프로그램을 컴파일하고 실행합니다.
g++ type_casting.cpp -o type_casting
./type_casting
예시 출력:
Implicit Conversion (int to double): 10
Explicit Conversion (double to int): 3
Static Cast (float to int): 7
Large to Small Type Conversion: 16960
Mixed Type Calculation: 7.5
기억해야 할 주요 사항:
- 더 작은 유형으로 캐스팅할 때는 주의하십시오.
- 더 안전하고 명시적인 변환을 위해
static_cast<>()를 사용하십시오. - 변환 중 잠재적인 데이터 손실을 이해하십시오.
- 암시적 변환은 계산에서 자동으로 발생할 수 있습니다.
const 키워드를 사용하여 상수 정의
이 단계에서는 const 키워드를 사용하여 C++ 에서 상수를 정의하는 방법을 배우게 됩니다. 상수는 초기화 후 변경할 수 없는 값으로, 의도하지 않은 수정으로부터 데이터를 보호하는 불변 변수를 생성하는 방법을 제공합니다.
WebIDE 를 열고 ~/project 디렉토리에 constants.cpp라는 새 파일을 만듭니다.
touch ~/project/constants.cpp
constants.cpp 파일에 다음 코드를 추가합니다.
#include <iostream>
int main() {
// Defining constants with const keyword
const int MAX_USERS = 100;
const double PI = 3.14159;
const char GRADE_SEPARATOR = '-';
// Demonstrating constant usage
std::cout << "Maximum Users: " << MAX_USERS << std::endl;
std::cout << "Value of PI: " << PI << std::endl;
std::cout << "Grade Separator: " << GRADE_SEPARATOR << std::endl;
// Naming convention: constants typically use UPPERCASE
const int DAYS_IN_WEEK = 7;
const std::string WELCOME_MESSAGE = "Welcome to C++ Programming!";
std::cout << "Days in a Week: " << DAYS_IN_WEEK << std::endl;
std::cout << "Welcome Message: " << WELCOME_MESSAGE << std::endl;
// Attempting to modify a constant (will cause a compilation error)
// Uncomment the next line to see the error
// MAX_USERS = 200; // This would cause a compile-time error
return 0;
}
상수를 자세히 살펴보겠습니다.
선언:
- 유형 앞에
const키워드를 사용합니다. - 선언 시 초기화해야 합니다.
- 초기화 후에는 수정할 수 없습니다.
- 유형 앞에
모범 사례:
- 상수 이름에 대문자를 사용합니다.
- 모든 데이터 유형에서 작동합니다.
- 우발적인 수정을 방지하는 데 도움이 됩니다.
프로그램을 컴파일하고 실행합니다.
g++ constants.cpp -o constants
./constants
예시 출력:
Maximum Users: 100
Value of PI: 3.14159
Grade Separator: -
Days in a Week: 7
Welcome Message: Welcome to C++ Programming!
기억해야 할 주요 사항:
const는 불변 변수를 생성합니다.- 상수는 선언 시 초기화되어야 합니다.
- 코드를 더 읽기 쉽고 안전하게 만드는 데 도움이 됩니다.
- 중요한 값의 의도하지 않은 변경을 방지합니다.
참/거짓 조건을 위한 불리언 변수 사용
이 단계에서는 참 또는 거짓 값을 나타내는 C++ 의 부울 변수에 대해 배우게 됩니다. 부울은 조건부 논리를 생성하고 프로그램에서 결정을 내리는 데 필수적입니다.
WebIDE 를 열고 ~/project 디렉토리에 boolean_variables.cpp라는 새 파일을 만듭니다.
touch ~/project/boolean_variables.cpp
#include <iostream>
int main() {
// Declaring boolean variables
bool isStudent = true;
bool hasPassedExam = false;
// Printing boolean values
std::cout << "Is Student: " << std::boolalpha << isStudent << std::endl;
std::cout << "Passed Exam: " << hasPassedExam << std::endl;
// Comparison operations that result in boolean values
int age = 20;
bool isAdult = (age >= 18);
std::cout << "Is Adult: " << isAdult << std::endl;
// Logical operations
bool hasScholarship = true;
bool canEnroll = isStudent && isAdult;
std::cout << "Can Enroll: " << canEnroll << std::endl;
// Negation
bool isUnemployed = !hasPassedExam;
std::cout << "Is Unemployed: " << isUnemployed << std::endl;
// Conditional statement using boolean
if (isStudent && hasPassedExam) {
std::cout << "Congratulations! You can proceed to the next level." << std::endl;
} else {
std::cout << "You need to improve your academic performance." << std::endl;
}
return 0;
}
부울 변수를 자세히 살펴보겠습니다.
선언:
bool키워드를 사용합니다.true또는false만 가능합니다.- 조건부 논리에 유용합니다.
연산:
- 비교 연산자는 부울 결과를 생성합니다.
- 논리 AND
&& - 논리 OR
|| - 부정
!
프로그램을 컴파일하고 실행합니다.
g++ boolean_variables.cpp -o boolean_variables
./boolean_variables
예시 출력:
Is Student: true
Passed Exam: false
Is Adult: true
Can Enroll: true
Is Unemployed: true
You need to improve your academic performance.
기억해야 할 주요 사항:
- 부울은 참/거짓 조건을 나타냅니다.
- 비교 및 논리 연산에 사용됩니다.
- 프로그램에서 의사 결정에 필수적입니다.
std::boolalpha는 1/0 대신 "true"/"false"를 출력합니다.
sizeof() 연산자를 사용하여 다양한 데이터 유형의 메모리 크기 확인
이 단계에서는 C++ 의 sizeof() 연산자에 대해 배우게 됩니다. 이 연산자를 사용하면 다양한 데이터 유형의 메모리 크기를 확인할 수 있습니다. 메모리 할당을 이해하는 것은 효율적인 프로그래밍과 메모리 관리에 매우 중요합니다.
WebIDE 를 열고 ~/project 디렉토리에 sizeof_operator.cpp라는 새 파일을 만듭니다.
touch ~/project/sizeof_operator.cpp
#include <iostream>
int main() {
// Checking memory size of integer types
std::cout << "Integer Types Memory Size:" << std::endl;
std::cout << "short: " << sizeof(short) << " bytes" << std::endl;
std::cout << "int: " << sizeof(int) << " bytes" << std::endl;
std::cout << "long: " << sizeof(long) << " bytes" << std::endl;
std::cout << "long long: " << sizeof(long long) << " bytes" << std::endl;
// Checking memory size of floating-point types
std::cout << "\nFloating-Point Types Memory Size:" << std::endl;
std::cout << "float: " << sizeof(float) << " bytes" << std::endl;
std::cout << "double: " << sizeof(double) << " bytes" << std::endl;
std::cout << "long double: " << sizeof(long double) << " bytes" << std::endl;
// Checking memory size of character and boolean types
std::cout << "\nOther Types Memory Size:" << std::endl;
std::cout << "char: " << sizeof(char) << " bytes" << std::endl;
std::cout << "bool: " << sizeof(bool) << " bytes" << std::endl;
// Checking memory size of specific variables
int intVar = 42;
double doubleVar = 3.14;
char charVar = 'A';
std::cout << "\nVariable Memory Size:" << std::endl;
std::cout << "intVar: " << sizeof(intVar) << " bytes" << std::endl;
std::cout << "doubleVar: " << sizeof(doubleVar) << " bytes" << std::endl;
std::cout << "charVar: " << sizeof(charVar) << " bytes" << std::endl;
return 0;
}
sizeof() 연산자를 자세히 살펴보겠습니다.
목적:
- 데이터 유형의 메모리 크기를 결정합니다.
- 바이트 단위로 크기를 반환합니다.
- 메모리 할당을 이해하는 데 유용합니다.
사용법:
- 데이터 유형과 함께 사용할 수 있습니다.
- 변수와 함께 사용할 수 있습니다.
- 메모리 요구 사항을 이해하는 데 도움이 됩니다.
프로그램을 컴파일하고 실행합니다.
g++ sizeof_operator.cpp -o sizeof_operator
./sizeof_operator
예시 출력:
Integer Types Memory Size:
short: 2 bytes
int: 4 bytes
long: 8 bytes
long long: 8 bytes
Floating-Point Types Memory Size:
float: 4 bytes
double: 8 bytes
long double: 16 bytes
Other Types Memory Size:
char: 1 bytes
bool: 1 bytes
Variable Memory Size:
intVar: 4 bytes
doubleVar: 8 bytes
charVar: 1 bytes
기억해야 할 주요 사항:
sizeof()는 바이트 단위로 메모리 크기를 반환합니다.- 메모리 크기는 시스템에 따라 다를 수 있습니다.
- 데이터 유형의 메모리 요구 사항을 이해하는 데 도움이 됩니다.
- 낮은 수준의 메모리 관리에 유용합니다.
정수 오버플로우 상황 처리
이 단계에서는 정수 오버플로우에 대해 배우게 됩니다. 정수 오버플로우는 계산 결과가 정수 유형의 최대 한계를 초과하는 상황입니다. 견고하고 신뢰할 수 있는 C++ 프로그램을 작성하려면 오버플로우를 이해하고 방지하는 것이 중요합니다.
WebIDE 를 열고 ~/project 디렉토리에 integer_overflow.cpp라는 새 파일을 만듭니다.
touch ~/project/integer_overflow.cpp
#include <iostream>
#include <limits>
int main() {
// Demonstrating integer overflow with short int
short smallInt = 32767; // Maximum value for short
std::cout << "Original Value: " << smallInt << std::endl;
// Overflow occurs when incrementing beyond maximum
smallInt++;
std::cout << "After Overflow: " << smallInt << std::endl;
// Using unsigned integers to prevent negative overflow
unsigned int positiveOnly = 0;
std::cout << "Unsigned Integer Start: " << positiveOnly << std::endl;
// Decrementing unsigned integer causes wrap-around
positiveOnly--;
std::cout << "Unsigned Integer Wrap-around: " << positiveOnly << std::endl;
// Checking integer limits
std::cout << "\nInteger Type Limits:" << std::endl;
std::cout << "Short Max: " << std::numeric_limits<short>::max() << std::endl;
std::cout << "Short Min: " << std::numeric_limits<short>::min() << std::endl;
// Safe increment method
try {
if (smallInt < std::numeric_limits<short>::max()) {
smallInt++;
std::cout << "Safe Increment: " << smallInt << std::endl;
} else {
std::cout << "Cannot increment further" << std::endl;
}
} catch (const std::overflow_error& e) {
std::cout << "Overflow Error: " << e.what() << std::endl;
}
return 0;
}
정수 오버플로우를 자세히 살펴보겠습니다.
오버플로우 특성:
- 값이 유형의 최대 한계를 초과할 때 발생합니다.
- 예상치 못한 결과를 초래할 수 있습니다.
- 부호 있는 (signed) 및 부호 없는 (unsigned) 유형에 대해 다른 동작을 보입니다.
예방 전략:
- 계산 전에 한계를 확인합니다.
- 더 큰 정수 유형을 사용합니다.
- 음수가 아닌 값에 부호 없는 유형을 사용합니다.
- 범위 검사를 구현합니다.
프로그램을 컴파일하고 실행합니다.
g++ integer_overflow.cpp -o integer_overflow
./integer_overflow
예시 출력:
Original Value: 32767
After Overflow: -32768
Unsigned Integer Start: 0
Unsigned Integer Wrap-around: 4294967295
Integer Type Limits:
Short Max: 32767
Short Min: -32768
Safe Increment: -32767
기억해야 할 주요 사항:
- 정수 오버플로우는 예상치 못한 결과를 초래할 수 있습니다.
- 서로 다른 유형은 서로 다른 오버플로우 동작을 보입니다.
- 계산 전에 항상 값 범위를 확인합니다.
- 계산에 적절한 데이터 유형을 사용합니다.
요약
이 랩에서는 C++ 의 다양한 데이터 유형과 이를 사용하는 방법에 대해 배웠습니다. 먼저 short, int, long을 포함한 다양한 크기의 정수 변수를 선언하고 각 범위에 대해 살펴보았습니다. 다음으로, 소수점이 있는 float 및 double과 같은 부동 소수점 변수를 초기화했습니다. 또한 작은 따옴표를 사용하여 문자 변수를 선언하고 초기화하는 방법과 std::string 클래스를 사용하여 문자열 변수를 만드는 방법을 배웠습니다. 또한, 서로 다른 숫자 유형 간의 변환을 위한 타입 캐스팅 (type casting) 을 탐구하고, const 키워드를 사용하여 상수를 정의했으며, 참/거짓 조건에 대한 부울 변수를 사용했습니다. 마지막으로, sizeof() 연산자를 사용하여 다양한 데이터 유형의 메모리 크기를 확인하고 정수 오버플로우 상황을 처리했습니다.



