c - 是否可以混合使用多个声明/初始化?

标签 c initialization declaration

我只使用 C99,昨天,我听说在 ANSI C 中无法混合使用多个声明和初始化。因此,代码如下:

unsigned x = 42, y = 21;
double e = 3.14;

会是,带有 gcc 的 -pedantic 标志:

unsigned x, y;
double e;

x = 42, y = 21;
e = 3.14;

我很惊讶,因为我在 C89 草案中没有找到任何关于它的信息,而且像这样的代码工作正常...

unsigned x = 42, y = 21;
double e = 3.14;

对不起,这似乎是一个微不足道的问题,但我做了一些研究,并没有告诉我关于这个规则...... 是真的吗?

最佳答案

初始化是声明的一部分,因此您可以在 C89/C99 的声明中进行初始化:

/* Valid in C89 and C99. There are no statement, only declarations */
unsigned x = 42, y = 21;
double e = 3.14;

你不能做的是在 C89 中混合语句和声明:

/* Not valid in C89, valid in C99: mixing declarations and statements */
unsigned x, y;
x = 42, y = 21;

double e;
e = 3.14;

关于c - 是否可以混合使用多个声明/初始化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11186464/

相关文章:

c - libcurl,用法 CURLOPT_REDIR_PROTOCOLS

c stdout 到 stdin 实时

c - `__heap_base`在clang 9.0.0中好像不见了,有没有替代品?

c - 使用指定初始化程序时是否有可能获得指向 'this' 结构的指针?

c - C 中声明的、未初始化的变量会发生什么?它有值(value)吗?

使用指针检查字符数组中的数字,在 c 中给出错误,给出运行时错误

c++ - 错误 : no matching function for call to . .. 在返回语句

ios - swift ios 以编程方式设置 PageViewController 的过渡样式和方向

c - C 中的枚举类型变量声明

c++ - 错误 : Declaration terminated incorrectly