简体   繁体   中英

How to force printf to output [-]inf or [-]infinity on Windows?

C99 standard says:

A double argument representing an infinity is converted in one of the styles [-]inf or [-]infinity -- which style is implemented is implementation-defined. (p.278 section 7.19.6.1)

Unfortunately on Windows:

printf("%f\n", 1.0f/0.0f)

produces: 1.#INF00

This is a problem because some applications expect C standard compliant strings as input (also C#'s Double.Parse works for "Infinity" but not for "1.#INF00", curiously "infinity" is not ok either at least when I tried it with Mono).

My question is how do I force printf under Windows to output "inf" or "infinity" instead of 1.#INF00 ?

(I am compiling with MinGW gcc 4.8.2)

You can choose between the MSVC (default) and mingw version of the printf-functions. Just define __USE_MINGW_ANSI_STDIO like this, and the output should be C99 compliant:

#define __USE_MINGW_ANSI_STDIO 1

Some documentation here and here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM