반응형
1. 기본 함수
|
C++ |
C |
헤더 파일 |
<stdio.h> |
<iostream> |
표준 입력 |
scanf |
std::cin |
표준 출력 |
printf |
std::cout |
개행 | '\n' | std::endl or '\n' |
#include <iostream>
int main()
{
int n = 0;
std::cin >> n; // 숫자 입력
std::cout << n; // 입력 숫자 출력
}
2. 조정자(iomanipulator) 함수
- 헤더 사용 : <iomanip>
- 변환 출력 : std::dec(10진수), std::hex(16진수)
- 문자열 출력 자리수 지정 : std::setw(10자리)
- 문자열 출력 정렬(기본 우측 정렬) : std::left(좌측), std::right(우측)
- 문자열 공백 문자 지정 : std::setfill('*')
#include <ostream>
#include <iomanip>
#include <string>
int main()
{
int n = 0;
std::string name = "harry";
std::cin >> n; // 숫자 입력
std::cout << std::hex << n << std::endl; // 입력 숫자 16진수 출력
std::cout << n << std::endl; // 입력 숫자 출력(16진수로 유지)
std::cout << std::dec << n << std::endl;// 입력 숫자 10진수로 재변환 출력
std::cout << name << std::endl; // "harry" 문자열 출력
std::cout << std::setw(10) << name << std::endl; // "harry" 문자열 10자리수로 출력(기본 우측 정렬)
std::cout << std::setw(10) << std::setfill('#') << name << std::endl; // 공백 문자 '#' 채우기
std::cout << std::setw(10) << std::left << name << std::endl; // "harry" 문자열 10자리수로 출력(좌측 정렬)
std::cout << std::setw(10) << std::right << name << std::endl; // "harry" 문자열 10자리수로 출력(우측 정렬)
std::cout << name << std::endl;
}
반응형
'프로그래밍 언어 > C++' 카테고리의 다른 글
C++ 함수 특징 #1 (0) | 2019.05.05 |
---|---|
C++ 제어문(if, switch)과 반복문(for) (0) | 2019.01.16 |
C++ 변수의 특징(variable) #2 (0) | 2019.01.15 |
C++ 변수의 특징(variable) #1 (0) | 2019.01.14 |
C++ 네임스페이스(Namespace) (0) | 2019.01.02 |