Notice
Recent Posts
Recent Comments
Link
250x250
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- log_bin
- upgrade
- Effective Java
- git
- migration
- expire_logs_days
- current_date
- npm
- log4j2
- spring
- REACTJS
- eslint
- JavaScript
- Regular expression
- 퀵소트
- update
- Webpack
- MySQL
- mysql 5.5
- 정규표현식
- nodejs
- Effective Java 3/e
- regex
- Chunk
- java
- spring cloud
- Node
- Spring Batch
- REACT
- Express
Archives
- Today
- Total
내 세상
[C++] 표준입출력, 변수 선언 본문
728x90
반응형
C 표준 입출력
- printf, scanf
C++ 표준 입출력
- cin, cout
- %d, %c, %f 등의 포맷 지정자 필요 없음.
- &a, &b 처럼 주소를 보낼 필요 없음.
#include <iostream> // C++ 스타일 입출력
#include <iomanip> // setw와 같은 조정자 (iomaniplator)
int main()
{
int n = 10;
printf("n= %d\n", n);
// C++ 방식의 출력
std::cout << n << std::endl; // 10진수
std::cout << std::hex << n << std::endl; // 16진수
std::cout << n << std::endl; // 16진수
std::cout << std::dec << n << std::endl; // 10진수
std::cout << "hello" << std::endl;
std::cout << std::setw(10) << "hello" << std::endl;
std::cout << std::setw(10) << std::setfill('#') << "hello" << std::endl;
}
C/C++의 struct 변수 사용 차이점
struct Point
{
int x;
int y;
};
int main()
{
struct Point pt1; // C 스타일의 변수 선언
// 그래서 C에서는 typedef struct point POINT; 처럼 사용함.
Point pt2; // C++ 스타일의 변수 선언, 바로 선언 가능
}
C/C++의 struct 선언 후 초기화 차이점
struct Point
{
int x = 0; // 구조체 멤버 초기화 가능.
int y = 0;
};
int main()
{
Point pt; // x, y가 0으로 초기화
int n1 = 0b1; // C++11부터 2진수 가능.
int n2 = 1'000'000; // literal 에 대한 자릿수 표기법
// 컴파일러가 리터럴 사이의 '는 무시한다.
// 편의를 위해 사용할 수 있음
// @@@@@@@@ C++에서의 새로운 초기화 @@@@@@@@@
// 모든 변수를 종류에 상관없이 {}로 초기화 할 수 있다.
// " 일관된 초기화(uniform initialization)"
// direct initialization : = 이 없는 초기화
// copy initialization : = 이 있는 초기화
int n = { 0 };
int x[2] = { 1,2 };
Point pt2 = { 1,2 };
// prevent narrow
int n3 = 3.14; // 허용되지만 아주 나쁜 특징. 버그의 원리
int n4 = { 3.14 }; // error
char c{ 300 }; // error. -128~127 까지만 가능
}
C++의 auto, decltype 사용법
int main()
{
int x[10] = { 1,2,3,4,5 };
int n1 = x[0];
// auto : 우변의 표현식으로부터 타입을 결정해달라
auto n2 = x[0];
auto a3 = x; // 1. int a3[10]; ----> Error, int a3[10] = x 이런 식이라 배열을 배열로 초기화 할 수 없음
// 2. int* a3; -----> Ok, 배열은 포인터로 받을 수 있음.
// decltype : () 안의 표현식으로 부터 타입을 결정해달라
decltype(n2) n3;
decltype(x) x2; // int x2[10] 로 선언됨.
decltype(x[0]) n4; // error, int가 아니고, int& .
}
C++11에서의 추가된 문법
// C언어의 typedef
//typedef int DWORD;
//typedef void(*F)();
// C++11에서 새로 추가한 문법, Line 2~3과 Line 7~8은 동일한 의미
using DWORD = int;
using F = void(*)();
// typedef : 타입에 대한 별칭(alias)만 만들 수 있다.
// using : 타입 뿐 아니라 "템플릿"에 대한 별칭도 만들 수 있다.
int main()
{
DWORD n; // int n
F f; // 함수 포인터 변수
}
constexpr
//int main()
//{
// int n1 = 10;
// const int c1 = 10; // 컴파일 시간 상수
// const int c2 = n1; // 실행 시간 상수 - 멀티 쓰레드 같은 환경에서 변경될 수 있음.
// // 값을 변경할 수 없다. 초기값이 얼마인지 컴파일러는 알수가 없다.
//
// // 다음 중 에러를 모두 골라 보세요
// // C89(1989년 표준화 버전): 배열의 크기는 컴파일 시간에 알아야 한다
// // C99(1999년 표준화 버전): 배열의 크기로 변수도 사용 가능.
// // g++ 지원, cl 등 일부 컴파일러 지원 안함
// // C11, C18도 있습니다.
// int arr1[10]; // ok
// int arr2[n1]; // error, 단 g++(gcc)는 가능
// int arr3[c1]; // ok
// int arr4[c2]; // error.
//
// foo(n1);//변수 전달 가능
//}
//
//void foo(const int c) { // 상단에 2개
// int x[c]; // error.
//}
extern const int c5; // 컴파일 시간 상수인지
// 실행 시간 상수인지 구별할 수 없다.
// 최적화 안됨.
int main()
{
int n = 10;
// const
const int c1 = n; // ok
const int c2 = 10; // ok
// C++11 constexpr : 컴파일 시간 상수를 의미!
constexpr int c3 = n; // error.
constexpr int c4 = 10; // ok.
// 결론 : 리터럴로 초기화되는 변수는 되도록이면 constexpr를 사용하자!
}
C++17에서의 새로운 방식 (auto)
struct Point
{
int x{ 5 };
int y{ 7 };
};
int main()
{
Point pt;
int x = pt.x; // 전통적인 방식
// C++ 17의 새로운 방식 - 25 Page
auto[a, b] = pt; // structure binding 이라는 문법
//swift, C#등에서 오래전부터 있던 문법
// 주의 1.auto만 가능.. int 안됨.
// 2. 일부만 꺼낼 수 없음. 전부 꺼내야 함.
int arr[3] = { 1,2,3 };
auto[a1, a2, a3] = arr; // 배열도 가능
}
728x90
반응형
'Language > C/C++' 카테고리의 다른 글
[C++] C++스타일의 캐스팅 (0) | 2020.01.02 |
---|---|
[C++] C++11/C++17 에서의 반복문/제어문 (0) | 2020.01.02 |
[C++] 함수 (0) | 2020.01.02 |
[C++] namespace, std (0) | 2020.01.02 |
[C Language] C언어로 퀵소트 구현하기 (0) | 2019.10.28 |