C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了c – 用g 4.9.1修复奇怪的“%a”格式行为?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
编译器: Windows 8.1下的Nuwen发行版64位MinGW G 4.9.1.

码:

#ifdef INCLUDE_IOSTREAM
#   include <iostream>
#endif
#include <stdio.h>      // ::snprintf
#include <stdlib.h>     // EXIT_succesS,EXIT_FAILURE
#include <stdexcept>    // std::exception

#ifdef snprintf
#   error snprintf defined as macro
#endif

#ifdef _MSC_VER
    auto const snprintf = _snprintf;
#endif

void test( double const value,int const precision)
{
    char buffer[34];
    snprintf( buffer,sizeof( buffer ),"%.*a",precision,value );
    printf( "Hex of %.3f with %2d digits: %s\n",value,buffer );
}

auto main() -> int
{
    using namespace std;
    try
    {
        for( int precision = 6; precision <= 8; ++precision )
        {
            test( 5.0,precision );
        }
        test( 0.0,14 );
        return EXIT_succesS;
    }
    catch( exception const& x )
    {
        fprintf( stderr,"!%s\n",x.what() );
    }
    return EXIT_FAILURE;
}

Visual C可以正常工作(但Visual C似乎缺乏相反的转换):

H:\dev\test\so\0187>cl /nologo- /? 2>&1 | find /i "ler ver"
Microsoft (R) C/C++ Optimizing Compiler Version 18.00.30723 for x86

H:\dev\test\so\0187>cl barx.cpp -D INCLUDE_IOSTREAM /Feb
barx.cpp

H:\dev\test\so\0187>b
Hex 5.000 with  6 digits: 0x1.400000p+2
Hex 5.000 with  7 digits: 0x1.4000000p+2
Hex 5.000 with  8 digits: 0x1.40000000p+2
Hex 0.000 with 14 digits: 0x0.00000000000000p+0

H:\dev\test\so\0187>_

当< iostream>不包括

H:\dev\test\so\0187>g++ --version | find "++"
g++ (GCC) 4.9.1

H:\dev\test\so\0187>g++ -std=c++11 barx.cpp

H:\dev\test\so\0187>a
Hex of 5.000 with  6 digits: 0x1.400000p+2
Hex of 5.000 with  7 digits: 0x1.4000000p+2
Hex of 5.000 with  8 digits: 0x1.40000000p+2
Hex of 0.000 with 14 digits: 0x0.00000000000000p+0

H:\dev\test\so\0187>_

当< iostream>时,异常结果挂起已经包括了:

H:\dev\test\so\0187>g++ -std=c++11 -D INCLUDE_IOSTREAM barx.cpp

H:\dev\test\so\0187>a
Hex of 5.000 with  6 digits: 0xa.000000p-1
Hex of 5.000 with  7 digits: 0xa.0000000p-1
Hex of 5.000 with  8 digits: 0x0.00000000p-33           ← Weird.
^C                                                      ← Hang,Ctrl+C
H:\dev\test\so\0187>_

我要求修复或解决方法.

@H_262_56@

解决方法

Microsoft的实现有许多printf bug,这些缺陷会影响MinGW( #377,#407等).

在所有情况下,建议appears to be在预处理器中将__USE_MINGW_ANSI_STdio定义为1,以替代使用MinGW自己的ANSI兼容实现.

大概Visual studio有自己的内部解决方案,用于底层系统代码的缺陷.

@H_262_56@ @H_262_56@

大佬总结

以上是大佬教程为你收集整理的c – 用g 4.9.1修复奇怪的“%a”格式行为?全部内容,希望文章能够帮你解决c – 用g 4.9.1修复奇怪的“%a”格式行为?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。