scanf需要额外的参数为什么?

war*_*una 2 c c++ scanf

我是c ++的新手,我开发用于扫描以获取输入参数.但我给了两个输入参数.但程序允许我输入额外的参数.请解释一下为什么会这样.请在下面找到使用的代码II.

#include <iostream>

int main(int argc, const char * argv[]) {

    int a,b;


    scanf("%i %i ",&a,&b);

    printf("a-> %i",a);
    printf("b-> %i",b);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出(允许40作为额外参数)

20
    30
    40
    a-> 20b-> 30Program ended with exit code: 0
Run Code Online (Sandbox Code Playgroud)

R S*_*ahu 6

scanf需要额外的参数为什么?

这不是一个准确的结论.额外输入保留在输入流中.它不被消费scanf.它在程序结束时被丢弃.

如果你有另scanf一条线,

scanf("%d", &c);
Run Code Online (Sandbox Code Playgroud)

该值40将被读入c.