728x90

C언어에서는 입출력을 위한 printf 함수와 scanf 함수를 사용하기 위해 표준 입출력 헤더파일 #include <stdio.h> 을 선언하는데, C++ 에서 표준 입출력 헤더파일 #include <iostream> 을 선언한다.


C++ 헤더 파일에는 .h 가 붙어있지 않다.

std::cout 은 출력을 담당하는 코드이고

std::cin 은 입력을 담당하는 코드이다.

using namespace std; 를 해주면 std::cout 대신에 cout 을 사용할 수 있다.

C언어에서는 %d, %s, %p, %c 처럼 자료형에 따라 서식 지정이 필요한데 C++ 에서는 필요가 없다.

std::endl 은 개행을 하는 코드로 C언어의 \n 과 같다.


#include <iostream> // C++ 표준 입출력 헤더파일
#include <string>

using namespace std;

int main() {
    int a, b;
   
    // shift(시프트) 연산자로 출력하는 방법
    // cout << "출력할 내용";
    std::cout << "Hello, World!" << std::endl;
    std::cin >> a >> b;
    std::cout << a << " + " << b << " = " << a + b << std::endl;

    string str;
    str = "Hello";
    cout << str << endl;

    string name;
    cout << "이름 입력 : ";
    cin >> name;

    string message = "안녕하세요, " + name + " 님";
    cout << message << endl;

}


블로그 이미지

Link2Me

,