1

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!

Mohan
  • 1,871
  • 21
  • 34
Froobyflake
  • 43
  • 1
  • 8

3 Answers3

4

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?

Community
  • 1
  • 1
sinsuren
  • 1,745
  • 2
  • 23
  • 26
3

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

scanf("%f", &a);
1

use %lf for scanning double precision inputs

Johney
  • 11
  • 1