变量在没有初始化的情况下被使用?C语言
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19602769/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Variable being used without being initialized? C Language
提问by karma2you
I can't quite figure out what my issue is here. I keep getting an error in my code.
我不太清楚我的问题是什么。我的代码中不断出现错误。
Error: Run-Time Check Failure: Variable used without being initialized. : warning C4700: uninitialized local variable 'b' used
错误:运行时检查失败:未初始化就使用了变量。:警告 C4700:使用了未初始化的局部变量“b”
can someone help me solve this problem ? Any help would be appreciated.I'm using visual studio as a compiler for C and I'm a beginner in it and this is a one of the assignment. I don't see why i keep getting this issue if i input "int b;" in the beginning of the program. Wouldn't that variable be initialized?
有人可以帮我解决这个问题吗?任何帮助将不胜感激。我使用 Visual Studio 作为 C 的编译器,我是它的初学者,这是一项任务。如果我输入“int b;”,我不明白为什么我总是遇到这个问题 在节目的开头。那个变量不会被初始化吗?
Here is the code:
这是代码:
#include <stdio.h>
//Create a program that asks the user to enter a number until the user enters a -1 to stop
int main()
{
int b;
//as long as the number is not -1, print the number on the screen
while(b!=-1) {
printf("Hello there! would you please enter a number?");
scanf(" %d",&b);
//as long as the number is not -1, print the number on the screen
if(b!=-1){
printf("Thank you for your time and consideration but the following %d you entered wasn't quite what we expected. Can you please enter another?\n",b);
//When the user enters a -1 print the message “Have a Nice Day :)” and end the program
}else {
printf("Have a Nice Day :), and see you soon\n");
}
}
return 0;
}
回答by Code Monkey
When you declare a variable, such as you have:
当你声明一个变量时,比如你有:
int b;
It is not initialised to have any value, it's value is unknown until you initialise it.
它没有初始化为具有任何值,它的值在您初始化之前是未知的。
To fix this error, replace
要修复此错误,请替换
int b;
With
和
int b = 0;
回答by sukhvir
Error is here:
错误在这里:
int main()
{
int b;
//as long as the number is not -1, print the number on the screen
while(b!=-1) {
Since you haven't initialized b, it can be anything. You then use it as a condition for whileloop. This is very dangerous.
因为你还没有初始化b,它可以是任何东西。然后将其用作while循环的条件。这是非常危险的。
It may be that system randomly assign value of -1( its a rare possibility ) to it .. in that case your whileloop will not be actioned
可能是系统随机为其分配值-1(这是一种罕见的可能性)..在这种情况下,您的while循环将不会被执行
Intialize bto some value
初始化b为某个值
For eg do this:
例如,这样做:
int b = 0;
回答by Crowman
You're doing:
你正在做的:
int b;
and then doing:
然后做:
while(b!=-1) {
without ever initializing b. The problem is exactly what your warning is telling you it is.
无需初始化b。问题正是您的警告告诉您的问题。
C does not automatically initialize local variables for you, the programmer has to take care of that. int ballocates memory for your variable, but doesn't put a value in there, and it will contain whatever garbage value was in that memory prior to allocation. Your variable won't be initialized until you explicit assign, or another function explicitly assigns, a value to it.
C 不会自动为您初始化局部变量,程序员必须注意这一点。int b为您的变量分配内存,但不会在其中放置值,并且它将包含分配之前该内存中的任何垃圾值。您的变量不会被初始化,直到您显式分配或另一个函数为其显式分配一个值。
回答by kfsone
int b;
is a variable declaration. Explicitly, the value is not initialized. The compiler will emit instructions for the program to reserve space to store an integer at a later time.
是一个变量声明。明确地,该值未初始化。编译器会为程序发出指令,以便在以后保留空间来存储整数。
int b = 1;
this is a variable declaration with initialization.
这是一个带初始化的变量声明。
int b;
while (b != -1)
this is use of an uninitialized variable, but so is
这是使用未初始化的变量,但也是如此
int a = rand() % 3; // so 'a' can be 0, 1 and or 2.
int b;
if (a == 0)
b = 1;
else if (a == 1)
b = 2;
printf("b = %d\n", b);
this is also a potential cause of uninitialized use of b. If 'a' is 2, we never assign a default value to b.
这也是未初始化使用 b 的潜在原因。如果 'a' 是 2,我们永远不会为 b 分配一个默认值。
Upshot is you should always try to specify the default value with the declaration. If the logic that will determine initialization is complex, consider using an out-of-bounds value, as you are using -1.
结果是您应该始终尝试在声明中指定默认值。如果确定初始化的逻辑很复杂,请考虑使用越界值,就像使用 -1 一样。
Can you spot the bug in the following?
你能找出下面的错误吗?
int b = -1;
if (a == 0)
b = 1;
else if (a == 1)
b = 2;
else if (a > 2)
b = 3;
if (b == -1) {
// this is an error, handle it.
}

