Question

Here is my code:

main()
{
    char firstName[30], lastName[30];
    printf("What is your name? ");
    scanf_s("%s %s", firstName, lastName);
    printf("Your name is %s %s\n", firstName, lastName);
}

I tried using scanf but VS said it was deprecated and recommended scanf_s. I know that you use the &/pointer before the variables unless it is a string/char[] (which this is meant to be) and I really don't know what the error is. Maybe it's the method parameters? Any help appreciated.

Was it helpful?

Solution

MS has a habit of replacing standard C functions with "safe" versions, scanf_s is one such implementation. The docs are here: http://msdn.microsoft.com/en-us/library/w40768et.aspx

I believe the error will go away if you make this change:

scanf_s("%s %s", firstName, _countof(firstName), lastName, _countof(lastName));

I also believe there's a way to get around their corruptions of the language, but I stopped using MSVC years ago for precisely this reason.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top