C语言 如何检查变量是否已在 C 中初始化?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36490948/
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
How to check if a variable has been initialized in C?
提问by Hafiz Temuri
Is there a way to check if a variable has been initialized or not in C?
有没有办法检查变量是否已在 C 中初始化?
Consider the following example,
考虑下面的例子,
int main(){
int a = 3, b = 7, c;
if ( a > b )
c = a-b;
// Now, how can I check if the variable "c" has some value or not
// I don't want check like this,
// if ( isalpha(c) ) or if ( isdigit(c) )
// or anything similar to like that
}
In other words, does C has some function like definedin Perl. In Perl, I can simply do if (defined c)that would check if the variable is defined or not, and it'd return False for above example. How can I achieve the same in C?
换句话说,C 有没有像definedin那样的函数Perl。在 中Perl,我可以简单地if (defined c)检查变量是否已定义,并且对于上面的示例,它会返回 False。我怎样才能在 C 中实现相同的目标?
回答by Bill Lynch
C does not have this ability. You have two main options:
C没有这个能力。您有两个主要选择:
A sentinel value
哨兵值
For example, if you know that the value of cwill never be negative, then initialize it to -1, and test that for that.
例如,如果您知道 的值c永远不会为负,则将其初始化为-1,并为此进行测试。
Add another variable
添加另一个变量
Create another variable bool we_set_c_to_something = false;and then set it to truewhen you write to c.
创建另一个变量bool we_set_c_to_something = false;,然后true在写入时将其设置为c.
回答by Graham
Wrong question. You're not asking whether the variable is defined. If the variable is not defined then compilation fails. Look up the difference between "declaration" and "definition". In the case of those local variables, you havedefined the variable c.
错误的问题。您不是在问变量是否已定义。如果未定义变量,则编译失败。查看“声明”和“定义”之间的区别。对于那些局部变量,您已经定义了变量 c。
What you're looking for is initialisation. Many compilers will warn you about using variables before they're initialised, but if you persist in running that code then the assumption is that you know better than the compiler. And at that point it's your problem. :) Some languages (e.g. Perl) have an extra flag that travels along with a variable to say whether it's been initialised or not, and they hide from you that there's this extra flag hanging around which you may or may not need. If you want this in C, you need to code it yourself.
您正在寻找的是初始化。许多编译器会在变量初始化之前警告您使用变量,但是如果您坚持运行该代码,那么假设您比编译器更了解。在这一点上,这是你的问题。:) 某些语言(例如 Perl)有一个额外的标志,它与一个变量一起传送,以说明它是否已初始化,并且它们向您隐藏了这个额外的标志,您可能需要也可能不需要。如果你想用 C 语言,你需要自己编写代码。
Since C++ allows operator overloading, it's relatively easily to implement this in C++. Boost provides an "optional" template which does it, or you could roll your own if you want a coding exercise. C doesn't have the concept of operator overloading though (hell, the concept didn't really exist, and the compilers of the day probably couldn't have supported it anyway) so you get what you get.
由于 C++ 允许运算符重载,因此在 C++ 中实现它相对容易。Boost 提供了一个“可选”模板来完成它,或者如果你想要一个编码练习,你可以自己动手。C 没有运算符重载的概念(见鬼,这个概念实际上并不存在,而且当时的编译器无论如何可能都不支持它)所以你得到了你所得到的。
Perl is a special case because it rolls the two together, but C doesn't. It's entirely possible in C to have variables which are defined but not initialised. Indeed there are a lot of cases where we wantthat to be the case, particularly when you start doing low-level access to memory for drivers and stuff like that.
Perl 是一个特例,因为它将两者结合在一起,而 C 则不然。在 C 中完全有可能定义但未初始化的变量。实际上,在很多情况下我们都希望如此,尤其是当您开始对驱动程序和类似内容进行低级内存访问时。
回答by alijandro
C is a compiled language which doesn't support runtime variable binding, while Perl is a interpreted language which support dynamic typing. So you can check the definition of a variable in Perl, but not in C.
C 是一种编译语言,不支持运行时变量绑定,而 Perl 是一种支持动态类型的解释型语言。所以你可以在 Perl 中检查变量的定义,但不能在 C 中检查。
When you declare a variable in C int c;, this variable cis defined but without initialization. The declaration and definition are in one statement.
当你在 C 中声明一个变量时int c;,这个变量c被定义但没有初始化。声明和定义在一个语句中。
The definition of a variable in C is not checked by code writer. The compilers do it for you. When compile and link your C code, the compiler will check all variable's definitions. An error will be invoked and the compiling or linking process will stop if there are undefined variables found in your code.
代码编写者不会检查 C 中变量的定义。编译器为你做这件事。编译和链接 C 代码时,编译器将检查所有变量的定义。如果在您的代码中发现未定义的变量,将调用错误并且编译或链接过程将停止。
Hope this will make you distinguish the differences.
希望这能让您区分差异。

