false
(boolean literal), 1897234L
(long integer literal), ‘A’
(character literal), 245.78F
(float literal), “Hello”
(string literal), 114.7892
(double literal), 234
(integer literal), 245.784321L
(long double literal): 하나의 피연산자에 대해 단항 연산자가 적용된 표현식
ex) Applying plus and minus operator
int x = 4;
int y = -10;
cout << "Using plus operator on x:" << +x << endl; // 4
cout << "Using plus operator on x:" << -x << endl; // -4
cout << "Using plus operator on x:" << +y << endl; // 10
cout << "Using plus operator on x:" << -y << endl; // -10
sizeof expression // finds the size of an expression
sizeof(type) // finds the size of a type
cout << sizeof(int) << endl; // 4
*
기호를 사용/
기호를 사용%
기호를 사용//multiplication
cout << 3*4 << endl; // 12
cout << 3.0*4 << endl; // 12.0
//division
cout << 30/5 << endl; // 6
cout << 4/7 << endl; // 0 -> integer 결과
//remainder
cout << 30%5 << endl; // 0
=
기호를 사용int x;
int y;
cout << (x=14) << endl; // 14
cout << (y=87) << endl; // 87
int x = 20;
int y = 30;
int z = 40;
int t = 50;
int u = 60;
x += 5;
y -= 3;
z *= 10;
t /= 8;
u %= 7;
cout << x << ", " << y << ", " << z << ", " << t << ", " << u << endl; // 25 27 400 6 4
C++ 컴파일러
는 연산 전에 암묵적 자료형 변환을 수행bool
→ int
(승격 후)char
→ int
short
→ int
unsigned short
→ unsigned int
float
→ double
#include <typeinfo> // 꼭 추가해줘야 typeid를 사용할 수 있음
bool x = true;
char y = 'A';
short z = 14;
float t = 24.5;
cout << typeid(x+100).name() << endl; // i: integer
cout << x+100 << endl; // 101
cout << typeid(y+1000).name() << endl; // i
cout << y+1000 << endl; // 10665
cout << typeid(z*100).name() << endl; // i
cout << z*100 << endl; // 1400
cout << typeid(t+15000.2).name() << endl; // d: double
cout << t+15000.2 << endl; // 15024.7
#include <typeinfo>
int x = 123;
long y = 140;
double z = 114.56;
cout << typeid(x+y).name() << endl; // l: long
cout << x+y << endl; // 263
cout << typeid(x+y+z).name() << endl; // d: double
cout << x+y+z << endl; // 377.56
#include <typeinfo>
int x;
double y;
x = 23.67; // x = 23 -> 암묵적 자료형 변환(데이터 손실로 부작용)
y = 130; // y = 130.0
cout << typeid(x=23.67).name() << endl; // i: integer - x가 int형이므로
cout << x << endl; // 23
cout << typeid(y=130).name() << endl; // d: double - y가 double형이므로 타입은 double
cout << y << endl; // 130 - 130.0이 아닌 이유: cout은 double을 출력할 때 .0은 출력 x