Standard I/O
Use of printf() and scanf()
 printf()는 Print Format의 약어로, 화면에 내용을 출력할 때 사
용하는 명령(함수)이다.
printf(“format string..”, argument list);

[Ex]
#include <stdio.h>
int main() {
int n ;
n = 25 * 10;
printf(“%d”, n);
}
[Ex]
printf(“%c%c%c”, ’a’, ’b’, ’c’ );
printf(“%s”, “def” );

/* 250 이 출력된다. */

/* abc 가 출력된다. */
/* def 가 출력된다. */

2
Use of printf() and scanf()
 Conversion characters (format string)
%c

character (문자)

printf(“%c”, ’a’);

%d

decimal integer (10진수로출력) printf(“%d”, 100);

%x

Hexadecimal integer
(16진수로출력)

printf(“%x”, 100);

%o

Octal integer (8진수로출력)

printf(“%o”, 100);

%u

Unsigned decimal (양수로출력)

printf(“%u”, 100);

%e

floating point number in
scientific notation

printf(“%e”, 1.234);

%f

floating point number

printf(“%f”, 1.234);

%g

e-format또는 f-format

printf(“%g”, 1.234);

%s

string (문자열)

printf(“%s”, ”C-book”);
3
Use of printf() and scanf()
 The Use of printf() : integer %d
printf(“ %md”, a );
printf(“ %-md”, a);

/*m 정수.오른쪽정렬 */
/*m 정수. 왼쪽정렬 */

[Ex]
a = 12;
printf(“%5d%-5d%dn” ,a, a, a+11);

12^^^^^^1223

– %5d로 출력 : 12^^^
( 5자리에 왼쪽정렬로 출력, 뒤쪽 공간이 남으면 공백으로 채움)
– %-5d로 출력 : ^^^12
( 5자리에 오른쪽정렬로 출력, 앞에 공간이 남으면 공백으로 채움)
– %d로 출력 : 23
(a +11의 값 ‘23’을 그대로 출력)

4
Use of printf() and scanf()
 The Use of printf() : float %f
printf(“ %m.pf”, a );
printf(“ %-m.pf”, a);

/*m, p 정수.오른쪽정렬 */
/*m, p 정수. 왼쪽정렬 */

[Ex]
83.1260^^83.126083.126000^^^83.13
a = 83.126;
printf(“%8.4f%-8.4f%f%-7.2fn”, a, a, a, a );
– %8.4f : 83.1260^
(소수점을 포함 총 8자리에 숫자를 출력하되 소수점 이하 4자리까
지 표시. )
– %-8.4f : ^83.1260
(%8.4f와 동일 , 그러나 음수로 인해 오른쪽으로 정렬)
– %f
: 83.126000
(칸수 제한 없이 소수점 이하 6자로 출력)
– %-7.2f : ^^^83.13
(7자리 확보에 소수점 이하 2자리-반올림, 오른쪽 정렬)
5
Use of printf() and scanf()
 Use of scanf()
– scanf() - key board로부터 data를 입력 받기 위해 사용하는 함수이
다.
scanf(“format string..”, argument list);
%c

character (문자)

scanf(“%c”, &a);

%d

decimal integer (10진수)

scanf(“%d”, &a);

%f

floating point number (float)

scanf(“%f”, &a);

%lf

floating point number (double)

scanf(“%lf”, &a);

%Lf

floating point number (long double)

scanf(“%Lf”, &a);

%s

string (문자열)

scanf(“%s”, &a);

6
Use of printf() and scanf()
[Ex]
#include <stdio.h>
int main() {
int n ;
printf(“Enter number : “);
scanf(“%d”, &n);
printf(“You entered : %d”, n);
return 0;
}

Enter number : 10
You entered : 10
7

2 3. standard io

  • 1.
  • 2.
    Use of printf()and scanf()  printf()는 Print Format의 약어로, 화면에 내용을 출력할 때 사 용하는 명령(함수)이다. printf(“format string..”, argument list); [Ex] #include <stdio.h> int main() { int n ; n = 25 * 10; printf(“%d”, n); } [Ex] printf(“%c%c%c”, ’a’, ’b’, ’c’ ); printf(“%s”, “def” ); /* 250 이 출력된다. */ /* abc 가 출력된다. */ /* def 가 출력된다. */ 2
  • 3.
    Use of printf()and scanf()  Conversion characters (format string) %c character (문자) printf(“%c”, ’a’); %d decimal integer (10진수로출력) printf(“%d”, 100); %x Hexadecimal integer (16진수로출력) printf(“%x”, 100); %o Octal integer (8진수로출력) printf(“%o”, 100); %u Unsigned decimal (양수로출력) printf(“%u”, 100); %e floating point number in scientific notation printf(“%e”, 1.234); %f floating point number printf(“%f”, 1.234); %g e-format또는 f-format printf(“%g”, 1.234); %s string (문자열) printf(“%s”, ”C-book”); 3
  • 4.
    Use of printf()and scanf()  The Use of printf() : integer %d printf(“ %md”, a ); printf(“ %-md”, a); /*m 정수.오른쪽정렬 */ /*m 정수. 왼쪽정렬 */ [Ex] a = 12; printf(“%5d%-5d%dn” ,a, a, a+11); 12^^^^^^1223 – %5d로 출력 : 12^^^ ( 5자리에 왼쪽정렬로 출력, 뒤쪽 공간이 남으면 공백으로 채움) – %-5d로 출력 : ^^^12 ( 5자리에 오른쪽정렬로 출력, 앞에 공간이 남으면 공백으로 채움) – %d로 출력 : 23 (a +11의 값 ‘23’을 그대로 출력) 4
  • 5.
    Use of printf()and scanf()  The Use of printf() : float %f printf(“ %m.pf”, a ); printf(“ %-m.pf”, a); /*m, p 정수.오른쪽정렬 */ /*m, p 정수. 왼쪽정렬 */ [Ex] 83.1260^^83.126083.126000^^^83.13 a = 83.126; printf(“%8.4f%-8.4f%f%-7.2fn”, a, a, a, a ); – %8.4f : 83.1260^ (소수점을 포함 총 8자리에 숫자를 출력하되 소수점 이하 4자리까 지 표시. ) – %-8.4f : ^83.1260 (%8.4f와 동일 , 그러나 음수로 인해 오른쪽으로 정렬) – %f : 83.126000 (칸수 제한 없이 소수점 이하 6자로 출력) – %-7.2f : ^^^83.13 (7자리 확보에 소수점 이하 2자리-반올림, 오른쪽 정렬) 5
  • 6.
    Use of printf()and scanf()  Use of scanf() – scanf() - key board로부터 data를 입력 받기 위해 사용하는 함수이 다. scanf(“format string..”, argument list); %c character (문자) scanf(“%c”, &a); %d decimal integer (10진수) scanf(“%d”, &a); %f floating point number (float) scanf(“%f”, &a); %lf floating point number (double) scanf(“%lf”, &a); %Lf floating point number (long double) scanf(“%Lf”, &a); %s string (문자열) scanf(“%s”, &a); 6
  • 7.
    Use of printf()and scanf() [Ex] #include <stdio.h> int main() { int n ; printf(“Enter number : “); scanf(“%d”, &n); printf(“You entered : %d”, n); return 0; } Enter number : 10 You entered : 10 7