GNU c对c的扩展

1.变长数组

#include<bits/stdc++.h>
using namespace std;

struct s{int n;long d[0];};
int main()
{
    int length=5;
    struct s *p=(struct s *)malloc(sizeof(struct s)+sizeof(long)*length);
    p->n=1;
    for(int i=0;i<=5;i++){
        p->d[i]=1;
    }
    for(int i=0;i<=6;i++){
        printf("%ld\n",p->d[i]);
    }
    return 0;
}

只用了一次malloc 效率更高。

2.使case可以匹配一个数值范围

#include <bits/stdc++.h>

using namespace std;

int main()
{
    char k;
    scanf("%c",&k);
    switch(k)
    {
        case 'a'...'z':
        cout<<"是小写字母"<<endl;
        break;
    }

    return 0;
}

3.typeof获取变量类型

#include <bits/stdc++.h>

using namespace std;

int main()
{
    char k;
    scanf("%c",&k);
    typeof(k) m='e';
    printf("%c",m);
    return 0;
}

我一开始还疑惑这东西到底有**用,后来发现自己还是年少无知。。。可以看看下面这个dalao写的

https://blog.csdn.net/ZhanShen2015/article/details/51495273

4.GNU C中宏函数允许使用可变参数类型

#include <bits/stdc++.h>
#define LOGSTRINGS(fm, ...) printf(fm,##__VA_ARGS__)
using namespace std;

int main()
{
    int emm=123;
    LOGSTRINGS("hello\n");
    LOGSTRINGS("hello,%d",emm);
    return 0;
}

这个应该很多人打过acm的大佬用吧

5. 元素编号

标准c规定数组和结构体必须按照固定顺序对成员进行初始化赋值。GNU则放宽了限制,使数组在初始化期间借助下表对某些元素赋值。

#include <bits/stdc++.h>
#define LOGSTRINGS(fm, ...) printf(fm,##__VA_ARGS__)
using namespace std;
unsigned char data[105]={
    [0]=10,
    [10 ... 50]=98,
    [55]=55,
    };
int main()
{

    LOGSTRINGS("输出%d",data[55]);
    return 0;
}

6.GNU C为当前函数准备了两个名字__FUNCTION__  __PRETTY__FUNCTION__

#include <bits/stdc++.h>
using namespace std;

void imFunctionA(){
    printf("%s\n",__FUNCTION__);
}
int main()
{

    imFunctionA();
    return 0;
}

输出:

imFunctionA

Process returned 0 (0x0)   execution time : 0.028 s
Press any key to continue.
 

7.特殊属性说明

这个下一篇写(有人叫我csgo去了.....)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章