This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int a[5] = { [3] = 42, [1] = 29 }; | |
int b[] = { [0 ... 9 ] = 1, [10 ... 19] = 4, [20] = 16 }; // GCC extension | |
struct point { int x, y; }; | |
struct point p1 = { .x = 0, .y = 1 }; | |
struct point p[10] = { [5].y = 1, [4].x = -1 }; | |
union foo { int i; double d; }; | |
union foo f = { .d = 4 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
union foo { int i; double d; }; | |
extern void hacky(union foo); | |
int x = 5; | |
union foo u = (union foo) x; | |
hacky((union foo) x); | |
hacky((union foo) 5); | |
double d = 42.0; | |
union foo u = (union foo) d; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stddef.h> | |
struct S { | |
int i; | |
double d; | |
}; | |
printf("The first element is at offset %lu, " | |
"the second element is at offset %lu.", | |
offsetof(S, i), offsetof(S, d)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void goodbye(char **s) { | |
printf("Goodbye %s!\n", *s); | |
} | |
int main(int argc, char *argv[]) { | |
char *name __attribute__((cleanup(goodbye))); | |
name = argc > 1 ? argv[1] : "World"; | |
printf("Hello %s!\n", name); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
typedef union { | |
float value; | |
struct { | |
unsigned int mantissa:23; | |
unsigned int exponent:8; | |
unsigned int negative:1; | |
}; | |
} ieee754_float_t; | |
ieee754_float_t f = (ieee754_float_t) 42.f; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct { | |
int a; | |
union { int b; float c; }; | |
int d; | |
} foo; | |
foo.b = 42; // OK |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern void mylib_user_defined_hook(void) __attribute__((weak)); | |
int mylib_do_something(void) { | |
... | |
if(mylib_user_defined_hook) { | |
mylib_user_defined_hook(); | |
} | |
... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void log_default(char *fmt, ...) { | |
va_list arglist; | |
va_start(arglist, fmt); | |
vprintf(fmt, arglist); | |
va_end(arglist); | |
} | |
extern void log_debug(const char *fmt, ...) __attribute__((weak, alias("log_default"))); | |
extern void log_info(const char *fmt, ...) __attribute__((weak, alias("log_default"))); | |
extern void log_warning(const char *fmt, ...) __attribute__((weak, alias("log_default"))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__attribute__((constructor)) | |
void hello() { | |
printf("Hello!\n"); | |
} | |
__attribute__((destructor)) | |
void goodbye() { | |
printf("Goodbye!\n"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define STATIC_ASSERT(COND,MSG) typedef char static_assertion_##MSG[(!!(COND))*2-1] | |
#define COMPILE_TIME_ASSERT3(X,L) STATIC_ASSERT(X,static_assertion_at_line_##L) | |
#define COMPILE_TIME_ASSERT2(X,L) COMPILE_TIME_ASSERT3(X,L) | |
#define COMPILE_TIME_ASSERT(X) COMPILE_TIME_ASSERT2(X,__LINE__) | |
COMPILE_TIME_ASSERT(sizeof(long) == 8); | |
int main() { | |
COMPILE_TIME_ASSERT(sizeof(int) == 4); | |
} |