Froobyflake
Froobyflake

Reputation: 43

scanf not working when reading float

I have the following code:

#include <stdio.h>

float a;            //Initialize variables
char b;
int c;
char d;

int main(void)
{
    printf("Enter float: ");
    scanf("%4.2f", &a);

    printf("%4.2f", a);

    //printf("Enter character:\n");

    //printf("Enter 4-digit integer: \n");

    //printf("Enter character:\n");

    return 0;
}

However I get the following errors when compiling:

1.) scanf:unknown field type character '.' in format specifier

2.) scanf: too many arguments passed for format string

Can anyone explain what the issue in my code is?

Thank you!

Upvotes: 1

Views: 4200

Answers (3)

Johney
Johney

Reputation: 21

use %lf for scanning double precision inputs

Upvotes: 1

sinsuren
sinsuren

Reputation: 1754

scanf("%f",&a) does not take format specifier.

As mentioned in comments Visual Studio treat this warning as error. So either use

scanf_s("%f",&a);

or go into settings and disable this warning as mentioned in this post Why does Visual Studio 2013 error on C4996?

Upvotes: 4

user6359267
user6359267

Reputation:

You should not format the input. So just use %f as a first argument of scanf

scanf("%f", &a);

Upvotes: 3

Related Questions