括号中的声明符

时间:2018-06-23 18:35:44

标签: c

在浏览C11标准文档时,我发现将变量声明符放在括号中是可以接受的。

  

如果在声明“ T D1”中D1具有表单标识符,则   为ident指定的类型为T。如果在声明“ T D1”中为D1   具有(D)形式,然后ident具有声明所指定的类型   ‘T D’’。 因此,括号中的声明符与   无括号的声明符,但绑定复杂的声明符   可能会因括号而改变。

所以我尝试了。我正在将Clang与MSVC用作后端。

#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

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

1 个答案:

答案 0 :(得分:2)

正如风向标所说。所有变量都未初始化。使用-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类似。

相关问题