C99特定的scanf说明符不适用于GCC 3.3.2——为什么不?
“hh”说明符是在C99中引入的,我在代码中使用这个说明符。(下面的代码示例…)我的编译器是GCC 3.3.2,我的操作系统是Solaris 8,我的C库是SUNW libc v1.21。此代码运行时,“hh”说明符无效。当我切换到Solaris 10、GCC 3.4.6和SUNW libc 1.23时,代码按预期运行。我编译时使用了“-std=c99”选项和不使用“-std=c99”选项,没有任何区别。我的结论是SUNW libc 1.21不支持这个C99特性。我想我的问题是(1)为什么不呢?(2)是否有某种方法可以确定特定的C库是否支持特定的C99功能?谢谢大家! 代码很简单:C99特定的scanf说明符不适用于GCC 3.3.2——为什么不?,c,gcc,solaris,c99,libc,C,Gcc,Solaris,C99,Libc,“hh”说明符是在C99中引入的,我在代码中使用这个说明符。(下面的代码示例…)我的编译器是GCC 3.3.2,我的操作系统是Solaris 8,我的C库是SUNW libc v1.21。此代码运行时,“hh”说明符无效。当我切换到Solaris 10、GCC 3.4.6和SUNW libc 1.23时,代码按预期运行。我编译时使用了“-std=c99”选项和不使用“-std=c99”选项,没有任何区别。我的结论是SUNW libc 1.21不支持这个C99特性。我想我的问题是(1)为什么不呢?
uint8_t* scanByte;
uint32_t uScratch;
uint8_t aByte;
strcpy(scanByte,"0xF9");
sscanf(scanByte,"%x", &uScratch );
sscanf(scanByte,"%hhx", &aByte );
printf("scanByte: %s uScratch: %x aByte: %x\n", scanByte, uScratch, aByte);
Solaris 8输出:
scanByte: 0xF9 uScratch: f9 aByte: 0
scanByte: 0xF9 uScratch: f9 scanResult1: 1
scanByte: 0xF9 uScratch: f9 scanResult2: 0
Solaris 10输出:
scanByte: 0xF9 uScratch: f9 aByte: f9
scanByte: 0xF9 uScratch: f9 scanResult1: 1
scanByte: 0xF9 uScratch: f9 scanResult2: 1
更新:
我已按如下方式更改代码,并重新运行:
unsigned int uScratch;
unsigned char aByte;
int scanResult1;
int scanResult2;
unsigned char* scanByte;
scanByte=malloc(5);
strcpy(scanByte,"0xF9");
scanResult1 = sscanf(scanByte,"%x", &uScratch );
printf("scanByte: %s uScratch: %x scanResult1: %d\n", scanByte, uScratch, scanResult1);
scanResult2 = sscanf(scanByte,"%hhx", &aByte );
printf("scanByte: %s uScratch: %x scanResult2: %d\n", scanByte, uScratch, scanResult2);
Solaris 10输出:
scanByte: 0xF9 uScratch: f9 aByte: f9
scanByte: 0xF9 uScratch: f9 scanResult1: 1
scanByte: 0xF9 uScratch: f9 scanResult2: 1
Solaris 8输出:
scanByte: 0xF9 uScratch: f9 aByte: 0
scanByte: 0xF9 uScratch: f9 scanResult1: 1
scanByte: 0xF9 uScratch: f9 scanResult2: 0
所以,是的,关于Solaris 8+scanf+hhx,正如我最初怀疑的那样。但我的问题是:如何确定libc是否支持特定的C99特性?GCC 3.3.2是一个C99编译器,但显然libc版本并不完全支持C99。如何找出哪些C99功能不受支持?[Edit]在对不正确的缓冲区初始化和格式说明符等小问题进行排序后,问题仍然存在:假定的C99编译器未在
scanResult2=sscanf(scanByte,“%hhx”、&aByte)中使用%hhx”
代码>如预期
这个编译器也没有定义SCNx8
宏,这意味着它不知道如何scanf()
auint8\t
#include <inttypes.h>
sscanf(scanByte,"%" SCNx32, &uScratch );
sscanf(scanByte,"%" SCNx8, &aByte );
printf("scanByte: %s uScratch: %" PRIx32 " aByte: %" PRIx8 "\n",
scanByte, uScratch, aByte);
建议在扫描无符号字符时使用这种缺乏宏定义的方法来引导代码
#include <inttypes.h>
...
unsigned char uc;
uint8_t u8;
#ifdef SCNx8
cnt1 = sscanf(buf, "%hhx", &uc);
cnt2 = sscanf(buf, "%" SCNx8, &u8);
#else
unsigned un;
cnt1 = sscanf(buf, "%2x", &un);
uc = (unsigned char) un;
cnt2 = sscanf(buf, "%2x", &un);
u8 = (uint8_t) un;
#ednif
也不应将uint32\u t
与%x
和uint8\u t
与%hhx
混合使用。最好使用SCNxn\u t
#include <inttypes.h>
sscanf(scanByte,"%" SCNx32, &uScratch );
sscanf(scanByte,"%" SCNx8, &aByte );
printf("scanByte: %s uScratch: %" PRIx32 " aByte: %" PRIx8 "\n",
scanByte, uScratch, aByte);
[编辑]
在对不正确的缓冲区初始化和格式说明符等小问题进行排序后,问题仍然存在:假定的C99编译器未在scanResult2=sscanf(scanByte,“%hhx”、&aByte)中使用%hhx”
代码>如预期
这个编译器也没有定义SCNx8
宏,这意味着它不知道如何scanf()
auint8\t
#include <inttypes.h>
sscanf(scanByte,"%" SCNx32, &uScratch );
sscanf(scanByte,"%" SCNx8, &aByte );
printf("scanByte: %s uScratch: %" PRIx32 " aByte: %" PRIx8 "\n",
scanByte, uScratch, aByte);
建议在扫描无符号字符时使用这种缺乏宏定义的方法来引导代码
#include <inttypes.h>
...
unsigned char uc;
uint8_t u8;
#ifdef SCNx8
cnt1 = sscanf(buf, "%hhx", &uc);
cnt2 = sscanf(buf, "%" SCNx8, &u8);
#else
unsigned un;
cnt1 = sscanf(buf, "%2x", &un);
uc = (unsigned char) un;
cnt2 = sscanf(buf, "%2x", &un);
u8 = (uint8_t) un;
#ednif
也不应将uint32\u t
与%x
和uint8\u t
与%hhx
混合使用。最好使用SCNxn\u t
#include <inttypes.h>
sscanf(scanByte,"%" SCNx32, &uScratch );
sscanf(scanByte,"%" SCNx8, &aByte );
printf("scanByte: %s uScratch: %" PRIx32 " aByte: %" PRIx8 "\n",
scanByte, uScratch, aByte);
[编辑]
在对不正确的缓冲区初始化和格式说明符等小问题进行排序后,问题仍然存在:假定的C99编译器未在scanResult2=sscanf(scanByte,“%hhx”、&aByte)中使用%hhx”
代码>如预期
这个编译器也没有定义SCNx8
宏,这意味着它不知道如何scanf()
auint8\t
#include <inttypes.h>
sscanf(scanByte,"%" SCNx32, &uScratch );
sscanf(scanByte,"%" SCNx8, &aByte );
printf("scanByte: %s uScratch: %" PRIx32 " aByte: %" PRIx8 "\n",
scanByte, uScratch, aByte);
建议在扫描无符号字符时使用这种缺乏宏定义的方法来引导代码
#include <inttypes.h>
...
unsigned char uc;
uint8_t u8;
#ifdef SCNx8
cnt1 = sscanf(buf, "%hhx", &uc);
cnt2 = sscanf(buf, "%" SCNx8, &u8);
#else
unsigned un;
cnt1 = sscanf(buf, "%2x", &un);
uc = (unsigned char) un;
cnt2 = sscanf(buf, "%2x", &un);
u8 = (uint8_t) un;
#ednif
也不应将uint32\u t
与%x
和uint8\u t
与%hhx
混合使用。最好使用SCNxn\u t
#include <inttypes.h>
sscanf(scanByte,"%" SCNx32, &uScratch );
sscanf(scanByte,"%" SCNx8, &aByte );
printf("scanByte: %s uScratch: %" PRIx32 " aByte: %" PRIx8 "\n",
scanByte, uScratch, aByte);
[编辑]
在对不正确的缓冲区初始化和格式说明符等小问题进行排序后,问题仍然存在:假定的C99编译器未在scanResult2=sscanf(scanByte,“%hhx”、&aByte)中使用%hhx”
代码>如预期
这个编译器也没有定义SCNx8
宏,这意味着它不知道如何scanf()
auint8\t
#include <inttypes.h>
sscanf(scanByte,"%" SCNx32, &uScratch );
sscanf(scanByte,"%" SCNx8, &aByte );
printf("scanByte: %s uScratch: %" PRIx32 " aByte: %" PRIx8 "\n",
scanByte, uScratch, aByte);
建议在扫描无符号字符时使用这种缺乏宏定义的方法来引导代码
#include <inttypes.h>
...
unsigned char uc;
uint8_t u8;
#ifdef SCNx8
cnt1 = sscanf(buf, "%hhx", &uc);
cnt2 = sscanf(buf, "%" SCNx8, &u8);
#else
unsigned un;
cnt1 = sscanf(buf, "%2x", &un);
uc = (unsigned char) un;
cnt2 = sscanf(buf, "%2x", &un);
u8 = (uint8_t) un;
#ednif
也不应将uint32\u t
与%x
和uint8\u t
与%hhx
混合使用。最好使用SCNxn\u t
#include <inttypes.h>
sscanf(scanByte,"%" SCNx32, &uScratch );
sscanf(scanByte,"%" SCNx8, &aByte );
printf("scanByte: %s uScratch: %" PRIx32 " aByte: %" PRIx8 "\n",
scanByte, uScratch, aByte);
诸如sscanf()
和sprintf()
之类的函数是在操作系统C库(libc)中实现的,而不是在编译器中实现的,因此在将这些参数传递给libc中的函数时,编译器支持什么并不重要。直到2000年2月Solaris 10发布,Solaris才将C99支持添加到该库中。因此,Solaris 8现在非常旧,即将结束其支持寿命,并且不是尝试编写C99代码的好选择。诸如sscanf()
和sprintf()
之类的函数在操作系统C库(libc)中实现,不在编译器中,因此将这些参数传递给libc中的函数时,编译器支持什么并不重要。直到2000年2月Solaris 10发布,Solaris才将C99支持添加到该库中。因此,Solaris 8现在非常旧,即将结束其支持寿命,并且不是尝试编写C99代码的好选择。诸如sscanf()
和sprintf()
之类的函数在操作系统C库(libc)中实现,不在编译器中,因此将这些参数传递给libc中的函数时,编译器支持什么并不重要。直到2000年2月Solaris 10发布,Solaris才将C99支持添加到该库中。因此,Solaris 8现在非常旧,即将结束其支持寿命,并且不是尝试编写C99代码的好选择。诸如sscanf()
和sprintf()
之类的函数在操作系统C库(libc)中实现,不在编译器中,因此将这些参数传递给libc中的函数时,编译器支持什么并不重要。Solaris在2000年2月发布Solaris10之前没有向该库添加C99支持,因此Solaris8现在非常旧,即将结束其支持生命,对于尝试编写C99代码来说不是一个好的选择。strcpy(scanByte,“0xF9”)代码>:错误<代码>扫描字节未初始化。请尝试uint8\u t扫描字节[10]代码>@