나의 작은 valley

[CPP] 입출력스트림(cout, cin, endl) 본문

Computer Science/[c++] 문법 정리

[CPP] 입출력스트림(cout, cin, endl)

붕옥 아이젠 2022. 12. 28. 14:38
728x90

cout을 이용한 기본적인 코드 1)

#include <iostream>	// cout, cin, endl, ...
#include <cstdio>	// printf

int main(void)
{
	int x = 1024;
	//std = name_space, cout같은 녀석들은 std라는 네임스페이스 안에 정의되어 있다.
	std::cout << "Hello World !" << std::endl;	
	std::cout << "x is " << x << std::endl;

    return 0;
}

 

출력)

 

 

입출력 스트림을 이용한 기본적인 코드 2)

#include <iostream>	// cout, cin, endl, ...
#include <cstdio>	// printf

int main(void)
{
	std::cout << "abc" << "\t" << "def" << std::endl;
	std::cout << "ab" << "\t" << "cdef" << std::endl;

	return 0;
}

출력)

abc     def
ab      cdef

>> \t(tab) 기능이 수행된 것을 볼 수 있다.

 

cf) 탭(tab)에는 자동 줄맞춤 기능이 있다. 

 

 

 

//std:: 생략하는 방법

#include <iostream>	// cout, cin, endl, ...
#include <cstdio>	// printf

int main(void)
{
	using namespace std;
	cout << "abc" << "\t" << "def" << endl;
	cout << "ab" << "\t" << "cdef" << endl;

	return 0;
}

>> 중괄호가 끝나는 부분 전까지 std::를 생략할 수 있다. using namespace std;를 쓰면 컴퓨터가 cout이나 endl을 만났을 때 자기가 std에 가서 찾는다.

 

 

//cin을 사용한 간단한 코드)

#include <iostream>	// cout, cin, endl, ...
#include <cstdio>	// printf

int main(void)
{
	using namespace std;

	int x;
	cin >> x;

	cout << "Your Input is " << x << endl;
	return 0;
}

출력)

123
Your Input is 123

>> cin은 사용자의 입력을 받는 함수이다.

728x90

'Computer Science > [c++] 문법 정리' 카테고리의 다른 글

[CPP] 이름 짓기  (0) 2022.12.29
[CPP] 함수(function)  (0) 2022.12.29
[CPP] 변수(Variable )  (0) 2022.12.28
[CPP] 주석(Comments)  (0) 2022.12.28
[cpp] C++ 이모저모  (0) 2022.12.21
Comments