Back home
中文
H7 / SECURITY RESEARCH NOTES

Chapter 4: Strings and Formatted Input/Output

  • The conversion specifier %s handles string input and output
  • A string is a sequence of one or more characters
- scanf_s()
# Reference Link
1.https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/scanf-s-scanf-s-l-wscanf-s-wscanf-s-l?redirectedfrom=MSDN&view=msvc-170 # Define of microsoft official
2.https://stackoverflow.com/questions/22024213/calling-scanf-s-with-array-of-chars # Introduce how to handle array of characters with scanf_s()
3.https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/countof-macro?view=msvc-170 # Introduce _countof()

- strlen()
# Reference Link
1.https://www.programiz.com/c-programming/library-function/string.h/strlen # Introduce how to use strlen()

- sizeof()
# Reference Link
1.https://www.geeksforgeeks.org/sizeof-operator-c/ # Introduce how to use sizeof()
  • For the sizeof() function, the corresponding conversion specifier is %zd
/*define of symbolic constant*/
#define TAXRATE 0.015
  • C90 added the const keyword to qualify a variable as read-only, making it more flexible to use than define.

  • size_t is the type returned by sizeof (C99)

  • For the conversion-specification modifiers and flags of printf(), see section 4.4.3

Three Ways to Break a String Across Lines:

Method 1:使用多个printf()语句,因为第一个字符串没有以\n字符结束,所以第二个字符串紧跟第1个字符串输出;
Method 2:使用反斜杠(\)和Enter(或Return)键组合来断行。(细节提示:下一行代码必须从最左边开始,如果进行了缩进,缩进的空格会成为字符串的一部分)
Method 3:在两个用双引号括起来的字符串之间用空白隔开,c编译器会把多个字符串看作为一个字符串。
  • printf() converts integers, floating-point numbers, characters, and strings into text displayed on the screen, while scanf() converts input strings into integers, floating-point numbers, characters, and strings.

Two Simple Rules About the scanf() Function:

1.如果用scanf()读取基本变量类型的值,在变量名前加上一个&。
2.如果用scanf()把字符串读入字符数组中,不要使用&。
  • For the conversion specifiers of scanf(), see section 4.4.5