简体   繁体   中英

how does scanf() check if the input is an integer or character?

I am wondering how does the standard C library function scanf() check if the input is an integer or a character when we call scanf("%d",&var) when a character itself is just a number? I know that when it encounters a non-integer it puts it back into the input buffer and returns a -1 but how does it know that the input is not an integer?

The input is always a string. If scanf is expecting an integer (because you passed it "%d" ), it attempts to convert the string to an integer for you.

You're correct in that each character is really represented as an 8-bit integer. The solution is simple: look at that number, and see if it is in the range 48-57, which is the range of SCII codes for the characters '0' - '9'.

Starting on line 1315 of the scanf() source code we can see this in action. scanf() is actually more complicated, though - it also looks at multi-byte characters to determine the numeric value. Line 1740 is where the magic happens and that character is actually converted into a number. Finally, and possibly this is the most useful, the strtol() function does the looping to perform that conversion.

在scanf语句中,你提到%d只保存整数,因此默认情况下它将变量理解为整数

Basically, the scanf function matches regular expressions based on the conversion specifier you pass. If you specify %d , that tells scanf to match input against a regular expression that's one or more characters between '0' and '9' (optionally with a leading + or - character). It then converts that sequence of characters to the equivalent integer value.

A very simplistic version might look something like this:

while (isdigit(c = fgetc(stream))
  val = val * 10 + valueOf(c); 
ungetc(c, stream);

where isdigit is a standard library function that returns true (non-zero) if the character value represents a decimal digit, and valueOf is a user-defined function that maps the character representing an integer ( '0' - '9' ) to the equivalent integer value ( 0 - 9 ) (I'm not aware of a standard library function that does that for individual character values). Why not just subtract '0' from c to get the equivalent integer value? Depending on the encoding, it's not guaranteed that all decimal integer characters will be laid out in order (All The World Is Not ASCII); best to delegate the actual conversion to a function that's aware of the current encoding.

它尝试从流的开头解析有效的十进制整数,实际上是可选的+或 - 然后是一个或多个数字的序列。

It basically uses a loop like:

while (isdigit((ch=getchar()))
    // ...

You misunderstand the %d format specifier. It does not return the ASCII value of the entered character (you can use %c for that); it returns the integer that the characters represent. So, on an input character of '9' , %d returns the value 9 - not the ASCII value of '9' .

(Well, %d actually looks at a sequence of characters, so if the input is '9' followed by '0' followed by ' ' , it'll interpret that as 90).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM