一个非常非常**基本的问题 [英] A very **very** basic question

查看:61
本文介绍了一个非常非常**基本的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在没有

练习的帮助下开始编写更多小程序时,我需要更多地了解一些小东西。

因此,下面,虽然这不是确切的代码,但问题的原理是相同的,(我希望:-))

#include< stdio.h>

int i = 0;

int main(){return 0; } / *没有错误或警告* /


但是


#include< stdio.h>

int i;

i = 0;

int main(){return 0; } / * 2警告。 * /

我认为其中一位常规贡献者之前曾提到过这个问题,但我希望能更清楚地理解这个原则。


所以,???


1)int i = 0是允许的,因为我被声明并初始化为

ext变量。 />
2)int i; i = 0是不允许的,因为?


a)即使我打算给i分配''0',这只能在
内发生函数?

b)编译器认为我再次声明''i'',其中已经声明了

,即使我的**意图**是初始化

一个外部变量。


我假设如果将i声明为静态,则适用相同的原则。


我错过了什么关键原则。


像往常一样感谢你。


As I begin to write more little programs without the help of the
exercises, little things pop up that I need to understand more fully.
Thus, below, and although this is not the exact code, the principle of
the question is the same, ( I hope :-) )
#include <stdio.h>
int i = 0;
int main () { return 0; } /* no errors or warnings*/

but

#include <stdio.h>
int i ;
i=0;
int main () { return 0; } /* 2 warnings. */
I think one of the regular contributors has previously alluded to this
issue, but I wish to understand the principle more clearly.

So, ???

1) int i = 0 is allowed because i is declared and initialized as an
ext variable.
2) int i; i = 0 is not allowed because ?

a) even though my intention is to assign ''0'' to i , this can only
occur within a function?
b) the compiler thinks I am once again declaring ''i'', which has
previously been declared, even though my **intent** is to initialize
an external variable.

I assume the same principles would apply if declared i as "static".

What key principle am I missing.

Thank you as usual.


推荐答案

9月25日晚上11点41分,mdh< m ... @ comcast.netwrote:
On Sep 25, 11:41 pm, mdh <m...@comcast.netwrote:

我开始写更多的小程序没有

练习的帮助,我需要更多地了解一些小东西。

因此,在下面,虽然这不是确切的代码,但原则是

问题是一样的,(我希望:-))


#include< stdio.h>

int i = 0;

int main(){return 0; } / *没有错误或警告* /
As I begin to write more little programs without the help of the
exercises, little things pop up that I need to understand more fully.
Thus, below, and although this is not the exact code, the principle of
the question is the same, ( I hope :-) )

#include <stdio.h>
int i = 0;
int main () { return 0; } /* no errors or warnings*/



很好,但是< stdio。这个程序不需要这个。

Fine, but <stdio.his not needed for this program.


>

但是


#include< stdio.h>

int i;

i = 0;

int main(){return 0; } / * 2警告。 * /


我认为其中一位常规贡献者之前已经提到了这个问题,但我希望能更清楚地理解这个原则。


所以,???


1)int i = 0是允许的,因为我被声明并初始化为

分机变量。
>
but

#include <stdio.h>
int i ;
i=0;
int main () { return 0; } /* 2 warnings. */

I think one of the regular contributors has previously alluded to this
issue, but I wish to understand the principle more clearly.

So, ???

1) int i = 0 is allowed because i is declared and initialized as an
ext variable.



Ext变量?不,不管那意味着什么。 int i = 0;是允许的,因为

你可以在任何

函数之外声明和初始化对象。这使得对象成为全局对象(它实际上使得标识符(

名称)全局)。

Ext variable? No, whatever that means. int i = 0; is allowed because
you are allowed to declare and initialize objects outside of any
function. That makes the object global (it makes the identifier (the
name) global actually).


2)int i; i = 0是不允许的,因为?


a)即使我打算给i分配''0',这只能在
内发生功能?
2) int i; i = 0 is not allowed because ?

a) even though my intention is to assign ''0'' to i , this can only
occur within a function?



是的。

yes.


b)编译器认为我再次声明''我',已经声明了

,即使我的**意图**是初始化

一个外部变量。
b) the compiler thinks I am once again declaring ''i'', which has
previously been declared, even though my **intent** is to initialize
an external variable.



当你喂他C时,编译器可以想到他想要的任何东西。

无效。

The compiler can think whatever he wants when you feed him C that''s
not valid.


我假设如果将i声明为静态,则适用相同的原则。


我错过了什么关键原则。
I assume the same principles would apply if declared i as "static".

What key principle am I missing.



代码只能在函数内部。它可以声明和

初始化全局变量。

Code can only be inside functions. It''s possible to declare and
initialize global variables.


mdh写道:

....
mdh wrote:
....

#include< stdio.h>

int i = 0;

int main(){返回0; } / *没有错误或警告* /


但是


#include< stdio.h>

int i;

i = 0;

int main(){return 0; } / * 2警告。 * /


我认为其中一位常规贡献者之前曾提到过这个问题,但我希望能更清楚地理解这个原则。


所以,???


1)int i = 0是允许的,因为我被声明并初始化为

分机变量。
#include <stdio.h>
int i = 0;
int main () { return 0; } /* no errors or warnings*/

but

#include <stdio.h>
int i ;
i=0;
int main () { return 0; } /* 2 warnings. */
I think one of the regular contributors has previously alluded to this
issue, but I wish to understand the principle more clearly.

So, ???

1) int i = 0 is allowed because i is declared and initialized as an
ext variable.



这是允许的,因为它有资格作为

标识符''i'的外部声明。外部声明是普通声明或

函数定义。在最高级别,C翻译单元

由一系列外部声明组成。

It is allowed because it qualifies as an external declaration of the
identifier ''i''. An external declaration is an ordinary declaration or
a function definition. At the highest level, a C translation unit
consists of a series of external declarations.


2)int i; i = 0是不允许的,因为?
2) int i; i = 0 is not allowed because ?



因为这是外部声明和

表达式声明的组合。语句可能只出现在复合 -

语句中。复合语句以''{''开头,以''}''

结尾,可能只出现在函数定义的主体内或作为函数定义的主体。

Because that is the combination of an external declaration and an
expression-statement. Statements may only appear in compound-
statements. A compound statement starts with a ''{'' and ends with a ''}''
and may only occur within or as the body of a function definition.


mdh说:
mdh said:

当我开始编写更多小程序而没有

练习的帮助时,很少弹出的东西,我需要更全面地了解。

因此,在下面,虽然这不是确切的代码,但问题的原理是相同的,(问题是一样的)(我希望:-))


#include< stdio.h>

int i = 0;

int main( ){return 0; } / *没有错误或警告* /


但是


#include< stdio.h>

int i;
As I begin to write more little programs without the help of the
exercises, little things pop up that I need to understand more fully.
Thus, below, and although this is not the exact code, the principle of
the question is the same, ( I hope :-) )
#include <stdio.h>
int i = 0;
int main () { return 0; } /* no errors or warnings*/

but

#include <stdio.h>
int i ;



这是一个声明,没关系。它也是一个暂定的定义,

也没关系。

That''s a declaration, and it''s fine. It''s also a tentative definition,
which is also fine.


i = 0;
i=0;



这是一个赋值语句,计为代码。你不能在函数外面有代码




-

Richard Heathfield< http:// www .cpax.org.uk>

电子邮件:-http:// www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日

That''s an assignment statement, which counts as code. You can''t have code
outside a function.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


这篇关于一个非常非常**基本的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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