Analysis of scanf function input in C language and scanf function in C Language

Source: Internet
Author: User

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);}       

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.