#include <stdio.h>
int main() {
/* 연산자 우선 순위
1. 괄호()
2. 단항 연산자 ! ++ --
3. 산술 연산자 * / -> + -
4. 비교 연산자 > < >= <= == !=
5. 논리 연산자 && -> ||
6. 대입 연산자 = += -=
*/
// 증감 연산자 ++ --
int a = 10;
printf("a는 %d\n", a);
a = a + 2;
printf("a는 %d\n", a);
a += 2;
printf("a는 %d\n", a);
a++; // ++ : 변수에 1을 더해서 다시 저장
printf("a는 %d\n", a);
a--; // -- : 변수에 1을 빼서 다시 저장
printf("a는 %d\n", a);
int b = 20;
printf("b는 %d\n", ++b); //전위 1. +1 계산 2.printf
printf("b는 %d\n", b++); //후위 1.printf 2. +1계산
printf("b는 %d\n", b);
int c = 30;
printf("c는 %d\n", --c);
printf("c는 %d\n", c--);
printf("c는 %d\n", c);
printf("\n----문제----\n");
int x = 4;
printf("x = %d\n", ++x);
x += 3;
printf("x = %d\n", x++);
printf("x = %d\n", x);
int y = 7;
printf("y = %d\n", ++y);
printf("y = %d\n", y++);
printf("y = %d\n", y);
printf("\n");
/* 논리 연산자 ! -> && -> ||
&& : AND 양쪽 모두 참이면 참
|| : OR 하나 이상 참이면 참
! : NOT 참과 거짓을 반대로
***C언에서는 참을 1, 거짓을 0
*/
a = 5, b = 10, c = 15;
printf("%d\n", a + b > c && c == 15);
// 1 2 4 3
printf("%d\n", a + (b > c));
printf("%d\n", !(a + b <= c) && b != 10);
// 3 1 2 5 4
printf("\n");
// 삼항 연산자 : 조건문 한 줄로 만들기
// 조건식 ? 참일 때 값 : 거짓일 때 값
printf("%s\n", 5 > 10 ? "참" : "거짓");
a = 5, b = 9;
int max = a > b ? a : b;
printf("더 큰 값은 %d\n", max);
printf("\n----문제----\n");
//점수가 60점 이상이면 합격, 아니면 불합격을 출력하는 프로그램
int score = 10;
printf("%s\n", score >= 60 ? "합격" : "불합격");
//d가 60이상이거나 e가 80미만이면 1, 아니면 0
int d = 70, e = 90;
printf("%d\n", d >= 60 || e < 80 ? 1 : 0);
return 0;
}
To embed this project on your website, copy the following code and paste it into your website's HTML: