括号运算符在 C 中有特殊用途吗?

问题描述

以下代码工作正常,但我没有找到可以使用 () 初始化数组的基础。有人能解释一下吗?

#include <stdio.h>

int main(void){
    int a[3][2] = {(0,1),(2,3),(4,5)};
    printf("%#x\n",a[0]);
    printf("%#x\n",a[1]);
    return 0;
}

解决方法

(0,1) 表达式使用 comma operator。并且可以优化为1。

阅读更多 this C reference 和一些 C 标准,如 n1570 或更新。

您可能想要编码:

int a[3][2] = {{0,1},{2,3},{4,5}};

printf("%#x\n",a[0]);

是错误的,您正在打印一个指针。所以使用 %p 而不是 %#x

如果编译时使用 GCC 作为 gcc -Wall -Wextra -g 调用,则会收到警告:

 % gcc -Wall -Wextra -g /tmp/articfox.c -o /tmp/articfox
 /tmp/articfox.c: In function ‘main’:
 /tmp/articfox.c:4:22: warning: left-hand operand of comma expression has no effect [-Wunused-value]
     4 |     int a[3][2] = {(0,1),(2,3),(4,5)};
       |                      ^
 /tmp/articfox.c:4:30: warning: left-hand operand of comma expression has no effect [-Wunused-value]
     4 |     int a[3][2] = {(0,5)};
       |                              ^
 /tmp/articfox.c:4:38: warning: left-hand operand of comma expression has no effect [-Wunused-value]
     4 |     int a[3][2] = {(0,5)};
       |                                      ^
 /tmp/articfox.c:4:19: warning: missing braces around initializer [-Wmissing-braces]
     4 |     int a[3][2] = {(0,5)};
       |                   ^
       |                    {             } {     }
 /tmp/articfox.c:5:15: warning: format ‘%x’ expects argument of type ‘unsigned int’,but argument 2 has type ‘int *’ [-Wformat=]
     5 |     printf("%#x\n",a[0]);
       |             ~~^     ~~~~
       |               |      |
       |               |      int *
       |               unsigned int
       |             %#ls
 /tmp/articfox.c:6:15: warning: format ‘%x’ expects argument of type ‘unsigned int’,but argument 2 has type ‘int *’ [-Wformat=]
     6 |     printf("%#x\n",a[1]);
       |             ~~^     ~~~~
       |               |      |
       |               |      int *
       |               unsigned int
       |             %#ls
,

C 中没有括号运算符。:)

如果去掉括号,数组的初始化看起来像

int a[3][2] = { 0,1,2,3,4,5};

即初始化器列表正好包含 6 个显式初始化器。

括号允许您使用更复杂的表达式作为初始值设定项。因此在这个声明中

int a[3][2] = {(0,5)};

只有 3 个显式初始化器。数组中除前三个元素外的所有其他元素都将初始化为零。

括号中的表达式是带有逗号运算符的表达式。每个表达式的值是最右边操作数的值。所以上面的声明等价于

int a[3][2] = { 1,5 };

考虑以下演示程序。

#include <stdio.h>

int main(void) 
{
    int i = 10;
    int x = ( i++,++i );
    
    printf( "x = %d\n",x );
    
    return 0;
}

它的输出是

x = 12

如果去掉这个声明中的括号

    int x = i++,++i;

然后编译器会认为在您要声明的标识符列表中,您忘记了逗号后的第二个标识符,例如

    int x = i++,y = ++i;