[C++] cout,cin 사용법
C++ cout, cin 사용법
출력 함수 cout
콘솔 화면에 데이터를 출력 하는 방법에 대해 알아보자
std::cout << 출력 할 대상
#include <iostream> int main(int argc, char** argv) { std::cout<<"hello world"<<std::endl; std::cout<<"hello world2"<<std::endl; return 0; } |
#include <iostream> --> 콘솔 화면에서 입출력 기능을 하기 위해 iostream을 한다.
.h는 생략 가능
std::cout --> 출력 할 대상
std::endl --> 개행 처리 한다.
입력 함수 cin
콘솔 화면에 데이터를 출력 하는 방법에 대해 알아보자
std::cin >> 입력 받을 대상
#include <iostream> int main(int argc, char** argv) { char text[10] = {0,};
std::cout<<"what your name?"<<std::endl; std::cin>>text; std::cout<<"hello "<<text<< std::endl; return 0; } |
char text[10] = {0,}; --> 문자열 10byte 할당 + 초기화
std::cin>>text; --> text 변수에 사용자가 입력 한 것을 저장 한다.
std::cout<<"hello "<<text<< std::endl; --> 변수 값 출력
C VS C++
C와 C++과 비교 해 보자
C |
C++ |
#include <stdio.h> |
#include <iostream> |
printf |
std::cout |
scanf |
std::cin |
파일명.c |
파일명.cpp |