变量声明 - 这在标准C中有效吗? [英] Variable declaration - Is this valid in standard C?

查看:79
本文介绍了变量声明 - 这在标准C中有效吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经做了一段时间的纯C编程(我用C ++编写)。


以下函数是否符合标准C?

int main(int argc,char * argv [])

{

int x;

x = 9;

printf(" x的值是:%d \ n",x);


int i; / *< ====这个声明有效吗?* /


for(i = 0; i< 10; i ++)

printf( i的值是:%d \ n,i);


}


它在gcc中编译(版本3.4。 0)和cc(版本6)。对于gcc我尝试使用" -x c"编译

和-ansi选项(gcc -x c -ansi< sourcefile.c>)

结果相同。从我做C程序时我记得的事情

主动是这会给你一个编译错误。


请告诉我这个标准是什么?

我还有一个问题


如果我声明变量i在for中像这样循环

.......................

for(int i = 0; i < 10; i ++)

...............................


那么

1.当我用gcc编译时(命令行$ gcc< sourcefile.c>)给我

一个错误10 :错误:''for''循环初始声明在C99之外使用

模式" ;.


2.当我用cc编译时(命令行$ cc< ; sourcefile.c>)它的

编译并生成二进制可执行文件。


我在这里有点困惑!?!?

任何评论都赞赏。


感谢您的时间。

-Kannan

Its been a while I have done pure C programming (I was coding in C++).

Is the following function valid according to standard C?

int main(int argc, char *argv[])
{
int x;
x = 9;
printf("Value of x is: %d\n", x);

int i; /* <==== Is this declaration valid?*/

for(i=0; i < 10; i++)
printf("Value of i is: %d\n", i);

}

Its compiling in gcc (ver 3.4.0) and cc (ver 6). For gcc I tried the to
compile with "-x c" and "-ansi" option (gcc -x c -ansi <sourcefile.c>)
the result is same. What I remember from when I was doing C programs
actively was this will give you a compile error.

Please tell me what is the standard about this?
I have one more question

If I declare the variable "i" in "for" loop like this
.......................
for(int i=0; i < 10; i++)
...............................

Then
1. When I compile with gcc (Command line $gcc <sourcefile.c>) gives me
an error "10: error: ''for'' loop initial declaration used outside C99
mode".

2. When I compile with cc (command line $cc <sourcefile.c>) its
compiling and producing the binary executable.

I am bit confused here !?!?
Any comments appreciated.

Thank you for your time.
-Kannan

推荐答案

gcc< sourcefile.c>)给了我

一个错误10:错误:''用于在C99之外使用的循环初始声明

模式。


2.当我使用cc(命令行
gcc <sourcefile.c>) gives me
an error "10: error: ''for'' loop initial declaration used outside C99
mode".

2. When I compile with cc (command line


cc< sourcefile.c>)进行编译时,其

c ompiling和生成二进制可执行文件。


我在这里有点困惑!?!?

任何评论都赞赏。


谢谢您的时间。

-Kannan

cc <sourcefile.c>) its
compiling and producing the binary executable.

I am bit confused here !?!?
Any comments appreciated.

Thank you for your time.
-Kannan


文章< 11 ******** **************@i3g2000cwc.googlegroups。 com>,

Kannan< ri ******* @ gmail.comwrote:
In article <11**********************@i3g2000cwc.googlegroups. com>,
Kannan <ri*******@gmail.comwrote:

>以下函数是否有效按照标准C?
>Is the following function valid according to standard C?


> int main(int argc,char * argv [])
$

x = 9;

printf(" x的值是:%d \ n",x);
>int main(int argc, char *argv[])
{
int x;
x = 9;
printf("Value of x is: %d\n", x);


int i; / *< ====这个声明有效吗?* /
int i; /* <==== Is this declaration valid?*/



不在C89中,但在C99中是。

Not in C89, but Yes in C99.


for(i = 0; i< 10; i ++)

printf(" i的值是:%d \ n,i);
}
for(i=0; i < 10; i++)
printf("Value of i is: %d\n", i);
}


>它在gcc(版本3.4.0)和cc(版本6)中编译。对于gcc,我尝试使用-x c进行编译。和-ansi选项(gcc -x c -ansi< sourcefile.c>)
结果相同。我记得从积极做C程序的时候,这会给你一个编译错误。
>Its compiling in gcc (ver 3.4.0) and cc (ver 6). For gcc I tried the to
compile with "-x c" and "-ansi" option (gcc -x c -ansi <sourcefile.c>)
the result is same. What I remember from when I was doing C programs
actively was this will give you a compile error.



特定的gcc选项是偏离主题的,但是试试-std = c89

specific gcc options are off-topic, but try -std=c89


>我还有一个问题
>I have one more question


>如果我声明变量i在for中像这样的循环
......................
for(int i = 0; i< 10; i ++) ..............................
>If I declare the variable "i" in "for" loop like this
......................
for(int i=0; i < 10; i++)
..............................


>然后
1。当我用gcc编译时(命令行
>Then
1. When I compile with gcc (Command line


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

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