Why reading '+' and '-' not work in Linux?

后端 未结 3 1216
一个人的身影
一个人的身影 2021-01-15 10:25

I have this fragment of code that reads arithmetic expressions like 1 + 2 * 3 into integers and characters:

int main() {
    int d, flag = 0;
           


        
3条回答
  •  自闭症患者
    2021-01-15 10:47

    Jean-François Fabre correctly explained what actually happens, but my opinion is that it is simply unspecified by the standard what should happen in that case.

    Draft n1570 for C11 says at 7.21.6.2 The fscanf function

    12 The conversion specifiers and their meanings are:
    d Matches an optionally signed decimal integer, whose format is the same as expected for the subject sequence of the strtol function with the value 10 for the base argument. The corresponding argument shall be a pointer to signed integer.
    ...

    and strtol is described in 7.22.1.4 The strtol, strtoll, strtoul, and strtoull functions (emphasize mine)

    Description
    2 The strtol, strtoll, strtoul, and strtoull functions convert the initial portion of the string pointed to by nptr to long int, long long int, unsigned long int, and unsigned long long int representation, respectively. First, they decompose the input string into three parts: an initial, possibly empty, sequence of white-space characters (as specified by the isspace function), a subject sequence resembling an integer represented in some radix determined by the value of base, and a final string of one or more unrecognized characters, including the terminating null character of the input string. Then, they attempt to convert the subject sequence to an integer, and return the result.

    I could not find anywhere in the standard what exactly could resemble a decimal integer. It is clear that positive and negative number do, and that numbers prefixed with a plus sign (+) also do. But it is not specified whether the plus and minus signs (+ and -) alone do resemble a decimal integer or not.

    If an implementation decides that they do, a %d specifier will eat alone + and - signs, if it decides that they do not, it will leave them in the stream.

提交回复
热议问题