变量声明和初始化 [英] Variable declaration and initialisation

查看:69
本文介绍了变量声明和初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想获得一些关于哪些更好的做法的建议:


1)应该在函数的开头声明变量,还是只需要

在他们需要之前?


2)申报后是否应该立即对所有变量进行初始化?


谢谢

I just wanted to get some advice on what are the better practices:

1) Should variables be declared at the beginning of a function or just
before they are required?

2) Should all variabled be initiailised immediately after declaration?

Thanks

推荐答案

1。)好吧,在c89你没有选择权。您必须在范围块的开头声明所有

变量。然而,在c99中,它的价格与b $ b不同。这取决于你选择最好的方法,但是,

一致。


2.)出于安全考虑,你应该,特别是指针。如果你声明

一个poiner并且不要将它初始化为一些非null值;初始化

为null以避免代码中出现不必要的错误。


这是我对它的看法。

relient 。

1.) Well, in c89 you don''t have a choice. You must declare all your
variables the the begining of the scope block. In c99, however, it''s
different. It''s up to you to choose the best method, but, be
consistent.

2.) For safety reasons, you should, especially pointers. If you declare
a poiner and don''t intialize it to some non-null value; inititiaze it
to null to avoid unwanted bugs in your code.

That''s my take on it.
relient.


" James Brodie"写道:
"James Brodie" writes:
我只想得到一些关于什么是更好的做法的建议:

1)应该在函数的开头声明变量还是只是
在他们被要求之前?


他们应该在需要之前申报。 C的常见形式不允许这样,所以它有点学术性。

2)所有变量都应该在声明后立即初始化吗?
I just wanted to get some advice on what are the better practices:

1) Should variables be declared at the beginning of a function or just
before they are required?
They should be declared just before needed. The common form of C does not
allow this, so it''s a bit academic.
2) Should all variabled be initiailised immediately after declaration?




不会。这只是在必须

维护程序的人的脑海中再创建一个问题。他为什么把它设为0?它会产生一种不存在的知识错觉。



No. It just creates one more question in the mind of the person who must
maintain the program. Why did he set this to 0? It creates an illusion of
knowledge that doesn''t exist.


" relient" < XL *************** @ gmail.com>写道:
"relient" <xl***************@gmail.com> writes:
1.)好吧,在c89你没有选择。您必须在范围块的开头声明所有的变量。然而,在c99中,它是不同的。选择最好的方法取决于你,但是要保持一致。


即使在C89 / C90中,您也可以在任何

块的开头声明变量,而不仅仅是在函数的开头。例如:


void func(void){

/ *代码不使用x * /

{

int x;

/ *代码使用x * /

}

/ *更多代码没有不要使用x * /

}

2.)出于安全考虑,你应该,特别是指针。如果你声明一个poiner,并且不要将它初始化为某个非空值;将它初始化为null以避免代码中出现不必要的错误。
1.) Well, in c89 you don''t have a choice. You must declare all your
variables the the begining of the scope block. In c99, however, it''s
different. It''s up to you to choose the best method, but, be
consistent.
Even in C89/C90, you can declare variables at the beginning of any
block, not just at the beginning of a function. For example:

void func(void) {
/* code that doesn''t use x */
{
int x;
/* code that uses x */
}
/* more code that doesn''t use x */
}
2.) For safety reasons, you should, especially pointers. If you declare
a poiner and don''t intialize it to some non-null value; inititiaze it
to null to avoid unwanted bugs in your code.




另一方面,我看到很多代码初始化变量,

然后在使用它之前为其赋值。 *始终*

的政策初始化每个变量,无论是否使用初始值,

都可以避免错误,但并不总是必要的。


此外,在发布后续信息时提供一些背景信息非常重要。

Google使这一点变得不必要,但并非不可能。见

< http://cfaj.freeshell.org/google/>详情。


-

Keith Thompson(The_Other_Keith) ks ** *@mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。



On the other hand, I see a lot of code that initializes a variable,
and then assigns a value to it before using it. A policy of *always*
initializing every variable, whether the initial value is used or not,
can avoid errors, but it''s not always necessary.

Also, it''s important to provide some context when posting a followup.
Google makes this unnecessarily difficult, but not impossible. See
<http://cfaj.freeshell.org/google/> for details.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


这篇关于变量声明和初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆