3주차(2)
2025. 4. 30. 19:31ㆍ프로그래밍1및실습
관계, 등가, 논리 연산자 우선순위
| operator | associativity |
| (), ++(postifx), --(postfix) | left to right |
| +(unary), -(unary), ++(prefix), --(prefix) | right to left |
| *, /, % | left to right |
| <, <=, >, >= | left to right |
| ==, != | left to right |
| && | left to right |
| || | left to right |
| ?: | right to left |
| =, +=, -=, *=, /=, etc | right to left |
| , (comma operator) | left to right |
Associativity(연산의 방향)
- 연산자가 피연사자와 어떻게 결합하는지에 대한 방향을 나타낸다.
-부호를 나타내는 +(unary)는 오른쪽 피연산자와 결합함!
ex) +5, -41
-나눗셈을 나타내는 /는 왼쪽의 피연산자를 오른쪽 피연산자로 나누어줌
ex) 6 / 2
증감 연산자
- unary(단항의 변수)에서만 적용가능하다.
true
++i
cnt--
false
777++
++(a*b-1)
- ++a, a++에는 큰 차이가 존재한다.
- a++은 현제 동작을 수행한 다음에 a를 1 증가해 줌
- ++a는 1을 증가시켜주고 동작을 수행해 줌
#include<stdio.h>
int main(){
int a=1, b=2;
printf("%d\n", ++a); //2를 출력
//a=2
printf("%d\n", a++); //2을 출력
//a=3
printf("%d\n", ++a + b++); //4+2=6을 출력
//a=4, b=3
printf("%d\n", ++a/--b); //5/2=2을 출력
//a=5, b=2
printf("%d %d %d", a++,b++,++a/b);//6,2,3을 출력
}
ex)
a=1, b=2, c=3, d=4
1. (a*b)/c =
2. a*b% c+1 =
3. ++a * b - c-- =
4. 7 -- b * ++ d =
정답
더보기
1. (1*2)/3=0
2. ((1*2)%3)+1=3
3. ((2)*2)-3=1
4. 7 - (-2*5) = 17
대입 연산자
- = : 연산자로 처리됨
- 다른 모든 연산자 보다 우선순위가 낮음
- right to left associativity
variable = right_side
right_side는 expression으로 표현 가능
오른쪽 항의 값이 변수에 대입되고, 그 대입 결과 자체가 다시 하나의 대입 표현식이 됩니다.
ex)
a=(b=2)+(c=3)=5
즉 b=2, c=3이 대입의 표현식이 된다.
그리고 (b=2)+(c=3)=b+c 가 된다.
#include<stdio.h>
int main(){
int a, b, c;
printf("%d\n", a=(b=2)+(c=3)); //5
printf("%d", (a=(b=(c=0)))); //0
}
Assignment operators
k=k+2 : k+=2
Assignment operators = += -= *= /= %= >>= <<= &= ^= |=
j*=k+3 : j=j*(k+3)
#include<stdio.h> int main(){ int a=1, b=2, c=3; a*=b+2;//a=1+*(2+2) printf("%d\n", a); b+=(c+3); //b=2+(3+3) printf("%d",b); }
관계 연산자
- a <b
- a> b
- a <=b
- a>=b
관계 연산자와 표현식
ex)
char c='w';
int i=1, j=2, k=-7
double x=7e+33, y=0.001
1. 'a'+1 <c =
2. -i-5*j>=k+1 =
3. 3 <j <5 =
4. x-3.333 <=x+y =
5. x <x+y =
더보기
1. ('a'+1)<c = 1
2. (-i)-(5*j)>=(k+1) = -9>=-6 = 0
3. 3 <j <5 = ((3 <j)<5) = (0 <5) = 1
4. x-3.3333 <= x+y = 1
5. x<(x+y) = 0 (x가 너무 크기 때문에 y처럼 작으면 x=x+y)
동등 연산자&&논리 연산자
- a==b
- a!=b
| i=1, j=2, k=3 | ||
| expression | equivalent | value |
| i==j | j==i | 0 |
| i!=j | j!=i | 1 |
| i+j+k==-2*-k | (i+j+k)==((-2)*(-k)) | 1 |
- ! a
- 만약 a가 0이면! a=1
- 만약 a가 0이 아니면! a=0
- !! 5=!(! 5)=1
| char c='A'; int i=7, j=7; double x=0.0, y=2.3; |
||
| expression | equivalent | value |
| !c | !c | 0 |
| !(i-j) | !(i-j) | 1 |
| !i - j | (!i) - j | -7 |
| !!(x+y) | !(!(x+y)) | 1 |
| !x*!!y | (!x)*!(!y) | 1 |
- a&&b
- a||b
| char c='B'; int i=3, j=3, k=3; double x=0.0, y=2.3; |
||
| expression | equivalent | value |
| i && j && k | (i&&j)&&k | 1 |
| x||i&&j-3 | x||(i&&(j-3)) | 0 |
| i<j && x<y | (i<j)&&(x<y) | 0 |
| i<j || x<y | (i<j)||(x<y) | 1 |
| 'A'<=c&&c<='Z' | ('A'<=c)&&(z<='Z') | 1 |
| c-1=='A'||c+1=='Z' | ((c-1)=='A')||((c+1)=='Z') | 1 |
분기 (Branching)와 반복(Loop)
- while()
#include<stdio.h>
#define SPACE ' '
int main(){
char c;
c=getchar();
while(c!='\n'){
if(c==SPACE) putchar(c);
else putchar(c+1);
c=getchar();
}
putchar(c);
return 0;
}
- getchar()는 문자를 하나씩 입력받고, putchar는 문자를 출력해 준다.
- c+1을 해주어서 출력해 준다.
cf)
ctype.h
name true if the argument is isalnum() 알파벳이거나 정수 isalpha() 알파벳 isblank() 공백 문자(스페이스, 탭, 줄 바꿈) iscntrl() 제어 문자 isdigit() 숫자인 경우 isgraph() 공백을 제외한 출력 가능한 문자 islower() 소문자 isprint() 출력 가능한 문자 ispunct() 구두점 문자 isspace() 공백 문자(스페이스, 줄 바꿈, 개행, 캐리지 리턴, 수직 탭, 수평 탭 등) isupper() 대문자 isxdigit() 16진수 숫자
콤마 연산자(expr1, expr2)
- expr1을 먼저 평가한 다음, expr2로 진행
#include<stdio.h>
int main(){
int sum, i;
for(sum=0, i=1; i<=10; ++i){ //1~10까지의 합을 구함
sum+=i;
}
printf("%d\n",sum); //55
for(sum=0,i=1;i<=10;sum+=i,++i); //1~10까지의 합을 구함
printf("%d\n",sum); //55
for(sum=0, i=1; i<=10;++i,sum+=i); //2~11까지의 합을 구함
printf("%d\n",sum); //65
}
- 컴마 연산자를 활용하여서 다음과 같이 사용할 수 있다.
- 컴마 연산자는 left to right로 적용된다.
- 마지막의 합은 ++i를 해준 다음 sum+=i를 해주기 때문에 65가 나오게 된다.
break과 continue
- 중첩 loop에서의 break
#include<stdio.h>
#include<stdbool.h>
int main(){
int a=3, b;
while(a>0){
b=3;
while(true){
printf("%d %d\n", a,b);
b-=1;
if(b<=0) {
break;
}
}
a-=1;
}
}
3 3
3 2
3 1
2 3
2 2
2 1
1 3
1 2
1 1
switch문
#include<stdio.h>
#include<stdbool.h>
int main(){
int n;
scanf("%d",&n);
switch(n){
case 1:
printf("1 is true");
break;
case 2:
printf("2 is true");
break;
case 3:
printf("3 is true");
break;
default:
printf("n is false");
}
}
2 //입력
2 is true //출력
- 만약 break가 없으면 만족하는 case문 이하의 동작을 수행한다.
#include<stdio.h>
#include<stdbool.h>
int main(){
int n;
scanf("%d",&n);
switch(n){
case 1:
printf("1 is true\n");
case 2:
printf("2 is true\n");
case 3:
printf("3 is true\n");
default:
printf("n is false\n");
}
}
2 //입력
2 is true
3 is true
4 is true //출력
조건 연산자
- expr1? expr2:expr3
- expr1이 참이면 expr2, 거짓이면 expr3을 실행한다.
#include<stdio.h>
#include<stdbool.h>
int main(){
int x=1,y=2;
if(x<y) printf("%d\n", y);
else printf("%d\n", x); //조건문
printf("%d\n",x<y?y:x); //조건 연산자
}
2
2
| char a='a', b='b'; int i=1, j=2; double x=7.07; |
|||
| expression | equivalent | value | value |
| i==j?a-1:b+1 | (i==j)?(a-1):(b+1) | 99 | int |
| j%3==0?i+4:x | (j%3==0)?(i+4):x | 7.07 | double |
| j%3?i+4:x | (j%3)?(i+4):x | 5.0 (x가 double 형이기 때문에 자동으로 타입 변환됨.) | double |
goto문
goto a;
a:
- a의 위치로 이동한다.
#include <stdio.h>
#include <stdbool.h>
int main() {
int x = 1, y = 2;
if (x < y) goto print_y;
else goto print_x;
print_y:
printf("y가 더 크다\n");
goto ternary;
print_x:
printf("x가 더 크다\n");
ternary:
if (x < y) printf("%d\n", y);
else printf("%d\n", x);
}
y가 더 크다
2