- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我收到错误“CHAR_WIDTH”未声明
当我尝试编译这个简单的程序时:
#include <stdio.h>
#include <limits.h>
int main()
{
printf("CHAR_BIT = %d\n", CHAR_BIT);
printf("CHAR_WIDTH = %d\n", CHAR_WIDTH);
return (0);
}
与
gcc ./show_char_width.c -o show_char_width
和gcc:GNU C17(Ubuntu 8.3.0-6ubuntu1)版本8.3.0(x86_64-linux-gnu)编译的GNU C版本8.3.0,GMP版本6.1.2,MPFR版本4.0.2,MPC版本1.1.0,isl版本isl-0.20-GMP,内核:5.0.0-37-generic。
如上所述here CHAR_WIDTH 应该在limits.h 中定义,它包含在我的程序中。那么为什么我会收到此错误?
使用 -v
选项,我发现将在这些目录中搜索该库:
#include "..." search starts here:
#include <...> search starts here:
/usr/lib/gcc/x86_64-linux-gnu/8/include
/usr/local/include
/usr/lib/gcc/x86_64-linux-gnu/8/include-fixed
/usr/include/x86_64-linux-gnu
/usr/include
/usr/lib/gcc/x86_64-linux-gnu/8/include-fixed包含一个limits.h,其中包含来自同一目录的syslimits.h,而后者又包含下一个limits.h,根据我的理解应该位于/usr/include 目录中。
CHAR_WIDTH 宏确实在这些文件中定义,但在某些条件下超出了我的实际知识。
到目前为止我发现的条件是:
/* The integer width macros are not defined by GCC's <limits.h> before
GCC 7, or if _GNU_SOURCE rather than
__STDC_WANT_IEC_60559_BFP_EXT__ is used to enable this feature. */
#if __GLIBC_USE (IEC_60559_BFP_EXT)
# ifndef CHAR_WIDTH
# define CHAR_WIDTH 8
# endif
和:
#ifdef __STDC_WANT_IEC_60559_BFP_EXT__
/* TS 18661-1 widths of integer types. */
# undef CHAR_WIDTH
# define CHAR_WIDTH __SCHAR_WIDTH__
这就是为什么我需要你的帮助。
注意:对于 A.5.1 中描述的所有其他宏,我都会遇到相同的错误,特别是:SCHAR_WIDTH、INT_WIDTH、LONG_WIDTH 等。
最佳答案
CHAR_WIDTH
不是标准的,任何其他 *_WIDTH
宏也不是标准的,但字符类型的宽度必须与 CHAR_BIT
相同> 无论如何*。
对于一般的 *_WIDTH
宏,它们并不是严格需要的,因为它们可以根据相应无符号类型的最大值进行编译时计算,即,您可以有一个 #define WIDTH_FROM_UNSIGNED_MAX(UnsignedMax)
扩展为整数常量表达式,也可用于预处理器条件 (#if
),尽管实现有点晦涩(请参阅 Is there any way to compute the width of an integer type at compile-time? ),例如:
#define WIDTH_FROM_UNSIGNED_MAX(UnsignedMax) POW2_MINUS1_BITS_2039(UnsignedMax)
/* Number of bits in inttype_MAX, or in any (1<<k)-1 where 0 <= k < 2040 */
#define POW2_MINUS1_BITS_2039(X) ((X)/((X)%255+1) / 255%255*8 + 7-86/((X)%255+12))
//compile-time test it, assuming uint{8,16,32,64}_t exist
#include <inttypes.h>
#if WIDTH_FROM_UNSIGNED_MAX(UINT8_MAX) != 8
#error
#endif
#if WIDTH_FROM_UNSIGNED_MAX(UINT16_MAX) != 16
#error
#endif
#if WIDTH_FROM_UNSIGNED_MAX(UINT32_MAX) != 32
#error
#endif
#if WIDTH_FROM_UNSIGNED_MAX(UINT64_MAX) != 64
#error
#endif
有些人只是执行CHAR_BIT * sizeof(integer_type)
,但这并不是严格的可移植,因为它假设integer_type
不能有填充位(通常没有,但理论上可以有),并且也不能在 #if
条件中使用它。
*不幸的是,要收集这些信息,您需要跳过所有标准:
整数类型的宽度(稍微间接地)定义为其非填充位的数量 ( 6.2.6.2p6 )。
6.2.6.2p2说 signed char
没有任何填充位。由于整数在 C 中的表示方式 ( 6.2.6.2p2 ),这意味着 unsigned char
也不能有任何填充位,而且 char
必须具有相同的限制作为 signed char
或 unsigned char
( 5.2.4.2.1p2 ),同时具有相同的 sizeof
值(即 1, 6.5.3.4p4 ),一个普通的char
也不能有任何填充位,因此 CHAR_BIT
== (char
|signed char
|无符号字符
)。
关于CHAR_WIDTH 未声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59212052/
我收到错误“CHAR_WIDTH”未声明当我尝试编译这个简单的程序时: #include #include int main() { printf("CHAR_BIT = %d\n", CHA
我是一名优秀的程序员,十分优秀!