声明变量的格式? [英] format for declaring variables?

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

问题描述



Hello Group,


请考虑以下代码:

/ *表格1 * /

int main(无效)

{

int i;

char * p;


/ *做点什么* /


返回(0);

}


/ *表格2 * /

int main(无效)

int i;

char * p;

{


/ *做点什么* /


返回(0);

}


似乎很多unix代码使用第二种形式来初始化

变量。我的代码很多都使用第一种形式。这两者之间是否存在

运营差异?


-

Daniel Rudy


电子邮件地址已经过base64编码以减少垃圾邮件

解码电子邮件地址使用b64decode或uudecode -m


为什么极客喜欢电脑:看看聊天日期触摸grep make unzip

strip view finger mount fcsk more fcsk yes spray umount sleep

解决方案

[code reflowed垂直压缩!]


Daniel Rudy说:


请考虑以下代码:


/ *表格1 * /

int main(void){int i; char * p; / *做某事* / return(0); }


/ *表格2 * /

int main(void)int i; char * p; {/ *做点什么* / return(0); }


似乎很多unix代码使用第二种形式来初始化

变量。我的代码很多都使用第一种形式。这两者之间是否存在

运营差异?



两种形式不相同,实际上第二种形式是非法的。

我认为你想要代表什么, *是*合法的,是:


int main(i,p)int i; char ** p; {return(0); $

它基本上是考古学的好奇心。它相当于:


int main(int i,char ** p){return(0);除了这个更新的(函数原型)版本

有助于编译器的类型检查 - 这是
为什么会引入

。由于歇斯底里的原因,较旧的表格仍然合法。


如果可以,请忽略旧版本。更好的是,更新使用

的代码来采用原型样式。


顺便提一下,0周围的括号是多余的,没有意义。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上述域名中, - www。


Daniel Rudy< sp ****** @ spamthis.netwrote:


/ *表格1 * /

int main(无效)

{

int i;

char * p;


return(0);

}


/ *表格2 * /

int main(无效)

int i;

char * p;

{


return(0);

}


似乎很多unix代码使用第二种形式来初始化

变量。



不,你不,如果你这样做,那就不是C.


我的代码很多都使用第一种形式。这两者之间是否存在

运营差异?



是的。前者是C,后者不是。


你______你看到的是:


int main()

int i;

char * p;

{


return(0);

}


注意函数头中缺少void关键字。这使得

成为旧式声明,而不是原型。它们已经过时了,而且b $ b和IMO应该在1999 C标准中被淘汰。非常古老的代码

可能仍然在18英尺左右写成这样的东西;代码

仍然必须在antediluvial上编译,标准前的编译器可能还需要像这样编写
;但是不应该写正常的代码

今天就像它一样。


Richard


Daniel Rudy< ; sp ****** @ spamthis.netwrote:


/ *表格1 * /

int main(void)

{

int i;

char * p;


/ *做点什么* /


return(0) ;

}


/ *表格2 * /

int main(void)

int i;

char * p;

{


/ *做点什么* /


return(0);

}

< blockquote class =post_quotes>
似乎很多unix代码使用第二种形式来初始化

变量。我的代码很多都使用第一种形式。这两者之间是否存在

运营差异?



这不是关于初始化变量而是函数参数。并且

第二种形式是正确的形式,第一种形式是在ANSI / ISO标准化C之前的时间

。即从1989/90之前开始。

它仍然只支持向后兼容性。并且,实际上,

你写它的方式是不正确的,如果我没记错的话,那将是
必须是


int main(i,p)

int i;

char * p;


使main()成为函数这需要一个整数参数''i'和一个char

指针参数''p''现在将写入


int main(int i ,char * p)


(当然,两者都不符合标准,因为main()需要

要么没有参数,要么是int和char **或一系列char *。)


问候,Jens

-

\ Jens Thoms Toerring ___ < a href =mailto:jt@toerring.de> jt@toerring.de

\ __________________________ http://toerring.de



Hello Group,

Consider the following code:
/* form 1 */
int main(void)
{
int i;
char *p;

/* do something */

return(0);
}

/* form 2 */
int main(void)
int i;
char *p;
{

/* do something */

return(0);
}

It seems alot of unix code uses the second form for initializing
variables. Alot of my code uses the first form. Is there any
operational difference between the two?


--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m

Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep

解决方案

[code reflowed for vertical compression!]

Daniel Rudy said:

Consider the following code:

/* form 1 */
int main(void) { int i; char *p; /* do something */ return(0); }

/* form 2 */
int main(void) int i; char *p; { /* do something */ return(0); }

It seems alot of unix code uses the second form for initializing
variables. Alot of my code uses the first form. Is there any
operational difference between the two?

The two forms are not equivalent, and indeed the second form is illegal.
What I think you''re trying to represent, which *is* legal, is:

int main(i, p) int i; char **p; { return(0); }

and it is basically an archaeological curiosity. It is equivalent to:

int main(int i, char **p) { return(0); }

except that this more up-to-date ("function prototype") version
facilitates the compiler''s type-checking - which is why it was
introduced. The older form is still legal for hysterical reasons.

Ignore the older version if you can. Better still, update code that uses
it to adopt the prototype style.

Incidentally, the parentheses around the 0 are redundant and pointless.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.


Daniel Rudy <sp******@spamthis.netwrote:

/* form 1 */
int main(void)
{
int i;
char *p;

return(0);
}

/* form 2 */
int main(void)
int i;
char *p;
{

return(0);
}

It seems alot of unix code uses the second form for initializing
variables.

No, you don''t, and if you did it isn''t C.

Alot of my code uses the first form. Is there any
operational difference between the two?

Yes. The former is C, the latter is not.

What you _may_ have seen is this:

int main()
int i;
char *p;
{

return(0);
}

Note the lack of a void keyword in the function header. This makes this
an old-style declaration, rather than a prototype. They''re obsolescent,
and IMO should have been obsoleted in the 1999 C Standard. Very old code
may still be around which was written like this in 18-something; code
which still has to compile on antediluvial, pre-Standard compilers may
still need to be written like this; but no normal code should be written
like it today.

Richard


Daniel Rudy <sp******@spamthis.netwrote:

/* form 1 */
int main(void)
{
int i;
char *p;

/* do something */

return(0);
}

/* form 2 */
int main(void)
int i;
char *p;
{

/* do something */

return(0);
}

It seems alot of unix code uses the second form for initializing
variables. Alot of my code uses the first form. Is there any
operational difference between the two?

It''s not about initializing variables but function parameters. And
the second form is the correct form, the first form is from times
before standardization of C by ANSI/ISO. i.e. from before 1989/90.
It''s only still supported for backward compatibility. And, actually,
the way you write it isn''t correct, if I remember correctly it would
have to be

int main(i,p)
int i;
char *p;

making main() a function that takes an integer argument ''i'' and a char
pointer argument ''p'' which would be written nowadays

int main(int i, char *p)

(Both are, of course, not standard-compliant since main() takes
either no arguments or an int and a char** or an array of char*.)

Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de


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

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