Jay Somedon
Jay Somedon

Reputation: 1118

scanf with precision specifier parameter

I just started learning C, and because I mixed scanf and printf, I accidentally wrote code like this:

    float loan = 0.0f; // loan
    float rate = 0.0f; // monthly interest rate
    float mpay = 0.0f; // monthly pay

    printf("Enter amount of loan: ");
    scanf("%.2f",&loan); // I am supposed to write scanf("%f",&loan),  I was trying to take a float input with two digits after point

    printf("Enter interest rate: ");
    scanf("%.2f",&rate); // similar here

    printf("Enter monthly payment: ");
    scanf("%.2f",&mpay); // similar here

I expected that this code would show three input prompts, each of which would hang and wait there until I type some input and hit enter. But what happened is, only the first prompt was waiting for my input, then when I hit enter, second and third prompts shows up quickly without waiting for my input, as if the code got my input from somewhere else. And later when I examined value of three variables that were supposed to hold three inputs, none of them actually stored input values.

I went back to my C book and realized that I mixed printf's minimum field and precision parameter concept with scanf's, so I guess there is just no such syntax for scanf? But if so, then why does the compiler compile, doesn't that mean there IS such syntax for scanf as well, or am I wrong about this? And how should I explain this weird behavior of not waiting for my second and third inputs, and not storing inputs into three variables?

Upvotes: 2

Views: 1418

Answers (3)

cremno
cremno

Reputation: 4927

Yes, the scanf() family as specified by the C standards doesn't support this. It's explicitly undefined behavior, which means anything is allowed to happen.

But some compilers do implement a warning that checks the validity of format strings at compile-time. GCC (and Clang) has -Wformat (enabled by -Wall which one should be using anyway). If you're using a different compiler, you have to read its documentation or use static analysis tools.

From the ISO C11 Committee Draft (N1570), 7.21.6.2 paragraph 13:

If a conversion specification is invalid, the behavior is undefined. 287)

The footnote refers to 7.31.11 par. 1:

Lowercase letters may be added to the conversion specifiers and length modifiers in fprintf and fscanf. Other characters may be used in extensions.

So . could be supported by a scanf() implementation as an extension, although I'm not aware of one that does.

Upvotes: 4

Pan Long
Pan Long

Reputation: 1032

You cannot convert the precision in a scanf. If you compile it using gcc, you should get a unknown conversion type character ‘.’ in format warning.

One way to check your program is to print out the return value of scanf and you will find all these 3 scanf give you 0, which means none of the 3 variables has been scanned any value in.

Upvotes: 0

Pankaj Andhale
Pankaj Andhale

Reputation: 403

run this code on TC, and it executed as you said... First of all, you should not include the field format specifiers in scanf.

In your case I think,the numbers you provide in addition to %f (like .2 in your case)..because of that the scanf behaves abnormally..it may happen that the values you provide as input are stored at different address locations(something related to.2)..and so the variables rate,loan etc are not showing the values because nothing is stored in it.

Upvotes: 1

Related Questions