点击上方,选择“置顶/星标公众号”
在嵌入式开发中,C语言的一些小技巧可以帮我们提高开发效率,事半功倍。
C语言常常让人觉得它所能表达的东西非常有限。它不具有类似第一级函数和模式匹配这样的高级功能。
但是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};
_(InWorklist) \
_(EmittedAtUses) \
_(LoopInvariant) \
_(Commutative) \
_(Movable) \
_(Lowered) \
_(Guard)
enum Flag {
None = 0,
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
};
#define FLAG_ACCESSOR(flag) \
bool is##flag() const {\
return hasFlags(1 << flag);\
}\
void set##flag() {\
JS_ASSERT(!hasFlags(1 << flag));\
setFlags(1 << flag);\
}\
void setNot##flag() {\
JS_ASSERT(hasFlags(1 << flag));\
removeFlags(1 << flag);\
}
FLAG_LIST(FLAG_ACCESSOR)
#undef FLAG_ACCESSOR
/* Force a compilation error if condition is false, but also produce a result
* (of value 0 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. */
/* Force a compilation error if condition is false */
STATIC_ASSERT(Total <= 32)
(void)sizeof(struct { int:-!(Total <= 32) })
(void)sizeof(struct { int: 0 })
(void)sizeof(struct { int: -1 } )
🫵兄弟们!一个人单打独斗确实能冲得挺快,但要想走得更远、更稳,还得靠一群志同道合的伙伴啊!
👊 麦鸽的知识星球现在已经聚集了一波人,大家都在这里互相学习、共同进步。
如果你也想找个靠谱的学习圈子,赶紧 戳链接 🔗 加入我们吧!
在这里,你能读到星球专栏的干货,优质教程,练手项目,随时向麦鸽提问,还能帮你定制学习计划。别犹豫了,兄弟,一起冲!💪
往期推荐