1,支持不定长数组,即使用变量定义数组大小,之前定义数组大小必须是常量类型。比如int a[5], 但 int a[n]就不行。
2,新的数组初始化方式,即可以指定元素赋予初值,试想如果有一个int数组a,大小为100, 即int a[100], 需要把a[50]赋值为5, a[70]赋值为7,其他为0, 则可以这样 int a[100] = {[50] = 5, [70] = 7}; 这对于初始化稀疏数组非常方便。
3,新的结构体初始化方式,具体看下面的例子。
4. 增加内联函数的支持,不过一般要写到头文件中去,或者声明为static类型。
5. 增加__func__宏,用于打印当前函数名,这对于调试非常有用。
6.结构体可以声明动态数组成员,即不指定数组长度,由使用时视分配内存大小决定数组长度。注意动态数组成员必须在声明最后,且前面必须有其他成员,至多只有一个动态数组,并且使用sizeof或许结构体大小时,将不包括动态数组大小。使用时必须由malloc分配内存,并且应该比原结构体分配内存要大。具体看下面的例子。
例子:
#include <stdio.h>
#include <stdlib.h>
static inline void foo() /* inline函数必须声明为static或者放到头文件中 */
{
/* 新增__func__宏,打印当前函数名 */
printf("function '%s' finished.\n", __func__);
}
typedef struct
{
int a;
char c;
int d[]; /* 增加动态数组支持,必须放到最后,且前面必须有一个成员 */
}data, *Data;
int main(int argc, char **argv)
{
int a[] = {[10] = 2, [5] = 5}; /* 数组初始化 */
int len = sizeof(a) / sizeof(int);
printf("a: ");
for (int i = 0; i < len; i++) /* 变量声明不必在语句开头 */
printf("%d ", a[i]);
printf("\n");
int n = 4;
int b[n]; /* 数组长度可以是变量 */
printf("sizeof(b) = %u\n", sizeof(b));
data data1 = {1,'c'}; /* 初始化结构体方法一 */
printf("data1.c = %c\n", data1.c);
data data2 = {.c='c'}; /* 初始化结构体方法二 */
printf("data2.a = %d, data2.c = %c\n", data2.a, data2.c);
Data data3 = malloc(sizeof(data) + sizeof(int) * 10);
printf("sizeof(data3) = %u\n", sizeof(*data3));
/* 内存不包括动态数组d的大小,返回8,
* int a占4B , char c占1B, 为了对齐,所以返回8 */
data3 -> d[0] = 1, data3 -> d[1] = 1;
printf("data3->d[1] = %d\n", data3->d[1]);
free(data3);
foo(1);
return 0;
}
另外还有其他特性,比如新增数据类型long long , long double, _Bool,复数等。摘自维基百科,如下
2025年5月11日 02:41
Cool you inscribe, the info is really salubrious further fascinating, I'll give you a connect to my scene. Kissimmee Bed Bug Control
2025年5月11日 02:41
This is very interesting, but it is necessary to click on this link: Oviedo Exterminator
2025年5月11日 02:41
Acknowledges for penmanship such a worthy column, I stumbled beside your blog besides predict a handful advise. I want your tone of manuscript... Orlando Termite Control
2025年5月11日 02:42
I simply want to tell you that I am new to weblog and definitely liked this blog site. Very likely I’m going to bookmark your blog . You absolutely have wonderful stories. Cheers for sharing with us your blog. Deltona Exterminator
2025年7月19日 07:26
I encourage you to read this text it is fun described ... traffic and truck driving citation