标准 C 要求数组或结构变量的初始化值必须以固定的顺序出现。比如初始化一个数组: char a [5] = {'a', 'b','c'}; 则必定是 a[0] 为 a; a[1] 为 b; a[2] 为 c ,这是一个固定的初始化顺序。
但在 GNU C 中,通过指定索引,允许初始值以任意的顺序出现。下面是一个示例代码:
[font=[object htmloptionelement]]
引用 #include <stdio.h>
#define SIZE 10
int main()
{
unsigned long array[SIZE]= {[2... SIZE-1]= 8};
int i;
for (i= 0; i < 10;i++)
printf("%d ",array [i]);
printf("\n");
return 0;
}
运行与输出:
引用 [email protected]:~/C/GNU_C_EXT> ./sign.exe
0 0 8 8 8 8 8 8 8 8
说明:
从程序中可以看到,可以从第 2 个元素初始化到最后一个元素,初始化值都是 8 ,而第0,第1个元素被默认初始化,值为0。
GNU C 允许在一个 case 标号中指定一个连续范围的值。
测试代码:
引用 #include <stdio.h>
void test (char code)
{
switch (code){
case '0' ... '9':
printf("code in 0~9\n");
break;
case 'a' ... 'f':
printf("code in a~f\n");
break;
case 'A' ... 'F':
printf("code in A~F\n");
break;
default:
printf("no right code\n");
}
}
int main()
{
test('9');
test('f');
test('z');
test('C');
return (0);
}
运行输出:
引用 [email protected]:~/C/GNU_C_EXT> ./case_more.exe
code in 0~9
code in a~f
no right code
code in A~F