How can I print the result of sizeof() at compile time in C?

后端 未结 12 1350
星月不相逢
星月不相逢 2020-11-29 20:47

How can I print the result of sizeof() at compile time in C?

For now I am using a static assert (home brewed based on other web resources) to compare the sizeof() re

12条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-29 21:44

    I stumbled upon a solution similar to Bakhazard's great solution, and this one produces a much less verbose warning, so you may find it useful:

    char (*__fail)(void)[sizeof(uint64_t)] = 1;
    

    This produces the error message

    Function cannot return array type 'char [8]'
    

    This was tested with the latest version of clang(1).

提交回复
热议问题