#include <stdio.h>
#include <stdlib.h>
#include <sys/cdefs.h>

// https://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/C-Extensions.html

// like templete, generic typed func or macro func
#define max(a,b) \
({ typeof (a) _a = (a); \
typeof (b) _b = (b); \
_a > _b ? _a : _b; }) // can be added generic macro func

#define type_name(type) typeof(type)

int a() {
return 1111111;
}

// there are no references in c. too bad.

int main() {
int result = ({ // this is statement expressions
int x = 5; // can use normal code line
int y = 3;
(void)(x + y);
a(); // return a value of returned a func
// return 1; return ; // cant use return keyword
});
printf("result code block: %d\n", result);

void test1(int a) { printf("result: %d\n", a); } // gnu nested functions!
test1(({
12345; // code block also use when passing to a parameter
}));
printf("999: %p\n", test1);
test1(999);

void test2(void (*a)(int)){
printf("%p\n", a);
(*a)(33333);
}
printf("33333: %p\n", test1);
test2(test1); // nested funcs can pass to another. + error
void (*fp)() = ({ void f(){printf("im func as a compount literal\n");};&f; });
fp();
({ void f(){printf("im func as a compount literal\n");} f; })();
int (*foo(void))[2] { // vla return
return &(int[2]){1,2};
}
int arr = (*foo())[0];
struct {
int type;
int size;
char content[0];
} *st1 = malloc(sizeof(st1) + 100); // 100 is content size
free(st1);

struct yy {
char a1: 1;
char a2: 1;
char a3: 1;
char a4: 1;
char a5: 1;
char a6: 1;
char a7: 2; // 2 !
char b1: 1; // add new char 1 byte padding
char b2: 1;
}; // total 2 byte, different types increase padding

    (int[33]){
[2] = ({
int x = 5, y = 10;
x * y;
}),
['A' != 'A' ? : 23] = 3 // gnu Omitted Operands (5.7)
} [0] = 1;

int a[6] = {[0 ... 5] = 666}; // syntactic sugar fill array
// https://gcc.gnu.org/onlinedocs/gcc/Designated-Inits.html
for (size_t i = 0; i < 6; i++) printf("%d\n", a[i]);

char c = 66;
switch(c) {
case '0' ... '9': // multiple int match, gnu extension
printf("[%c] is a number.\n", c);
break;
case 'a' ... 'z':
printf("[%c] is a lowercase letter.\n", c);
break;
case 'A' ... 'Z':
printf("[%c] is an uppercase letter.\n", c);
break;
default:
printf("[%c] is not a valid character!\n", c);
break;
}

return 0;
}

// cleanup (cleanup_function)

Yorumlar