1

Hi I have a question about subsequent code in C:

 #include <stdio.h>
 #include <stdlib.h>

int main()
{
    char name[10];
    printf("Enter your name: \n");
    scanf_s("%s", &name);

    printf("Your name is: %s", name);


    return 0;
}

It works perfectly using CodeBlocks, but using Microsoft Visual Studio 2015 it just doesn't work. This warning is always shown:

Warning C4473   'scanf_s' : not enough arguments passed for format string

Could you please give me a hint on what's going on?

NOTE: In Visual Studio I always use scanf_s instead of scanf.

6
  • 2
    scanf_s() is not a standard function and requires extra parameters. Read the documentation. Commented Sep 9, 2017 at 13:59
  • Also you should pass name not &name to scan into. Commented Sep 9, 2017 at 14:03
  • 2
    "In Visual Studio I always use scanf_s instead of scanf." Why? Commented Sep 9, 2017 at 14:03
  • I'm still confused as to why it would work on command line instead of VS. If I understand correctly, you change the function when you go from one to the other? In that case, I have the same questions as @alk Commented Sep 9, 2017 at 14:06
  • @alk because vs2015 will refuse to compile the code. @PinkP use #define _CRT_SECURE_NO_WARNINGS before #include <stdio.h> and other 'include' files to disable deprecation and use scanf() normally. Commented Sep 9, 2017 at 14:11

1 Answer 1

1

"scanf_s" needs an extra parameter that is buffer size.

for example:

scanf_s("%s", name, 1024);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.