c - 括号中的声明符
问题描述
在浏览 C11 标准文档时,我发现将变量声明符放在括号中是可以接受的。
如果在声明“T D1”中,D1 具有形式标识符,则为 ident 指定的类型为 T。如果在声明“T D1”中,D1 具有形式 (D),则 ident 具有类型由声明“T D”指定。因此,括号中的声明符与未加括号的声明符相同,但复杂声明符的绑定可能会被括号更改。
所以我尝试了。我正在使用带有 MSVC 的 Clang 作为后端。
#include <stdio.h>
int main() {
int (a),(b),(c);
int p, q, r;
printf("Value of a, b, c is %d, %d, %d\n", a, b, c);
printf("Value of p, q, r is %d, %d, %d\n", p, q, r);
return 0;
}
它生成这样的输出。
PS D:\C> .\a.exe
Value of a, b, c is 0, 1, 1069425288
Value of p, q, r is 0, 0, 0
我真的不明白这里发生了什么,当变量在括号中声明时,它肯定持有不同的默认值。谁能解释一下?
解决方案
正如风向标已经说过的那样。所有变量都未初始化。用标志编译-Wall
会告诉你:gcc t.c -std=c11 -Wall
t.c: In function ‘main’:
t.c:6:5: warning: ‘a’ is used uninitialized in this function [-Wuninitialized]
printf("Value of a, b, c is %d, %d, %d\n", a, b, c);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
t.c:6:5: warning: ‘b’ is used uninitialized in this function [-Wuninitialized]
t.c:6:5: warning: ‘c’ is used uninitialized in this function [-Wuninitialized]
t.c:7:5: warning: ‘p’ is used uninitialized in this function [-Wuninitialized]
printf("Value of p, q, r is %d, %d, %d\n", p, q, r);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
t.c:7:5: warning: ‘q’ is used uninitialized in this function [-Wuninitialized]
t.c:7:5: warning: ‘r’ is used uninitialized in this function [-Wuninitialized]
在我的系统上,我只是得到一些其他机器相关的变化输出。
>>> ./a.out
Value of a, b, c is 1024607536, 22001, 1134350448,
Value of p, q, r is 32766, 0, 0
我想它与 Clang 类似。
推荐阅读
- kubernetes - 如何访问 minikube 的 openapi 接口?
- rider - Rider 说所有项目文件都不是项目的一部分
- c++ - c++ Visual Studio 编译器 OpenMP
- typescript - TypeScript:如何委托接口方法
- android - 无法在 android oreo(API 级别 26)上以设备管理员身份激活应用
- airflow - Airflow DAG 每 30 秒自动刷新一次
- ansible - AnsibleUndefinedVariable:未找到具有此名称的变量:region1a"}
- azure - 在 Azure 数据工厂中获取历史数据复制的日期
- sql - Presto - 联合和限制
- c - 如何修复 C 中的错误“表达式不是函数定位器”?