使用非常量初始化的全局变量的定义 [英] Definition of global variables using a non constant initializer

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

问题描述

 的#include<&stdio.h中GT;INT I = 10;
INT J =;
诠释的main()
{
    的printf(%D,J);
}

我得到一个错误,指出初始元素不是常量?这背后有什么原因呢?


解决方案

  

这背后的原因是什么?


C(不像C ++)不允许与非常数值全局值初始化。

C99标准:第6.7.8:


  

在为具有静态存储期限为对象的初始化所有前pressions不断前pressions或字符串。


#include <stdio.h>

int i=10;
int j=i;
int main()
{
    printf("%d",j);
}

I get an error stating that initialization element is not a constant? What is the reason behind this?

解决方案

What is the reason behind this?

C(unlike C++) does not allow initialization of global values with non constant values.

C99 Standard: Section 6.7.8:

All the expressions in an initializer for an object that has static storage duration shall be constant expressions or string literals.

这篇关于使用非常量初始化的全局变量的定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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