GNU C语言的 扩展(四)数组索引初始化&case标号连续范围
来源:互联网 发布:万人迷网络电话软件 编辑:程序博客网 时间:2024/04/30 01:40
标准 C 要求数组或结构变量的初始化值必须以固定的顺序出现。比如初始化一个数组: char a [5] = {'a', 'b','c'}; 则必定是 a[0] 为 a; a[1] 为 b; a[2] 为 c ,这是一个固定的初始化顺序。
但在 GNU C 中,通过指定索引,允许初始值以任意的顺序出现。下面是一个示例代码:
[font=[object htmloptionelement]]
运行与输出:
说明:
运行输出:
但在 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;
}
运行与输出:
引用beyes@linux-beyes:~/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);
}
运行输出:
引用beyes@linux-beyes:~/C/GNU_C_EXT> ./case_more.exe
code in 0~9
code in a~f
no right code
code in A~F
0 0
- GNU C语言的 扩展(四)数组索引初始化&case标号连续范围
- GNU扩展数组初始化
- GNU C语言的 扩展(二)零长度数组&可变参数宏
- GNU C语言的 扩展(一)语句表达式
- GNU C语言的 扩展(六)内建函数
- GNU C语言的 扩展(七)__attribute__测试
- GNU C的扩展
- GNU C的扩展
- GNU C的扩展
- GNU C的扩展
- 2.2.2 GNU C对标准C语言的扩展
- C语言的数组初始化
- C语言的数组初始化
- c语言 数组的初始化
- C语言的数组初始化
- C语言的数组初始化
- C语言的数组初始化
- C语言的数组初始化
- DSP入门必看(上)
- 朴素的UNIX之-翻开历史
- 2、掌握变量定义的位置与时机
- [Leetcode] Valid Palindrome
- DSP入门必看(下)
- GNU C语言的 扩展(四)数组索引初始化&case标号连续范围
- struts 2标签之s:if 、s:iterator注意的问题
- 第一课课后作业------列表
- Ubuntu中文输入法的安装
- 我的词典:即粘即用一
- Android SpeechRecognizer语音识别
- [Leetcode] Combination Sum
- hadoop2.0yarn框架简介
- LeetCode 题解(49): Jump Game II