티스토리 뷰
목차
C언어 C++ 정수형 변수 int란 (기본 Integer, 정수 뜻)
먼저, 훑고 넘어갈 사항
1. 정수형 변수 Int 자료형 크기 (C언어 C++ 공통)
- 16 Bit = 2 Byte = 1 Word (IBM 호환, 이 포스트는 16비트 프로세서를 기준으로 작성됨)
2. 어셈블러에서 사용되는 자료형 (괄호 안은 바이트, 비트)
- byte(1-8), word(2-16), dword(4-32), qword(8-64)
3. 기본 자료형 크기
byte |
1 Byte |
short |
2 Byte |
int |
4 Byte |
long |
4 Byte |
double |
8 Byte |
4. 출처 - Numeric Values [링크]
c++ 데이터 타입 정수형
Practical Learning: Using Integer Variables
아래는 사용 예입니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | #include <iostream> using namespace std; void main() { unsigned short period; unsigned int principal; wprintf_s(L"=-= Loan Evaluation =-="); _putwch('\n'); wprintf_s(L"Enter the amount of the loan: "); cin >> principal; wprintf_s(L"Enter the number of months: "); cin >> period; wprintf_s(L"=-= Loan Summary =-="); _putwch('\n'); cout << "Principal: " << principal << "\n"; cout << "Period: " << period << " months\n"; getchar(); getchar(); } | cs |
도스 창에서 정수형 변수로 원하는 값을 입력하시면 아래와 같은 결과 창이 생성됩니다. (C언어, C++ 모두 결과는 같음)
1 2 3 4 5 6 7 8 9 | =-= Loan Evaluation =-= Enter the amount of the loan: 3500 Enter the number of months: 24 =-= Loan Summary =-= Principal: 3500 Period: 24 months | cs |
c++ 데이터 타입 정수형
Long Integers
Introduction
INT(Integer) 데이터형 이외에 자연수를 표현하기 위한 다른 방법도 존재합니다.
아래는 사용 예입니다.
1 2 3 4 5 6 7 | #include <iostream> using namespace std; void main() { long number; } | cs |
Long형 데이터를 위해 Win32는 LONG, LONG32란 데이터형을 지원합니다.
Insigned Long Integers
Long형도 음수, 양수를 표현할 수 있으며, 양수를 표현할 때 unsigned long을 사용합니다. 이 경우에는 0에서 4,294,967,295까지 표현할 수 있습니다. Win32 라이브러리는 이를 위해 ULONG, ULONG32, DWORD 데이터 타입을 지원합니다.
c++ 데이터 타입 정수형
Large Integers
정수형 변수인 이 자료형은 두 개의 32비트(64비트, Quad-word) 이상의 메모리로 자연수를 나타낼 수 있습니다.
『64비트를 필요』로 하는 경우에 사용이 되며, Win32 라이브러리는 이를 위해 LONGLONG, LONG64, DWORD64, INT64란 데이터 타입을 지원합니다. 표현 범위는 -9,223,372,036,854,775,808에서 9,223,372,036,854,775,807까지입니다.
아래는 사용 예입니다. (C언어 C++ 공통)
1 2 3 4 5 6 7 8 9 10 11 | #include <iostream> #include <Windows.h> using namespace std; void main() { DWORD64 number = 514608; cout << number; putchar(L'\n'); } | cs |
만약, 양수만 사용할 경우엔, ULONGLONG, ULONG64, DWORDLONG, UINT64를 사용해, 0에서 18,446,744,073,709,551,615까지 표현할 수 있습니다.
Floating-Point Numbers
Floating-Point Numbers With Single-Precision
integer를 사용하지 않고, 부동 소수점 하나로 십진법 소수를 나타낼 수 있으며, 특히 Intel 프로세서는 부동 소수점을 위한 2개의 카테고리를 지원합니다.
다음은 Float 사용의 예와 7자리 숫자의 표현 방법 예입니다.
1 2 3 4 5 6 7 | #include <iostream> using namespace std; void main() { float number; } | cs |
1 2 3 4 5 6 7 8 9 10 | #include <iostream> using namespace std; void main() { float number = 51.4608; cout << number; putchar(L'\n'); } | cs |
Float형의 변수는 3.4 x 10-38에서 3.4 x 1038까지 『4바이트를 할당』해 저장할 수 있으며, Win32 라이브러리는 FLOAT형을 제공합니다. (C언어 C++ 공통)
c++ 데이터 타입 정수형
Floating-Point Numbers With Double-Precision
정수형 변수로 표현할 수 없는 소수점입니다. 더 정밀한 부동 소수점을 표현하기 위해선 더 많은 메모리 공간이 필요하며, 이는 2개의 Long형 변수의 사용량이 필요하다는 의미로 64비트 환경에서 구동된다는 것입니다(범위 1.7 x 10-308에서 1.7 x 10308까지).
1 2 3 4 5 6 7 8 9 | #include <iostream> using namespace std; void main() { double number = 57.4038; cout << number; putchar(L'\n'); } | cs |
Win32 라이브러리는 이를 위해서 DOUBLE 자료형을 제공합니다.
Practical Learning: Using Double-Precision Numbers
아래는 사용 예입니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | #include <iostream> using namespace std; void main(){ unsigned short period; unsigned int principal; double interestRate; double interestPercentage; double interestEarned, futureValue; double periodInYears; wprintf_s(L"=-= Loan Evaluation =-="); _putwch('\n'); wprintf_s(L"Enter the amount of the loan: "); cin >> principal; wprintf_s(L"Enter the number of months: "); cin >> period; wprintf_s(L"Enter the interest rate applied on the loan: "); cin >> interestRate; periodInYears = period / 12; interestPercentage = interestRate / 100; interestEarned = principal * interestPercentage * periodInYears; futureValue = principal + interestEarned; wprintf_s(L"=-= Loan Summary =-="); _putwch('\n'); cout << "Principal: " << principal << "\n"; cout << "Period: " << period << " months\n"; cout << "Interest Rate: " << interestRate << "%\n"; cout << "Interest Earned: " << interestEarned << "\n"; cout << "Future Value: " << futureValue << "\n"; getchar(); getchar(); } | cs |
위와 같이 코딩한 이후에, F5를 눌러 실행시킵니다.
principal에는 2650, period에는 28, interest rate에는 12.65를 입력합니다.
그러면, 『정수형 변수』를 포함한 아래와 같은 결과화면이 생성됩니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 | =-= Loan Evaluation =-= Enter the amount of the loan: 2650 Enter the number of months: 28 Enter the interest rate applied on the loan: 12.65 =-= Loan Summary =-= Principal: 2650 Period: 28 months Interest Rate: 12.65% Interest Earned: 670.45 Future Value: 3320.45 | cs |
c++ 데이터 타입 정수형
Long Double-Precision Numbers
10바이트를 사용하여 더 넓은 범위의 Double형 변수를 사용할 수 있습니다.
(범위 3.4 x 10-4932에서 1.1 x 104932
아래는 사용 예입니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include <iostream> using namespace std; void main() { long double radius = 15.625, Radius = 18.125; long double area, Area, TotalArea; Area = Radius * Radius * 3.14159; area = radius * radius * 3.14159; TotalArea = Area - area; cout << "Properties of the plate"; cout << "\nExternal Radius: " << Radius; cout << "\nInternal Radius: " << radius; cout << "\nArea: " << TotalArea; } | cs |
C언어 C++ 정수형 변수 int란 (기본 Integer, 정수 뜻)