fwprintf()"%ff"格式

时间:2014-05-18 12:13:25

标签: c

我偶然发现了以下格式:

 fwprintf( file, "%ff" ,someFloat);

这是什么意思?我知道“%ll”是“很长”for example。但是%ff?它是双精度浮点数吗?这种格式对于浮点数有什么实际意义吗?

1 个答案:

答案 0 :(得分:1)

%ff格式与%f格式相同,后者代表float,但第一种格式也会写出额外的f。

[归功于Oli]

如果格式被视为%f,而第二个f被视为典型字符,则会发生什么。

示例:

  float a = 1.2345678;
  fprintf(stdout, "%f\n", a);
  fprintf(stdout, "%ff\n", a);

输出:

1.234568
1.234568f

因此,在fwprintf的情况下,您将在文件中写入冗余 f

以下是一个例子:

#include <stdio.h>
#include <wchar.h>

int main(void)
{
  float a = 1.23456;

  const char *testFileName = "test.txt";
  FILE *wideTestFile;
  wideTestFile = fopen(testFileName, "w");
  fwprintf( wideTestFile, L"Format ff: %ff\n" ,a);
  fwprintf( wideTestFile, L"Format f: %f\n" ,a);

  return 0;
}

test.txt包含:

Format ff: 1.234560f
Format f: 1.234560