Alex Fateev
Alex Fateev

Reputation: 53

What is the difference between %f and %F in C?


Started to learn C and stacked with one question.
At the chapter about the console output describes the conversion specifications and two of them are %f and %F. I tried to figure out what's the difference between them and wrote a simple C script given below:

#include <stdio.h>
int main(void)
{
    float num = 123.456;
    printf("First float: %f \n", num);
    printf("Second float: %F \n", num);
    return 0;
}

The output is:

First float: 123.456001
Second float: 123.456001

Upvotes: 4

Views: 151

Answers (1)

Vlad from Moscow
Vlad from Moscow

Reputation: 310950

From the C Standard (7.21.6.1 The fprintf function)

A double argument representing an infinity is converted in one of the styles [-]inf or [-]infinity — which style is implementation-defined. A double argument representing a NaN is converted in one of the styles [-]nan or [-]nan(n-char-sequence) — which style, and the meaning of any n-char-sequence, is implementation-defined. The F conversion specifier produces INF, INFINITY, or NAN instead of inf, infinity, or nan, respectively.

Upvotes: 4

Related Questions