本文介绍了如何在Windows上强制printf输出[-] inf或[-] infinity?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C99标准说:

不幸的是,在Windows上:

Unfortunately on Windows:

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

产生:1.#INF00

produces: 1.#INF00

这是一个问题,因为某些应用程序希望使用符合C标准的字符串作为输入(C#的Double.Parse也适用于"Infinity",但不适用于"1.#INF00",奇怪的是,至少当我使用"Infinity"时,它也不行)用Mono试过了.

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).

我的问题是如何强制Windows下的printf输出"inf"或"infinity"而不是1.#INF00?

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

(我正在使用MinGW gcc 4.8.2进行编译)

(I am compiling with MinGW gcc 4.8.2)

推荐答案

您可以在printf-functions的MSVC(默认)版本和mingw版本之间进行选择.只需这样定义__USE_MINGW_ANSI_STDIO,输出应符合C99:

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

某些文档此处此处.

这篇关于如何在Windows上强制printf输出[-] inf或[-] infinity?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 16:59