- 转换说明符
%s处理字符串的输入和输出 - 字符串是一个或多个字符的序列
- 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()
- 对于
sizeof()函数,有对应的转换说明符%zd
/*define of symbolic constant*/
#define TAXRATE 0.015
-
C90新增了const关键字,用于限定一个变量为只读,用起来比define更加灵活。 -
size_t是sizeof返回的类型(C99) -
关于
printf()的转换说明修饰符和标记见章节4.4.3
给字符串(断行)换行有三种方法:
Method 1:使用多个printf()语句,因为第一个字符串没有以\n字符结束,所以第二个字符串紧跟第1个字符串输出;
Method 2:使用反斜杠(\)和Enter(或Return)键组合来断行。(细节提示:下一行代码必须从最左边开始,如果进行了缩进,缩进的空格会成为字符串的一部分)
Method 3:在两个用双引号括起来的字符串之间用空白隔开,c编译器会把多个字符串看作为一个字符串。
printf()把整数、浮点数、字符和字符串转换为显示在屏幕上的文本,而scanf()把输入的字符串转换成整数、浮点数、字符和字符串。
两条关于scanf()函数的简单规则:
1.如果用scanf()读取基本变量类型的值,在变量名前加上一个&。
2.如果用scanf()把字符串读入字符数组中,不要使用&。
- 关于
scanf()的转换说明符见章节4.4.5