scanf
function also has a letter f
,formatted
as printf
meaning that it can specify the format. scanf
function accepts input and is a standard library function like printf
, so when using it, #include <stdio.h>
must be declared at the beginning. Unless otherwise specified for the input device, scanf
input comes through the standard input system, and usually receives keyboard input. The function refers to the memory address of the variable to assign the input (About the memory addresses are covered later). It passes the address of a variable, an array, or a structure, and is classified by a &
mark.
scanf
함수는 printf
함수와 동일하게 f
, formatted
즉 서식을 지정할 수 있다. scanf
함수는 입력을 받아들이는 함수이며, printf
와 동일하게 표준 라이브러리 함수이기 때문에 사용시 #include <stdio.h>
를 서두에 선언해야 한다. 입력 장치에 대한 지정을 특별히 하지 않았다면, scanf
의 입력은 표준입력체계를 통해 들어오며, 보통 기보드 입력을 받는다. 함수는 받은 입력을 할당하기 위해 변수의 메모리 주소를 참조한다 (메모리 주소에 대한 내용은 추후 다룬다). 변수나 배열, 구조체의 주소를 전달하며 &
표시를 통해 구분한다.
In the case of visual stdio, a warning appears when compiling, and a warning to use scanf_s appears. To ignore the warning, write #define _CRT_SECURE_NO_WARNINGS
or write #programa waring(disable:4996)
.
When receiving the int
data type as input, the %d
format specifier is used, and each input is separated by white space (space, tab, enter). When receiving char
data type as input, use the %c
format specifier. When receiving string
as input, %s
is used, and in the case of characters, all white spaces are classified as input.
visual stdio 의 경우 컴파일 시 경고가 타나다고, scanf_s를 사용하라는 경고가 나타난다. 경고를 무시하기 위해 #define _CRT_SECURE_NO_WARNINGS
를 적어주거나 #programa waring(disable:4996)
을 적어주면 된다.
int
자료형을 입력으로 받을 때는 %d
포맷지시자를 사용하고 각각의 입력은 White space(띄어쓰기, 탭, 엔터)로 구분된다. char
자료형을 입력으로 받을 때는 %c
포맷지시자를 사용한다. 문자열을 입력으로 받을 때에는 %s
를 사용하며 문자의 경우 White space가 모두 입력으로 분류된다.
#include <stdio.h>
int main(void){
int a;
char b;
char c[10];
scanf("%d", &a);
scanf("%c", &b);
scanf("%s", c);
printf("\n");
printf("%d\n", a);
printf("%c\n", b);
printf("%s\n", c);
return 0;
}
4s spring
4
s
spring