首页 > 解决方案 > 括号中的声明符

问题描述

在浏览 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

我真的不明白这里发生了什么,当变量在括号中声明时,它肯定持有不同的默认值。谁能解释一下?

标签: c

解决方案


正如风向标已经说过的那样。所有变量都未初始化。用标志编译-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 类似。


推荐阅读