c++ - printf 语句和 printf_s 语句到底有什么区别在VS2013编译环境当中?
PHP中文网
PHP中文网 2017-04-17 12:00:37
0
1
804

printf 语句和 printf_s 语句到底有什么区别在VS2013编译环境当中?
最近,在用VS2013编写C语言的程序,这个问题老是在我脑海里面。另外想问一下为什么,必须用scanf_s,不能用scanf语句呢?

PHP中文网
PHP中文网

认证高级PHP讲师

reply all(1)
Peter_Zhu

The main difference between printf_s and printf is that printf_s checks the format string for valid formatting characters, whereas printf only checks if the format string is a null pointer.

MSDN printf_s

So, the difference between printf and printf_s is that printf will only check whether the format string is empty, while printf_s will also check whether the user-defined format string is legal. For example:

    char* test = "Hello world!";
    char* formatStr = "%s%d%h\n";

    printf(formatStr, test,10);
    printf_s(formatStr, test, 10);

The format string given in the second line is problematic, but printf on the third line can still output "Hello world!10", and an error will be reported when the fourth line is executed.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template