c - 不编译确定 `sizeof float`

标签 c gcc sizeof

我想知道 GCC 中 float 的大小,而不必运行编译器。我知道一个选择是编写一个小函数并让编译器打印出一个汇编列表。

limits.h,其中包含最小值和最大值,但是否有类似的东西告诉不同隐式类型的大小?

我在 Windows 7 x64 上使用 GCC;目标平台是 ARM7 32 位模式。语言是 C。

最佳答案

您可以让 GCC 打印出所有默认宏:

gcc -dM -E - </dev/null | grep FLT

然后你会得到这样的行:

#define __FLT_MANT_DIG__ 24
#define __FLT_MAX_EXP__ 128

现在您可以按如下方式解析:

24 + lg(128) + 1 = 32

查找文档:

1) man gcc:

   -E  Stop after the preprocessing stage; do not run the compiler proper.
       The output is in the form of preprocessed source code, which is
       sent to the standard output.

...

   -dCHARS
       CHARS is a sequence of one or more of the following characters, and
       must not be preceded by a space.  Other characters are interpreted
       by the compiler proper, or reserved for future versions of GCC, and
       so are silently ignored.  If you specify characters whose behavior
       conflicts, the result is undefined.

       M   Instead of the normal output, generate a list of #define
           directives for all the macros defined during the execution of
           the preprocessor, including predefined macros.  This gives you
           a way of finding out what is predefined in your version of the
           preprocessor.  Assuming you have no file foo.h, the command

                   touch foo.h; cpp -dM foo.h

           will show all the predefined macros.

2) 实际的宏:

http://www.gnu.org/s/hello/manual/libc/Floating-Point-Parameters.html

关于c - 不编译确定 `sizeof float`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7168215/

相关文章:

c - IPC SysV 共享内存 - shmget()、shmat() 不起作用

c++ - 为什么从未使用过 C3 分配构造函数?

c++ - 为什么 std::vector::reserve 调用复制构造函数?

c++ - sizeof... 是否允许在模板参数中用于特化?

objective-c - Objective-C 中的 "Invalid application of ' sizeof ' to interface ' 小数 ' in non-fragile ABI"

c - 为什么我无法将此数组传递给函数并对其进行编辑?

c++ - 在内存空间打开文件

c++ - 'if' 语句中的赋值和比较顺序

c++ - 检测共享库中变量的重复定义

c++ - 成员初始值设定项中使用的 sizeof 的行为是什么?