简体   繁体   中英

scanf_s doesn't work with printf in Visual Studio C++ 2010 Express

I wrote these code:

#include <stdio.h>
int main(void)
{
    char input[50]={0};
    scanf_s("%s",input);
    printf("%s",input);
    return 0;
}

When I run it in release mode (Ctrl+F5) it doesn't print anything, but in debug mode (F5) it does, and has no errors. In release mode console screen is something like this:

abcd
Press any key to continue . . .

The first line is my input, and this is the screen in debug mode:

abcd
abcdPress any key to continue . . .

When I use scanf instead of scanf_s, it does print, both in debug and release mode. What am I missing?

This version of scanf() requires one more argument, the correct way to call it is

scanf_s("%49s", input, 50);

and also, scanf() returns a value, you should not ignore it, never

if (scanf_s("%49s", input, 50) == 1)
    printf("%s\n", input);

also, add the '\\n' to flush the output stream without the need for fflush() , and make your compiler as annoying as possible with warnings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM