시작

hello, world를 출력하는 c코드는 다음과 같다.

#include <stdio.h>
main ()
{
    printf("hello, world\n");
}
📑 Note
  • main 함수를 진입점으로 인식한다.

  • 함수는 ()로 매개변수를 받는다.

  • {}는 statement들을 하나로 묶는다.

  • 함수 호출은 함수명과 그 매개변수들을 나열하는 것으로 이루어진다.

  • "hello, world\n"은 문자열인데 줄바꿈이 허용되지 않는다.

  • \n으로 줄바꿈이 이루어진다.

변수와 수식표현

화씨 온도를 섭씨 온도로 변경하는 프로그램을 작성한다. 화씨와 섭씨의 관계는 다음과 같다.

59(°F32)=°C\begin{align}\frac{5}{9} (°\mathbf{F} -32) = °\mathbf{C}\end{align}
#include <stdio.h>
main()
{
    int fahr, celsius;
    int lower, upper, step;

    lower = 0;
    upper = 300;
    step = 20;
    fahr = lower;
    while (fahr <= upper) {
        celsius = 5 * (fahr - 32) / 9;
        printf("%d\t%d\n", fahr, celsius);
        fahr = fahr + step;
    }
}
📑 Note
  • 변수는 사용전에 선언되어야 한다.

    • 변수 선언시 타입을 정의한다.

    • printf%d를 통해 변수값을 문자열에 넣을 수 있다.

연습 1-3

#include <stdio.h>
main()
{
    int fahr, celsius;
    int lower, upper, step;

    lower = 0;
    upper = 300;
    step = 20;
    fahr = lower;
    printf("화씨에서 섭씨로 전환");
    while (fahr <= upper) {
        celsius = 5 * (fahr - 32) / 9;
        printf("%d\t%d\n", fahr, celsius);
        fahr = fahr + step;
    }
}

연습 1-4

#include <stdio.h>
main()
{
    int fahr, celsius;
    int lower, upper, step;

    lower = 0;
    upper = 300;
    step = 20;
    celsius = lower;
    while (celsius <= upper) {
        fahr = 9 * celsius / 5 + 32;
        printf("%d\t%d\n", celsius, fahr);
        celsius = celsius + step;
    }
}