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去了.....)

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