diff --git a/08_03_printf/README.md b/08_03_printf/README.md new file mode 100644 index 0000000000000000000000000000000000000000..dd67f9ae3a221ea95e0bb4fc3ac68ff61518ac96 --- /dev/null +++ b/08_03_printf/README.md @@ -0,0 +1,62 @@ +# Personal Project1 : printf(2020.08.03) + +## stdio.h + +### 표준 스트림 +* 표준 스트림은 Standard Stream의 약자로 컴퓨터 프로그램에서 **표준적으로** 입력으로 받고 출력으로 보내는 데이터와 매체를 총칭하는 용어 +* 표준 입력은 프로그램에 입력되는 데이터의 표준적인 출처로 stdin라고 표현 +* 표준 출력은 프로그램에서 출력되는 데이터의 표준적인 방향으로 크게 stdout과 stderr로 구분 + +### stdin, stdout, stderr +1. stdin(standart input)-0 + * 리눅스에서 표준 입력은 file descriptor가 0으로 설정 + * C, C++ + ```C + //C + FILE* stdin + ``` + ```C++ + //C++ + std::cin + ``` +2. stdout(standard output)-1 + * 정상적인 출력이 반환되는 방향 + * 리눅스에서 표준 출력은 file descriptor가 1로 설정 + * C, C++ + ```C + //C + FILE* stdout + ``` + ```C++ + //C++ + std::cout + ``` +3. stderr(standard error)-2 + * 프로그램의 비정상 종료시에 반환되는 방향 + * 리눅스에서 표준 출력은 file descriptor가 2로 설정 + * C, C++ + ```C + //C + FILE* stderr + ``` + ```C++ + //C++ + std::cerr + ``` + +## Redirection +* 표준 입력이나 표준 출력을 키보드나 화면으로 하는 것이 아니라 파일로 입력받거나 파일로 출력하도록 변경하는 것 +* `> file` + * 표준 출력을 파일로 redirection + * 파일이 없으면 새로 만들고, 파일이 있으면 덮어쓴다. +* `>> file` + * 표준 출력을 파일로 redirection + * 파일이 없으면 새로 만들고, 파일이 있으면 파일의 끝에 덧붙인다. +* `2>&1` + * 표준 에러를 표준 출력으로 redirection + * 표준 에러도 표준 출력의 자격으로 보내진다. +* `< file` + * 파일로부터 표준 입력을 받도록 redirection +* ex) + * ./a.out < input_data > output_data + 입력값은 input_data에서 받고 output_data에 출력된다. diff --git a/08_03_printf/hello.c b/08_03_printf/hello.c new file mode 100644 index 0000000000000000000000000000000000000000..aefbe1fc4783689d363b362c1513c149d874ede5 --- /dev/null +++ b/08_03_printf/hello.c @@ -0,0 +1,12 @@ +#include <stdio.h> + +int main() +{ + int a; + fscanf(stdin, "%d", &a); + if(a > 0) + fprintf(stderr, "Error: It is positive.\n"); + else + fprintf(stderr, "Error: It is negative.\n"); + fprintf(stdout, "Hello, World %-10d\n", a); +} diff --git a/08_03_printf/input_data b/08_03_printf/input_data new file mode 100644 index 0000000000000000000000000000000000000000..08839f6bb296e888d311d8ea2f35f7ff82dd3f2d --- /dev/null +++ b/08_03_printf/input_data @@ -0,0 +1 @@ +200 diff --git a/08_03_printf/output_data b/08_03_printf/output_data new file mode 100644 index 0000000000000000000000000000000000000000..4d6b988819ba436af54cea6e49d6f871cea244a9 --- /dev/null +++ b/08_03_printf/output_data @@ -0,0 +1 @@ +Hello, World 200 diff --git a/08_03_printf/stderr.txt b/08_03_printf/stderr.txt new file mode 100644 index 0000000000000000000000000000000000000000..2864cf698a16205b3d5b1292ca92b51c6c7752fd --- /dev/null +++ b/08_03_printf/stderr.txt @@ -0,0 +1 @@ +Error: It is positive. diff --git a/08_03_printf/stdout.txt b/08_03_printf/stdout.txt new file mode 100644 index 0000000000000000000000000000000000000000..5acd122c2a122cb0b374743c907a702cac22cd2a --- /dev/null +++ b/08_03_printf/stdout.txt @@ -0,0 +1 @@ +Hello, World 2000