rah*_*hmu 9 linux c header-file
从联机帮助页man limits.h:
{WORD_BIT}\n Number of bits in an object of type int.\n Minimum Acceptable Value: 32\nRun Code Online (Sandbox Code Playgroud)\n但是,如果我运行一个简单的程序:
\n#include <limits.h>\n#include <stdio.h>\n\nint main() {\n printf("%d\\n", WORD_BIT);\n return 0;\n}\nRun Code Online (Sandbox Code Playgroud)\n然而,当尝试使用 gcc ( gcc file.c -o file) 进行编译时,我得到以下信息:
error: \xe2\x80\x98WORD_BIT\xe2\x80\x99 undeclared (first use in this function)\nRun Code Online (Sandbox Code Playgroud)\n为什么我的系统上没有定义这个信息,我还能在哪里找到这个信息(在 C 程序中)?
\n我的系统:
\nSte*_*itt 16
I\xe2\x80\x99m 不确定这是否已记录,但使用 GNU C 库,您需要设置以_GNU_SOURCE获得WORD_BIT:
$ gcc -include limits.h -E - <<<"WORD_BIT" | tail -n1\nWORD_BIT\n\n$ gcc -D_GNU_SOURCE -include limits.h -E - <<<"WORD_BIT" | tail -n1\n32\nRun Code Online (Sandbox Code Playgroud)\n你真的应该使用sysconf:
$ gcc -include limits.h -E - <<<"WORD_BIT" | tail -n1\nWORD_BIT\n\n$ gcc -D_GNU_SOURCE -include limits.h -E - <<<"WORD_BIT" | tail -n1\n32\nRun Code Online (Sandbox Code Playgroud)\n或者,按照 GNU C 库文档中的建议:
\n#include <unistd.h>\n#include <stdio.h>\n\nint main(int argc, char **argv) {\n printf("%ld\\n", sysconf(_SC_WORD_BIT));\n}\nRun Code Online (Sandbox Code Playgroud)\n(至于为什么它出现在 中man limits.h,这个手册页是的 POSIX 参考limits.h,\xe2\x80\x99 不一定准确地记录您系统上的 C 库。您可以通过查看手册页 \xe2 的部分来了解这一点\x80\x94 \xe2\x80\x9cP\xe2\x80\x9d 后缀表示它\xe2\x80\x99s POSIX 文档。)
你可以改用sizeof(int) * CHAR_BIT;CHAR_BIT总是被定义的。\xe2\x80\x99s 更重要的是,POSIX 扩展了 C 标准并指定为CHAR_BIT8,因此如果您假设 POSIX,则可以使用sizeof(int) * 8.
| 归档时间: |
|
| 查看次数: |
1178 次 |
| 最近记录: |