返回不同答案的c程序
我刚刚开始学习 C,想尝试一下欧拉问题。
对于 Euler #2
#include <stdio.h>
int main(){
int sum;
int prev = 0;
int curr = 1;
int next;
do{
if(curr % 2 == 0 ){
sum = sum + curr;
}
next = curr + prev;
prev = curr;
curr = next;
} while (curr <= 4000000);
printf("%d\n", sum);
return 0;
}
当我编译这个程序并运行它时,我得到一个完全不同的数字。我现在不在乎我是否得到正确的答案,因为我得到的数字有时会相差 1,000,000。
我唯一能想到的两件事是,在虚拟机中运行 linux 让它变得疯狂,或者 gcc 搞砸了。
g++ euler2.c -o euler2
编译 c 正确吗?是的 euler2.c 是我的文件名。
已解决:感谢您的回复。绝对有很多有用且非常快速的信息。是的,我应该添加“我可能搞砸了”的可能性=)
I just started C and wanted to try out a euler problem.
For Euler #2
#include <stdio.h>
int main(){
int sum;
int prev = 0;
int curr = 1;
int next;
do{
if(curr % 2 == 0 ){
sum = sum + curr;
}
next = curr + prev;
prev = curr;
curr = next;
} while (curr <= 4000000);
printf("%d\n", sum);
return 0;
}
When I compile this program and run it, I get a completely different number. I don't care right now that I'm not getting the right answer as much as the numbers I am getting are varying by 1,000,000 at times.
The only 2 things I can think of is that running linux in a vm is making it crazy or somehow gcc is messing up.
Is g++ euler2.c -o euler2
correct for compiling c? Yes euler2.c is the name of my file.
SOLVED: Thanks for the replies. Definitely a lot of useful, and extremely quick information. And yes I should have added the possibility that "I might have messed up" =)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果它没有初始化(在
c
中),它包含随机垃圾值。因此,随机输出和
g++ 适用于 c++,但这与您的问题没有任何关系。
您可能会发现这很有趣: http://gcc.gnu.org/onlinedocs/gcc /调用-G_002b_002b.html
If it is not initialized (in
c
) it contains random-garbage value. hence the random outputand
g++ is for c++, but this does not have anything to do with your problem.
You might find this interesting : http://gcc.gnu.org/onlinedocs/gcc/Invoking-G_002b_002b.html
因为
sum
没有初始化。编译器不会像人们所期望的那样将堆栈上的变量初始化为零。相反,内存是为变量分配的,但变量的值采用占用分配内存的任何内容(实际上是随机的)。像对堆栈上的其他变量所做的那样初始化
sum
。Because
sum
is not initialized. The compiler will not initialize variables on the stack to be zero, as one might expect. Instead, memory is allocated for the variable, but the value of the variable takes on whatever contents occupy the allocated memory (which is effectively random).Initialize
sum
as you did for the other variables on the stack.您的变量
sum
未初始化,因此它不仅以任意值开始,而且“使用”该值实际上是未定义的行为,此时任何事情都可能发生。编写
int sum = 0;
(并习惯这样做!)此外,
g++
是 C++ 编译器包装器; 您需要gcc
。最后,您对数据类型
int
的范围做出假设。适合您的范围取决于平台;它可以小到 [-32768, +32767],或者像大多数现代 32 位系统上那样,它可以是 [-2147483648, +2147483647]。您可以通过指定您想要一个
unsigned int
来稍微缓解这种情况,它通常使最大值加倍(通过禁止负值),但您也许应该查看 stdint.h,如果您需要特定范围。Your variable
sum
is uninitialised, so not only is it starting off with an arbitrary value, but "using" that value is actually Undefined Behaviour, at which point anything can happen.Write
int sum = 0;
(and get used to doing this!)Also,
g++
is the C++ compiler wrapper; you wantgcc
.Finally, you're making assumptions about the range of the data type
int
. It's platform dependent as to what the range will be for you; it could be as small as [-32768, +32767] or, as found on the majority of modern 32-bit systems, it could be [-2147483648, +2147483647].You can mitigate this slightly by specifying that you want an
unsigned int
which usually doubles the maximum value (by disallowing negative values), but you perhaps ought to look into the fixed-width types available in stdint.h, if you need a specific range.sum 没有初始化,所以它的值不是 0,而是一些垃圾。初始化时添加 sum = 0。
sum is not initialized, so its value is not 0, but some garbage. add sum = 0 at initialization.
您没有初始化变量“sum”
you aren't initializing the variable 'sum'
您没有初始化您的 sum 变量,我怀疑它因此包含任意值。
You do not initialize your sum variable, I suspect it thus contains an arbitrary value.
默认情况下,只有全局变量为 0,如果不初始化函数,函数中的所有内容都会被分配任何。
Only global variables are 0 by default, everything in a function will be assigned anything if you don't initialize it.