const int x = 5; [英] const int x=5 ;

查看:79
本文介绍了const int x = 5;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



可以告诉我为什么以下代码无法编译:


int main(无效)

{

const int a = 5;

int buf [a];

返回0;

}


语句中似乎有一些错误const int i = 5;

谢谢


Can anyboby please tell me that why the following code is''nt compiling:

int main(void)
{
const int a=5;
int buf[a];
return 0;
}

There seems to be some error in the statement const int i =5;
Thanks

推荐答案

ca ******** @ yahoo.com 写道:
可以告诉我为什么下面的代码没有编译:

int main(void)
{
const int a = 5;
int buf [a];
返回0;
}



语句const int i中似乎有一些错误5;
Can anyboby please tell me that why the following code is''nt compiling:

int main(void)
{
const int a=5;
int buf[a];
return 0;
}

There seems to be some error in the statement const int i =5;




首先,在C语言中''const int i = 5''不是声明,它是一个

声明。


其次,你的代码中没有''const int i = 5''。你可能意味着

''const int a = 5''。


第三,在C语言中,常量积分对象不符合

''整数常量''并且不能用于整数常量表达式

(ICE)。


用原始C语言(C89 / 90)数组声明中的数组大小必须是
是一个ICE。在你的代码中它不是。这就是为什么代码没有编译

(假设你使用的是C89 / 90编译器)。


在C99中这样的数组大小声明不需要是ICE,

意味着在C99中这段代码是合法的,''buf''将成为

可变长度数组(VLA) 。


-

祝你好运,

Andrey Tarasevich



Firstly, in C language ''const int i =5'' is not a statement, it is a
declaration.

Secondly, there''s no ''const int i =5'' in your code. You probably meant
''const int a=5''.

Thirdly, in C language constant integral objects do not qualify as
''integer constants'' and cannot be used in integral constant expressions
(ICE).

In the original C language (C89/90) array size in array declaration must
be an ICE. In your code it is not. That''s why the code is not compiling
(assuming that you are using a C89/90 compiler).

In C99 the array size in such declaration is not required to be an ICE,
meaning that in C99 this code would be legal and ''buf'' would become a
variable-length array (VLA).

--
Best regards,
Andrey Tarasevich


ca********@yahoo.com 写道:
ca********@yahoo.com wrote:
可以告诉我为什么下面的代码没有编译:

int main(void){
const int a = 5;
int buf [a];
返回0;
}

在语句const int i = 5中似乎有一些错误;
cat main.c
#include< stdio.h>


int main(void){

const

int n = 5;

int buf [n];

for(int j = 0; j< n; ++ j)

buf [j] = j;

for(int j = 0; j< n; ++ j)

fprintf(stdout," ; buf [%d] =%d \ n",j,buf [j]);

返回0;

}

gcc -Wall -std = c99 -pedantic -o main main.c
./main
Can anyboby please tell me that why the following code is''nt compiling:

int main(void) {
const int a = 5;
int buf[a];
return 0;
}

There seems to be some error in the statement const int i = 5; cat main.c #include <stdio.h>

int main(void) {
const
int n = 5;
int buf[n];
for (int j = 0; j < n; ++j)
buf[j] = j;
for (int j = 0; j < n; ++j)
fprintf(stdout, "buf[%d] = %d\n", j, buf[j]);
return 0;
}
gcc -Wall -std=c99 -pedantic -o main main.c
./main



buf [0] = 0

buf [ 1] = 1

buf [2] = 2

buf [3] = 3

buf [4] = 4


它似乎对我来说很好。


buf[0] = 0
buf[1] = 1
buf[2] = 2
buf[3] = 3
buf[4] = 4

It seems to work just fine for me.


Andrey Tarasevich写道:
Andrey Tarasevich wrote:
ca ******** @ yahoo.com 写道:
ca********@yahoo.com wrote:
可以告诉我为什么下面的代码没有编译:

int main(void){
const int a = 5;
int buf [a] ;
返回0;
语句const int a = 5;
Can anyboby please tell me that why the following code is''nt compiling:
int main(void) {
const int a = 5;
int buf[a];
return 0;
}

There seems to be some error in the statement const int a = 5;



在C语言[标准文档],
''中似乎有一些错误const int a = 5''不是声明,它是声明。



In C language [standards documents],
''const int a = 5'' is not a statement, it is a declaration.




它*是*一个声明。声明*是*声明。

声明与语句区分

仅在ANSI / ISO C标准文档的上下文中。



It *is* a statement. A declaration *is* a statement.
Declarations are distinguished from statements
only in the context of the ANSI/ISO C standards documents.


这篇关于const int x = 5;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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