Analysis of scanf function input in C language and scanf function in C Language
When defining the scanf function in C language, the header file stdio. h is already included. Therefore, you do not need to add a header file when using scanf.
int main(void){ char a; scanf("%c",&a); printf("%c\n",a); return 0;}
There is no problem at all.
Scanf functions do not have precision control, for example:Scanf ("% 4.8f", & a) is invalid
However, the width is controllable, for example:Scanf ("% 3d", & a) is completely usable
The interval between input data is available when multiple values are entered.Space, TAB, or enter
When the program is compiled with spaces, TAB keys, enter keys, and invalid data (for example, if "% d" is entered as "3 H", H is an invalid character), the data ends.
Possible problems:
(1)
Int a, B; scanf ("% 3d % 3d", & a, & B); // input 123456789. The program assigns 123 to a, 456 to B, the rest is intercepted.
(2)
Char a, B; scanf ("% c", & a, & B); printf ("% c \ n", a, B ); // there is no space between "% c" in the scanf function. When q w (q space w) is input, the output result is only q // If qw is input, the output result is qw.
(3)
The scanf implementation source code is attached.
/****scanf.c - read formatted data from stdin** Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.**Purpose:* defines scanf() - reads formatted data from stdin********************************************************************************/#include #include #include #include #include #include #include /****int scanf(format, ...) - read formatted data from stdin**Purpose:* Reads formatted data from stdin into arguments. _input does the real* work here.**Entry:* char *format - format string* followed by list of pointers to storage for the data read. The number* and type are controlled by the format string.**Exit:* returns number of fields read and assigned**Exceptions:********************************************************************************/int __cdecl scanf ( const char *format, ... )/* * stdin 'SCAN', 'F'ormatted */{ int retval; va_list arglist; va_start(arglist, format); _ASSERTE(format != NULL); _lock_str2(0, stdin); retval = (_input(stdin,format,arglist)); _unlock_str2(0, stdin); return(retval);}