In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you how to use scanf and scanf_s functions in C language, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
1.scanf_s (a function provided by vs)
a. Code 1
Int main () {char a = 0; / scanf_s ("% c", & a, 1); scanf_s ("% c", & a, sizeof (a)); return 0;}
Scanf_s has three parameters, and the last one is the size of the space occupied by the variable a (in bytes), where you can write either 1 or sizeof (a). If an is an integer, the third parameter is 4, or sizeof (a).
b. Code 2
Int main () {char a = 0, b = 0; scanf_s ("% c", & a, 1, & b, 1); printf ("% c", a, b); return 0;}
But don't write it as scanf_s ("% c% c", & a _
c. Code 3
Int main () {char arr1 [5] = {0}; scanf_s ("% s", arr1, sizeof (arr1)); printf ("% s", arr1); return 0;}
Note: when entering the number of characters, it must be smaller than the number of space in the array, for example, the code above can only enter 1-4 characters instead of 5, otherwise it will make an error. (arr [4] should have a newline character)
d. Safety detection
Int main () {char arr1 [5] = {0}; int c=scanf_s ("% s", arr1, sizeof (arr1)); printf ("% d\ n", c); printf ("% s", arr1); return 0;}
If the number of characters entered > = the number of array spaces, the return value of scanf_s is 0, and vice versa.
Int main () {char arr1 [5] = {0}; scanf_s ("% s", arr1, sizeof (arr1)); printf ("% s", arr1); return 0;}
If you enter the number of characters > = the number of array spaces, the elements in the array will not be printed. (the scanf function will print out)
2.scanf (standard library function)
a. Code 1
# define _ CRT_SECURE_NO_WARNINGS#include int main () {int a = 0; scanf ("% d", & a); printf ("% d", a); return 0;}
Using the scanf function under VS13 17 19 must be at the beginning (# define _ CRT_SECURE_NO_WARNINGS), otherwise an error will be reported.
b. Security testing
Int main () {char arr1 [5] = {0}; int c=scanf ("% s", arr1); printf ("% d\ n", c); printf ("% s", arr1); return 0;}
When we type abcdefg 7 characters (the number of space in the array has been exceeded), although it can be printed out, but the stack space near the arr1 has been destroyed, the situation has crossed the line. Scanf_s does not print out, so there is no out-of-bounds behavior.
c. When entering a format character in the scanf function
If% d% d (there is a space between% d% d), a space is also added between the two integers when typed through a standard input device (keyboard).
Such as a comma between% d% d (% d% d), a comma is also added between two integers when typed through a standard input device (keyboard).
3. Summary
Although the security of using scanf_s function is high, it is poor in portability. For example, code that contains scanf_s functions may not necessarily be able to run on other compilers.
Although the scanf function is not as secure as the scanf_s function, it is more portable than scanf_s.
The use and comparison of the scanf_s function and the scanf function is shared here today, if it is helpful to you, please pay attention to it and give a like by the way.
These are all the contents of the article "how to use scanf and scanf_s functions in C language". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.