expr1?expr2:expr3// expr1 : 조건식// expr2 : 조건식이 참일 때의 값// expr3 : 조건식이 거짓일 때의 값
예시
1
2
3
4
5
6
7
8
9
10
11
12
13
14
voidconditional_operator(){// 표준 형식inta=1;intb=2;intmax=a>b?a:b;println_int("the max is %d\n",max);// 수식을 담는 경우intc=1;intd=2;c>d?d++:c++;printf("c's value : %d / d's value : %d",c,d);}
1
2
the max is 2 # b 의 값이 출력됨
c's value : 2 / d's value : 2 # c 가 증가됨
Reference
C 프로그래밍 (김형근, 곽덕훈, 정재화 공저)
C 프로그래밍 강의 (방송통신대 - 이병래)
댓글 남기기