본문 바로가기
타입스크립트로 함께하는 웹 풀 사이클 개발(React, Node.js)/TIL

데브코스 11주차 Day 2 - 연산자 / 조건문 with C

by 슈크림 붕어빵 2024. 1. 25.

1. 연산자

산술 연산자 

  • 사칙 연산이 기본
  • +, -, /, *, %

대입연산자

  • = , += , -= , *= , /= , %=

증감 연산자

  • a++, ++a, a--, --a
#include <stdio.h>

int main() {
    int a=5;
    printf("%d ",++a);
    printf("%d ",a++);
    printf("%d ",a);
    return 0;
}

 

=> 출력 : 6 6 7

 

관계 연산자

  • ==, !=, > , <, >= , <=

논리 연산자

  • 두개의 조건식 등을 결합하여 하나의 결과값을 만들어낸다.
  • ! , && , ||
int main() {
    int a=5;
    printf("%d", !a);
    return 0;
}

=> 출력: 0

 

 예제 문제- 합 구하기(연산자와 입출력)

#include <stdio.h>

int main() {
    int a,b,c;
    scanf("%d %d %d", &a, &b, &c);
    printf("%d",a+b+c);
    return 0;
}

 

2. 조건문

  • 관계연산자로 분기

예제 문제 - 내림차순으로 정렬 

#include <stdio.h>

int main() {
    int a, b, c;

    scanf("%d %d %d", &a, &b, &c);

    if (a < b) {
        int temp = a;
        a = b;
        b = temp;
    }
    if (a < c) {
        int temp = a;
        a = c;
        c = temp;
    }
    if (b < c) {
        int temp = b;
        b = c;
        c = temp;
    }

    printf("내림차순 정렬: %d, %d, %d\n", a, b, c);

    return 0;
}