简体   繁体   English

C:printf中的额外输出

[英]C: Extra output in printf

I've just started to learn C programming last week and I've learnt about some basics about it. 我上周刚开始学习C编程,并且了解了一些有关C的基础知识。 So now I'm trying to make a program which can add up two numbers and show the result. 因此,现在我正在尝试制作一个可以将两个数字相加并显示结果的程序。 Here's my code: 这是我的代码:

#include <stdio.h>

int main (void)
{
    int a;
    int b;
    int result;

    printf("Insert a number:%d\n");
    scanf ("%d",&a);

    printf ("Insert the next number:%d\n");
    scanf ("%d",&b);

    result = a + b;

    printf ("Result is:%d\n",result);
    return 0;

}

It can be compiled and run but the following result is shown. 可以编译并运行它,但显示以下结果。 [1] http://i.stack.imgur.com/4Xjdv.png [1] http://i.stack.imgur.com/4Xjdv.png

Can someone please help me to get rid of that 4200612, which is output at the first printf statement? 有人可以帮我摆脱在第一个printf语句中输出的4200612吗? Thanks for your help and sorry for my bad english. 感谢您的帮助,对不起我的英语不好。

There is no need of %d in first two printf statement. 前两个printf语句不需要%d

printf("Insert a number: ");
scanf ("%d",&a);
printf ("Insert the next number: ");
scanf ("%d",&b);

Since there is no corresponding argument. 由于没有相应的论点。 It will print some random value. 它将打印一些随机值。

Try getting rid of the extra %d's in your printfs. 尝试摆脱printfs中多余的%d。

#include <stdio.h>

int main (void)
{
    int a;
    int b;
    int result;

    printf("Insert a number:\n");
    scanf ("%d",&a);

    printf ("Insert the next number:\n");
    scanf ("%d",&b);

    result = a + b;

    printf ("Result is:%d\n",result);
    return 0;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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