简体   繁体   English

在Visual Studio C ++ 2010 Express中,scanf_s不适用于printf

[英]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. 当我在发布模式(Ctrl + F5)中运行它时,它不会打印任何内容,但是在调试模式(F5)中它可以打印任何内容,并且没有错误。 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. 当我使用scanf而不是scanf_s时,它会在调试和释放模式下进行打印。 What am I missing? 我想念什么?

This version of scanf() requires one more argument, the correct way to call it is 这个版本的scanf()需要另一个参数,正确的调用方式是

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

and also, scanf() returns a value, you should not ignore it, never 并且scanf()返回一个值,您不应忽略它,永远不要

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. 另外,添加'\\n'来刷新输出流,而不需要fflush() ,并通过警告使您的编译器尽可能令人讨厌。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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