have to do_yeon

[C++ / OOP_lec02] C part in C++ (1) 본문

Supplementary/OOP (1-2)

[C++ / OOP_lec02] C part in C++ (1)

또김또 2022. 9. 15. 19:04


1. Essentioal Structure of C++ Program

 

C++ 코드를 작성할 때 기본이 되는 구조이다. 각 행에 대한 설명을 주석으로 첨부해놓았다.

// Header files
#include <iostream>

// Namespace skip
using namespace std;

// Function Header
int main() {	// Return Type, Function name

    cout << "Hello world!" << endl;
    
    return 0;	// Return Value
}

C++ 코딩 환경에서 C언어를 작성할 때 사용했던 헤더파일인 "stdio.h" 도 사용 가능하다.

그런데... "iostream" 에서는 C++의 입출력인 "cin", "cout" 뿐만 아니라 C언어의 입출력인 "scanf", "printf" 도 사용 가능하다!

 

그러니 그냥 iostream을 선언하도록 하자.

 

이외에도 math, string, algorithm 등등... 많은 헤더파일이 있다. 이에 대해 더 알아보고싶다면 다음 사이트를 참고해보자.

>> https://en.cppreference.com/w/cpp/header 

 

C++ Standard Library headers - cppreference.com

Standard Library headers The interface of C++ standard library is defined by the following collection of headers. Concepts library Fundamental library concepts [edit] Coroutines library Coroutine support library [edit] Utilities library std::any class [edi

en.cppreference.com

 


2. Standard Output & Input

 

다음은 기본적인 입출력에 대한 설명이다.

#include <iostream>
using namespace std;

bool main() {
    int a, b;

    cout << "a : ";	// print "a : "
    cin >> a;		// input a

    cout << "b : ";	// print "b : "
    cin >> b;		// input b

    auto result = a + b;
    
    cout << "a + b = " << result << endl;
}

위 함수에서 main 함수에 반환값이 없으므로 반환형을 bool로 해주었다.

(필자도 평소에는 int로 설정하지만 그냥 해보았다ㅎㅎ... 그냥 참고하라는 뜻~)

<< : "insertion" operator
>> : "extraction" operator

 

위 인용 박스는 강의자료에 있는 내용을 그대로 가져온 것이다. 출력을 하는 cout 옆에는 << , 입력하는 cin 옆에는 >> 을 사용해야 한다. 화살표처럼 생각하면 편하다.

 

그리고 위 예제에서 마지막 행에 cout 옆에 << 를 붙여서 계속 출력하는 것이 가능하다. 예전에 간단하게 정리해놓은 글인데, 필요하면 참고하도록 하자.

>> https://have-to-do-yeon.tistory.com/7

 

[C++] 01-1. 입력과 출력

1. 헤더파일 선언 : #include 2. 데이터 출력 : std :: cout << ‘출력대상1’ << ‘출력대상2’ << ‘출력대상3’ << std :: endl ; std :: cout << “a” << ‘ ’ << “B”; std :: cout << ‘ ’ << “D” <<..

have-to-do-yeon.tistory.com

 


3. Variable

 

개인적으로 코드를 짤 때 중요하다고 생각하는 변수에 관한 설명이다.

#include <iostream>
using namespace std;

int main() {
    int a;		// Declaration
    int b = 1;		// Declaration & Initialization
    int c { 3 };	// Introduced in C++11
    
    cout << b + c << endl;
    
    return 0;
}

C언어를 공부할 때 배웠듯이, 변수 선언이 필요하다. 위 예제에서 a는 선언되었고, b와 c는 초기화까지 되었다.

초기화가 중요한데, 선언을 한다고 초기화가 되는 것이 아니다. 이것때문에 나중에 오류가 뜰 수도 있다... (경험담 맞음

 

입력을 받을 것이 아니라면, 반드시 변수는 초기화 해주자!

 


4. Data Types

 

① Integer : 숫자, 그 중에서도 정수에 대한 데이터타입이다.

② Character, Boolean, Floating Point

Data Type Bytes Range
Integer short 2
-32,768 ~ 32,767
(-2^15 ~ 2^15 - 1)
unsigned short
0 ~ 32,767
(0 ~ 2^16 - 1)
int 4
-2,147,483,648 ~ 2,147,483,647
(-2^31 ~ 2^31 - 1)
unsigned int 0 ~ 2,147,483,647
(0 ~ 2^
32 - 1)
long 4
-2,147,483,648 ~ 2,147,483,647
(-2^31 ~ 2^31 - 1)
unsigned long 0 ~ 2,147,483,647
long long 8
-9,223,372,036,854,775,808 ~ 9,223,372,036,854,775,807
(-2^64 ~ 2^64 - 1)
unsigned long long 0 ~ 9,223,372,036,854,775,807
(0 ~ 2^
64 - 1)
Chatacter char 1 -128 ~ 127 (2^-7 ~ 2^7-1)
unsigned char 0 ~ 255 (0 ~ 2^8-1)
Boolean bool 1 true (1) / false (0)
Floating Point float 4 ±1.17E-38 ~ ±3.40×E-38
(3.4 X 10^-38) ~ (3.4 X 10^38)
double 8 ±2.25E-308 ~ ±1.79×E-308
(1.7 X 10^-308) ~ (1.7 X 10^308)
long double 8 ±2.25E-308 ~ ±1.79×E-308
(1.7 X 10^-308) ~ (1.7 X 10^308)

 


5. 32-bit Floating Point Type

 

소수표현에 관한 설명이다. 일반적으로 double은 float보다 2배 정도 더 정확하다고 한다. double은 15-16개의 10진수를, float은 7개의 10진수를 표현하기 때문이다.

Comments