잔잔이네
article thumbnail

1. 연산자

1.1. 제곱

<code />
#include <iostream> using namespace std; int main() { int x = std::pow(2, 3); // 제곱 cout << x << endl; return 0; }

pow를 사용하여 2^38이 출력된다

 


1.2. 나누기 연산자

<code />
#include <iostream> int main() { using namespace std; int x = 7; int y = 4; cout << x / y << endl; cout << float(x) / y << endl; cout << x / float(y) << endl; cout << float(x) / float(y) << endl; cout << -5 / 2 << endl; cout << -5 % 2 << endl; return 0; }

연산에서 둘 중 하나만 float형이면 결과가 float형으로 나온다.

 

음수를 나누게 되었을 때, 음수 결과는 소수 부분을 절삭해서 출력된다.

음수의 나머지 연산자는 왼쪽 숫자의 부호를 따라간다.

1.3. 산술연산자의 표기법

<code />
int x = 7; int y = 4; int z = x; z += y; // z = z + y z %= y; // z = z % y

z += y; // z = z + y

z %= y; // z = z % y 와 같은 방식으로 표기할 수 있다.

 


2. 증감연산자

<code />
#include <iostream> int main() { using namespace std; int x = 6, y = 6; cout << x << " " << y << endl; cout << ++x << " " << --y << endl; cout << x << " " << y << endl; cout << x++ << " " << y-- << endl; cout << x << " " << y << endl; return 0; }

증감연산자를 에 붙여주면 선 연산 - 저장 - 출력

에 붙여주면 선 출력 - 연산 - 저장


2.1. 나쁜 예

<cpp />
int x = 1; int v = add(x, ++x); cout << v << endl;

(x, ++x) // 이런식의 코딩은 좋지않다.

 


profile

잔잔이네

@잔잔잔잔

🌈

검색 태그