잔잔이네

1. sizeof의 사용

<code />
#include <iostream> int main() { using namespace std; float a; sizeof(float); sizeof(a); //sizeof a; 변수명을 선언하면 괄호생략도가능하다 return 0; }

sizeof 연산자는 byte 단위로 크기를 구해준다.

 

사용자가 만든 자료형에도 sizeof 사용이 가능하다.


2. 쉼표 연산자 ( comma operator )

<code />
// comma operator int x = 3; int y = 10; int z = (++x, ++y); cout << x << " " << y << " " << z << endl;

z++x++y 를 계산하고 괄호 처리를하면 뒤의 값을 z에 대입하게 되는 것이 콤마 오퍼레이터 연산이다.
( 괄호처리를 하지않으면 int z = ++x가 대입됨 )

 


3. 조건부 연산자

<cpp />
bool onSale = true; if (onSale) price = 10; else price = 100; cout << price << endl;

일반적인 if문 형태인데

 

onSaletrue여서 price10이 된다.

3.1. const와 삼항연산자 사용

<cpp />
// conditional operator bool onSale = true; const int price = (onSale == true)? 10 : 100; cout << price << endl; return 0;

위의 if문과 다르게 const로 두고 if, else문이 없어도 한번에 10100을 처리할 수 있다.

 

profile

잔잔이네

@잔잔잔잔

🌈

검색 태그