-
기본출력_220701데일리 codeup/출력 2022. 7. 1. 11:54
따옴표 출력
문자열에 특수 문자 포함시키기
c++에서 문자열은
"
로 표현할 수 있다.cout << ""Hello"";
를 사용하면"
의 시작과 끝을 구분하지 못해 에러가 발생한다."
의 문자열 속성을 제거하기 위해 문자앞에\
를 붙힌다.#include <iostream> using namespace std; int main(){ cout << "He says \"It\'s a really simple sentence\"."; return 0; }
2줄 출력
c++에서 줄바꿈을 출력하기 위해서는 cout << endl; 을 이용할 수 있다. 문자열 출력에 이어 endl 을 붙여주게 되면 한 줄이 띄어지게 된다.
예제1
#include <iostream> using namespace std; int main() { cout << "Hello World"; cout << endl; cout << "C++ is Fun"; return 0; }
다음과 같이 표현할 수 있다.
#include <iostream> using namespace std; int main() { cout << "Hello World" << endl << "C++ is Fun"; return 0; }
줄바꿈을 출력하기 위해 \n 문자를 이용할 수도 있다. \n 문자는 new line을 의미하는 특수문자로, 한 줄을 띈다는 것을 나타낸다.
예제2
#include <iostream> using namespace std; int main() { cout << "Hello\nWorld"; return 0; }
숫자 출력
숫자 3 출력
#include <iostream> using namespace std; int main(){ cout << 3; return 0; }
공백을 사이에 두고 출력
2개의 값을 공백을 사이에 두고 출력하기 위해서는 cout 함수를 이용해 " " 문자열 내 두 값을 공백을 사이에 두고 출력해주면 된다.
#include <iostream> using namespace std; int main() { cout << "3 5"; return 0; }
또 다른 방법으로는 다음과 같이 << 을 여러번 사용하여 출력할 수 있습니다.
#include <iostream> using namespace std; int main() { cout << 3 << " " << 5; return 0; }
한줄 출력
Let's go LeebrosCode!
를 출력하는 프로그램을 작성해보세요.#include <iostream> using namespace std; int main(){ cout << "Let\'s go LeebrosCode!"; return 0; }
두줄 출력
Welcome to LeebrosCode!
를 출력하는 프로그램을 작성해보세요.
#include <iostream> using namespace std; int main(){ cout << "Hello students!" << endl << "Welcome to LeebrosCode!"; return 0; }
다양하게 출력
365 Circumference rate 3.1415926535
를 출력하는 프로그램을 작성해보세요.
#include <iostream> using namespace std; int main(){ cout<<"Total days in Year\n365\nCircumference rate\n3.1415926535"; return 0; }
'데일리 codeup > 출력' 카테고리의 다른 글
변수 값 변경_220701 (0) 2022.07.01 소수점 맞춰 출력_220701 (0) 2022.07.01 출력형식_220701 (0) 2022.07.01 변수와 자료형_220701 (0) 2022.07.01 기본출력_220629 (0) 2022.06.29