영넌 개발로그

[C++ 기초] Hello World! 출력하기 / 변수 입력받기 본문

코딩/C++

[C++ 기초] Hello World! 출력하기 / 변수 입력받기

영넌 2020. 9. 30. 23:10
#include <iostream>

int main()
{
	std::cout << "Hello World!" << std:: endl;

	return 0;
}

 

  • <iostream> 헤더 이름에 .h 가 붙지 않음 / 입출력과 관련된 헤더 파일
  • std ::                  c++ 내장 기본 namespace임 
  • cout                  출력 (consol out) 
  • 화살표                오른쪽의 있는 문자열을 cout에 정해 준다.
  • endl                   end of line 줄바꿈을 뜻함 / 끝에 안붙여주면 줄바꿈 안됨 (c에서 \n와 같음)

  

 

* name-space ??

     함수나 변수의 이름이 겹칠 수가 있음 

     겹쳐서 사용된 이름이 혼동되지 않도록 사용함

     using을 사용하여 아래처럼 std:: 쓰는것을 생략하여 사용할 수 있음

 

#include <iostream>

int main()
{
	using namespace std;
	cout << "Hello World" << endl;

	return 0;
}

 

 

 

* 변수를 출력해보자

cout 은 알아서 출력해준다.

%d %f %s 와 같이 사용자가 맞춰주지 않아도 된다.

 

#include <iostream>

int main()
{
	using namespace std;

	int n;

	cout << "n has";
	n = 25;
	cout << n << endl;

	// 출력 결과 : n has 25

	n -= -1;

	cout << "n has now" << n << endl;

	//출력 결과 : n has now 24

	return 0;
}

 

 

 

* 변수를 입력받아보자

cout과 반대방향으로 화살표를 써준다. >>

 

#include <iostream>

int main()
{
	using namespace std;

	cout << "enter number: ";
	int n;
	
	cin >> n;
	cout << "The number you entered is " << n << endl;

	return 0;
}

 

 

Comments