scanf函数需要额外的参数,为什么呢? [英] scanf takes extra parameter why?

查看:395
本文介绍了scanf函数需要额外的参数,为什么呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的C ++&放大器;其发展我用来扫描取输入参数。但我给两个输入参数。但是,程序允许我输入额外的参数。请解释一下我为什么发生这种情况。请看以下code I I使用。

 的#include<&iostream的GT;INT主(INT ARGC,为const char * argv的[]){    诠释A,B;
    scanf函数(%I%I,&安培;一,和b);    的printf(A->%I,一);
    的printf(B->%I,B);    返回0;
}

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

  20
    三十
    40
    A-> 20b-> 30Program,退出code结束:0


解决方案

您有第二个%后,我在你的格式的空间。 scanf函数将读取额外的数据相匹配的空间。取出空间,如你预期它应该工作。

I'm new to c++ & its developing I used to scan to take input parameters. But I gave two input parameters. But program allows me to enter extra parameter. Please explain me why this happened. Please find below the code I I used.

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

Output (40 is allowed as an extra parameter)

20
    30
    40
    a-> 20b-> 30Program ended with exit code: 0

解决方案

You have a space in your format after the second %i. scanf will read extra data to match the space. Remove the space and it should work as you expected.

这篇关于scanf函数需要额外的参数,为什么呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆