添加输出不正确 [英] The addition output is not correct
本文介绍了添加输出不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#包括< stdio.h中>
main()
{
int a,b,c = a + b;
printf(输入a \ n的值);
scanf(%d,& a);
printf(输入b\ n的值);
scanf(%d,& b);
printf(a + b =%d,c);
system(暂停);
}
我的尝试:
我尝试使用不同的输入,但输出显示1总是针对不同的输入
解决方案
代码与您编写时一样工作。
发生的事情是您定义了变量a,b和c。变量a和b没有值,但应该是0.你告诉编译器将a和b加在一起并将结果赋给c。
Wellllllll,that代码只能工作一次。你并没有告诉编译器使用它作为一个公式,说这就是你总是如何确定c的值。没有在C或我知道的任何其他语言中定义这样的公式。
你必须运行代码才能在你之后进行计算从用户那里得到a和b的值。
int a,b,c;
// 获取a和b的值...
c = a + b;
// 显示结果...
与浮点变量的输出为0.0000 [ ^ ]
好悲伤,但你学习速度慢...
与上一个问题相同,双引号位于错误的位置。改变这个:scanf(%d,& b);对此:
scanf(%d,& b);并尝试记住这一次! :笑:
#include<stdio.h> main() { int a,b,c=a+b; printf("Enter the value for a\n"); scanf("%d, &a"); printf("Enter the value for b\n"); scanf("%d, &b"); printf("a+b=%d", c); system ("pause"); }
What I have tried:
I tried with different inputs but the output is showing 1 always for different inputs
解决方案
The code works as you wrote it.
What is happening is that you defined the variables a, b, and c. The variables a and b have no values, but should be 0. You told the compiler to add a and b together and assign the result to c.
Wellllllll, that code only works once. You're NOT telling the compiler to use this as a formula that say "this is how you always determine the value of c". There is no such thing as defining a formula like this in C, or any other language that I know of.
You have to run code that makes that calculation AFTER you get the values of a and b from the user.
int a, b, c; // Get the values for a and b... c = a + b; // Show the result...
Exactly the same error on your part as The output for float variable is 0.0000[^]
Good grief, but you are a slow learner...
Same as your last question, the double quotes are in the wrong place. Change this:scanf("%d, &b");To this:
scanf("%d", &b);And try to remember this time! :laugh:
这篇关于添加输出不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文