首页 > > > 几点实用的C语言技巧
来源 | 百问科技
int fibs[] = {1, 1, 2, 3, 5};/* Entries may not correspond to actual numbers. Some entries omitted. */
/* ... */
/* ... */
/* ... */char *err_strings[] = {[] = "Success",[] = "Invalid argument",[] = "Not enough memory",[] = "Bad address",/* ... */[] = "Argument list too long",[] = "Device or resource busy",/* ... */[] = "No child processes"/* ... */};struct point {int x;int y;int z;}struct point p = {.x = 3, .y = 4, .z = 5};#define FLAG_LIST(_) \ \_(EmittedAtUses) \_(LoopInvariant) \_(Commutative) \_(Movable) \_(Lowered) \_(Guard)enum Flag {None = ,FLAG_LIST(DEFINE_FLAG)Total};enum Flag {None = 0,DEFINE_FLAG(InWorklist)DEFINE_FLAG(EmittedAtUses)DEFINE_FLAG(LoopInvariant)DEFINE_FLAG(Commutative)DEFINE_FLAG(Movable)DEFINE_FLAG(Lowered)DEFINE_FLAG(Guard)Total};enum Flag {None = 0,InWorklist,EmittedAtUses,LoopInvariant,Commutative,Movable,Lowered,Guard,Total};bool isreturn hasFlags(1 << flag);\}\void setJS_ASSERT(!hasFlags(1 << flag));\setFlags(1 << flag);\}\void setNotJS_ASSERT(hasFlags(1 << flag));\removeFlags(1 << flag);\}FLAG_LIST(FLAG_ACCESSOR)/* Force a compilation error if condition is false, but also produce a result* (of value and type size_t), so it can be used e.g. in a structure* initializer (or wherever else comma expressions aren't permitted). *//* Linux calls these BUILD_BUG_ON_ZERO/_NULL, which is rather misleading. */#define STATIC_ZERO_ASSERT(condition) (sizeof(struct { int:-!(condition); }) )#define STATIC_NULL_ASSERT(condition) ((void *)STATIC_ZERO_ASSERT(condition) )/* Force a compilation error if condition is false */#define STATIC_ASSERT(condition) ((void)STATIC_ZERO_ASSERT(condition))STATIC_ASSERT(Total <= 32)(void)sizeof(struct { int:-!(Total <= 32) })(void)sizeof(struct { int: })(void)sizeof(struct { int: -1 } )来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/615/viewspace-3168128/ ,如需转载,请注明出处,否则将追究法律责任。