printf 여러가지 사용법

the c programming language - c언어 만든 사람이 쓴 책
c언어 표준 문서

1. the c programming language에서 처음 소개된 예제
#include <stdio.h>
// c언어는 입력 값이 없을 때 void를 써 줘야 한다.
//안그러면 어떤 값이 올지 모르는 unknown상태가 된다.
//c++은 void를 안써줘도 된다.
main(unknown) {
    printf("hello, world\n");
}

2.
#include <stdio.h> // 전처리지시자 헤더파일
전처리의 대상은 c코드
헤더파일에는 함수의 선언만이 존재

int main(void)
{
    printf("hello, world\n"); // f = format, \n = escape sequence
    return 0; // 0 프로그램이 정상적으로 종료되었다는 것을 의미한다.
}
escape sequence
ctrl + c, shift + q 등

3.
#include <stdio.h>

void main(void) // old c++ stytle
{
    printf("hello, world\n");
}

// 펌웨어 프로그램이 이런식으로 구성되어있다.
void main(void)
{
    setup();

    for(;;)
    {
        loop();
    }
}

4.
#include <stdio.h>

int main(void)
{
    printf("hello, "); // 가능한 함수 호출은 적게 하는 것이 좋다.
    printf("world");
    printf("\n");
    return 0;
}

5.
#include <stdio.h>

int main(void)
{
    printfc("hello, " "world\n"); // 하나의 문자열로 인식한다.
    return 0;
}

6.
#include <stdio.h>

int main(void)
{
    fprintf(stdout, "hello, world\n"); // printf 함수를 쓰면 내부적으로 fprintf 함수를 호출한다.
    return 0;
}

7.
// 7번과 8번 중에 8번을 써야 한다.
// 왜냐하면...
#include <stdio.h>

int main(void)
{
    char str[] = "hello";
    printf("%s", str);
    return 0;
}

8.
#include <stdio.h>

int main(void)
{
    char *str = "hello";
    printf("%s", str);
    return 0;
}

9.
#include <stdio.h>

int main(void)
{
    printf("%s\n", "hello");
    return 0;
}

Comments