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,841
  • 20
  • 32
Froobyflake
  • 43
  • 1
  • 8
  • It still seems to have build errors when I try to compile. Hmmm.... – Froobyflake Oct 13 '16 at 05:22
  • I posted the two errors above. The issue certainly has something to do with the scanf function. – Froobyflake Oct 13 '16 at 05:26
  • That is exactly the code I have. What could be causing it to not work on my end? I am using Visual Studio 2015 – Froobyflake Oct 13 '16 at 05:29
  • I used scanf_s instead and it worked! Why would scanf not work and scanf_s would? – Froobyflake Oct 13 '16 at 05:32
  • http://stackoverflow.com/questions/30577519/error-c4996-scanf-this-function-or-variable-may-be-unsafe-in-c-programming this can answer all your queries. – sinsuren Oct 13 '16 at 05:33
  • Thankyou for your help sinsuren! – Froobyflake Oct 13 '16 at 05:35
  • 1
    Welcome to Stack Overflow. Please note that the preferred way of saying 'thanks' around here is by up-voting good questions and helpful answers (once you have enough reputation to do so), and by accepting the most helpful answer to any question you ask (which also gives you a small boost to your reputation). Please see the [About] page and also [How do I ask questions here?](http://stackoverflow.com/help/how-to-ask) and [What do I do when someone answers my question?](http://stackoverflow.com/help/someone-answers) – Jonathan Leffler Oct 14 '16 at 02:21

2 Answers2

3

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
  • 20
  • 25
2

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

scanf("%f", &a);