当前课程知识点:C Programming > Chapter 3 Programming in C > 3.7 Formatted input scanf function > 3.7 Formatted input scanf function.mp4
大家好
我是云南大学信息学院丁海燕老师
欢迎走进C语言程序设计课堂
今天我们讲解格式化输入scanf函数
scanf函数功能是
从键盘上输入数据
该输入的数据
按指定的输入格式被赋给相应的输入项
scanf函数格式为
scanf
双引号格式控制字符串双引号
输入项列表
输入项列表可以是变量的地址
或字符串的首地址
例如
scanf
a=%d
逗号b=%d
&a&b
scanf
%d%f
&a&f
scanf
%o%f
&b&x
例如
scanf
c=%d
d=%d
回车
其中
格式控制字符串
是用双引号括起来的字符串
由格式说明和普通字符两部分组成
输入项列表可以是变量的地址
或字符串的首地址
其类型与个数
必须与格式控制字符串中格式字符的类型
个数一致
当有多个输入项时
各项之间用逗号分隔
scanf
c=%d
逗号
d=%d
/n
可以输入c=6
,d=8 回车
三
格式说明
格式说明一般格式为
%修饰符
格式字符
scanf函数的格式声明与
printf函数中的格式声明相似
以%开始
以一个格式字符结束
•中间可以插入附加的字符
修饰符是可选的
例如scanf
a=%f,b=%f,c=%f
&a&b&c
格式字符规定了对应输入项的输入格式
常用格式字符见下表
整型数据有4种格式字符
实型数据有2种格式字符
字符型数据有2种格式字符
如表所示
d(ld)
输入有符号十进制整型或长整型数
u或者lu
表示输入无符号十进制整数或长整型数
o或者lo
表示输入八进制整数或长整型数
x或lx
表示输入十六进制整型或长整型数
f或lf
表示输入小数形式的单精度
或双精度实型数
e或le
表示输入指数形式的单精度
双精度实型数
c表示输入一个字符
s表示输入字符串
scanf函数中使用的修饰符如下
m指定数据输入的宽度
对float和double型数据
宽度指整数位数加小数点加小数位数
星号表示忽略输入的数据
即不将输入数据赋给对应变量
例如
%3d
表示输入一个三位的十进制整数
%*d
忽略输入的整型数
不把它赋给变量
%md
可以连续输入一组数字
而不需要使用分隔符
系统会自动按域宽
截取数字
截给对应的变量
例如
scanf
%d%d%d
输入3个整型十进制数
以空格
tab键或回车键分隔
例如scanf
%d, %o, %f
输入3个数
以,分隔
scanf
a=%d, b=%d
则输入的形式是
a=32, b=28
普通字符要照原样输入
使用scanf函数时应注意的问题
scanf %f%f%f
abc错误
而scanf%f%f%f
&a,&b,&c 正确
对于
scanf a=%f b=%f c=%f
1空格3空格2 回车
错误
正确的格式为
a=1,b=3,c=2
没有逗号也是错误
注意
要求在程序运行中输入数据
输入的数据个数和类型
必须与格式说明符一一对应
地址参数形式
&变量名
数组或指针变量除外
格式控制中有普通字符时
必须照原样输入
格式控制中无普通字符时
输入的数值型数据和字符串用空白符分隔
字符型数据不必分隔
double型数据输入时
必须用%lf
或%le格式
实型数输入时域宽
不能用m.n形式的附加说明
为了减少不必要的输入量
除了逗号
分号
空格符以外
格式控制中尽量不要出现普通字符
也不要使用斜线n
斜线t 等转义字符
下面来看例3.8
整型数据的输入与输出
程序如下
int a b c d
定义整型变量a b c d
scanf %d
,%d"
用逗号分隔2个输入的整型数据
printf a=%d
, b=%d
用指定格式输出整型变量a b的值
程序运行结果如下
下面来看例3.9
单精度
双精度实型数据的输入与输出
程序如下
定义X1Y1两个实型变量
scanf %f
输入X1的值
scanf %lf
输入Y1的值
输出X1Y1
用%5.2f的格式输出X1Y1
单精度数据输入时用%f或%e
双精度数据输入时用%lf或%le
但输出时
无论单精度还是双精度数据
都可用%f或%e
程序运行时
输入5.3和3.1415926
可用空格
Tab键或回车分隔多个输入数据
程序运行结果如图所示
下面来看例3.10
字符数据的输入与输出
char c1 c2 c3
scanf %c
,%c
,%c
输入三个字符赋给变量c1 c2 c3
printf
c1=%c
,c2=%c
,c3=%c斜线n
在printf()的格式控制字符串中的
普通字符要原样输出
程序运行结果如图所示
下面来看例3.11
字符串的输出与输出
定义字符数组s1 s2
用格式说明%s输入字符串s1 s2
程序运行结果如图所示
用%s输入字符串s1和s2时
用空格作为分隔符
好了
同学们
格式化输入scanf函数
我们今天就学习到这儿
下节课再见!
-1.1 The development and characteristics of C language
--1.The development and characteristics of C language .mp4
--1.1 Self test questions
-1.2 A simple C Language program
--1.2 A simple C Language program.mp4
--【Source program】 Example 1.1 output a line of text Hello, world! "
--【Source program】 Example 1.2 program composed of multiple functions, find the larger of two integers
--1.2 Self test questions
-1.3 Program, Programming Language and C Program Running Steps
--1.3 Program, Programming Language and C Program Running Steps.mp4
--Running steps of C source program in CodeBlocks
--1.3 Self test questions
-chapter 1 Self-Test
-2.1 The Concept and Description of Algorithms
--2.1 The Concept and Description of Algorithms.mp4
--2.1 Self test questions
-2.2 Examples of Simple Algorithms, Computational Thinking and Structured Programming
--2.2 Examples of Simple Algorithms, Computational Thinking and Structured Programming.mp4
--2.2 Self test questions
-Chapter 2 Self test questions
-3.1 Simple Structure and Identifier of C Language Program
--3.1 Simple Structure and Identifier of C Language Program.mp4
--【Source program】 Example 3.1 input two integers and output the sum of two numbers.
--【Source program】 Example 3.2 input two integers and output the average value.
--【Source program】 Example 3.3 output the value of the character variable.
--3.1 Self test questions
-3.2 Constants, Variables and Assignments
--3.2 Constants, Variables and Assignments.mp4
--3.2 Self test questions
-3.3 Arithmetic, assignment, increment and decrement operators
--3.3 Arithmetic, assignment, increment and decrement operators.mp4
--3.3 Self test questions
-3.4 Conditions, commas, addresses, byte operators, and mixed operations among various numerical data
--3.4 Conditions, commas, addresses, byte operators, and mixed operations among various numerical data
--【Source program】 example of sizeof
--3.4 Self test questions
-3.5 Input and Output Examples and Character Input and Output
--3.5 Input and Output Examples and Character Input and Output.mp4
--【Source program】 Example. Find the root of quadratic equation of one variable.
--【Source program】 Example 1. Successively output three characters of BOY.
--【Source program】Example 2. Input 3 characters of BOY from the keyboard and output them to the screen
--3.5 Self test questions
-3.6 Formatted output printf function
--3.6 Formatted output printf function.mp4
--【Source program】 Example 3.4 output of integer data.
--【Source program】 Example 3.5 real data output.
--【Source program】 Example 3.6 character data output.
--【Source program】 Example 3.7 uses %s to output a string.
--3.6 Self test questions
-3.7 Formatted input scanf function
--3.7 Formatted input scanf function.mp4
--【Source program】 Example 3.8 Input and output integer data
--【Source program】Example 3.9 Input and output single precision and double precision real data.
--【Source program】Example 3.10 input and output character data.
--【Source program】Example 3.11 input and output string.
--3.7 Self test questions
-3.8 Basic Data Types of C Language
--3.8 Basic Data Types of C Language.mp4
--3.8 Self test questions
-Chapter 3 Self test questions
-Operator and expression self test questions
-4.1 Relational operators, logical operators, and if statements
--4.1 Relational operators, logical operators, and if statements.mp4
--【source example】Example 4.1 Find the root of the quadratic equation of one variable.
--【Source program】Example 4.2 Input two real numbers and output them from small to large.
--4.1 Self test questions
-4.2 Switch Statement
--【Source program】Example 4.3 uses switch statement to implement simple menu program
--【Source program】Example 4.4 converts centesimal score into the corresponding grade system score.
--4.2 Self test questions
-4.3 Examples of Selection Structural Programs
--4.3 Examples of Selection Structural Programs.mp4
--【Source program】 Example 4.5 determines whether a year is a leap year.
--【Source program】 Example 4.6 find the solution of quadratic equation of one variable.
--【Source program】Example 4.7 calculate the transportation cost for the user.
--4.3 Self test questions
-Chapter 4 Self test questions
-5.1 While and Do-While Statement
--5.1 While and Do…while statement.mp4
--【Source program】example 5.1 Use while statement to find 1 + 2 + 3 + +100
--【Source program】example 5.2 using do while statement to find 1 + 2 + 3 + +100
--5.1 Self test questions
-5.2 For Statement
--【Source program】example 5.3 Use for loop to find 1 + 2 + 3 +100
--5.2 Self test questions
-5.3 Change the State of Loop Execution and Nested Loop
--5.3 Change the state of loop execution and nested loop .mp4
--【Source program】example 5.4 collecting charity donations
--【Source program】example 5.5 output a number between 100 and 200 that cannot be divided by 3.
--【Source program】multiplication table
--【Source program】example 5.6 Output the following 4*5 matrix.
--5.3 Self test questions
-5.4 Loop Structure Program example 1
--5.4 Loop structure program example 1.mp4
--5.4 Self test questions
-5.5 Loop Structure Program example 2
--5.5 Loop structure program example 2.mp4
--【Source program】example 5. Output the following figure.
--【Source program】example6. The problem of 100 chickens and 100 coins
--【Source program】example 7. Find the approximate value of PI
--5.5 Self test questions
-Chapter 5 Self test questions
-6.1 Definition, Reference and Initialization of One-Dimensional Arrays
--6.1 Definition, Reference of One-Dimensional Arrays.mp4
--6.1 Self test questions
-6.2 One-Dimensional Array Programming
--6.2 One-Dimensional Array Programming.mp4
--【Source program】example 1. Fibonacci sequence
--【Source program】example 2. Find the maximum value and the minimum value.
--【Source program】example 3 exchanging array elements in reverse order.
--【Source program】example 4. Bubble sorting.
--6.2 Self test questions
-6.3 Definition, Reference and Initialization of Two-Dimensional Arrays
--6.3 Definition, Reference of Two-Dimensional Arrays.mp4
--6.3 Self test questions
-6.4 Two-Dimensional Array Programming
--6.4 Two-Dimensional Array Programming.mp4
--【Source program】example 1. Get the average score of each subject.
--【Source program】Example 2. The elements of row and column of a 2D array are interchanged
--6.4 Self test questions
-6.5 Definition, initialization and input and output of character arrays
--6.5 Definition, initialization and input and output of character arrays .mp4
--【Source program】Example 6.6 Output a known string.
--【Source program】Example 6.7 Output a diamond.
--【Source program】Example 6.10 Sorting of strings.
--6.5 Self test questions
-6.6 String Processing Function
--6.6 String Processing Function.mp4
--6.6 Self test questions
-6.7 Character Array Programming
--6.7 Character Array Programming.mp4
--6.7 Self test questions
-Chapter 6 Self test questions
-7.1 Function Concept and How to Define and Call Functions
--7.1 Function Concept and How to Define and Call Functions.mp4
--7.1 Self test questions
-7.2 Data Transfer in Function Call, Call Procedure and Function Return Value
--7.2 Data Transfer in Function Call, Call Procedure and Function Return Value.mp4
--7.2 Self test questions
-7.3 Declarations of called functions and nested calls to functions
--7.3 Declarations of called functions and nested calls to functions .mp4
--【Source program】Example 7.4 use a function to find the sum of t
--7.3 Self test questions
-7.4 Recursive call to function
--7.4 Recursive call to function.mp4
--【Source program】Example 7.6. How old is the fifth student?
--【Source program】Example 7.7 Use recursion method to find n!
--7.4 Self test questions
-7.5 Array as function parameter (1)
--7.5 Array as function parameter (1).mp4
--【Source program】Example 7.10
--7.5 Self test questions
-7.6 Array as function parameter (2)
--7.6 Array as function parameter (2).mp4
--【Source program】Selection sorting.
--【Source program】Example 7.13 find the maximum of a 3×4 matrix.
--7.6 Self test questions
-7.7 Local and global variables, internal and external functions
--7.7 Local and global variables, internal and external functions.mp4
--【Source program】Example 7.14
--7.7 Self test questions
-7.8 The Survival Period of Variables and the Storage Mode of Local Variables
--7.8 The Survival Period of Variables and the Storage Mode of Local Variables.mp4
--【Source program】Example 7.17
--7.8 Self test questions
-7.9 Storage Categories of Global Variables
--7.9 Storage Categories of Global Variables.mp4
--7.9 Self test questions
-Chapter 7 Self test questions
-8.1 Pointer Concept, Definition and Reference of Pointer Variables
--8.1 Pointer Concept, Definition and Reference of Pointer Variables.mp4
--8.1 Self test questions
-8.2 Pointer variables as function parameters
--8.2 Pointer variables as function parameters.mp4
--【Source program】Example 8.3. Exchange two data with function call.
--8.2 Self test questions
-8.3 Pointer of array element, operation of pointer and reference of array element by pointer
--8.3 Pointer of array element, operation of pointer and reference of array element by pointer.mp4
--【Source program】Point to array elements with pointer variables.
--8.3 Self test questions
-8.4 Using Array Name as Function Parameter
--8.4 Using Array Name as Function Parameter.mp4
--【Source program】Store array elements in reverse order.
--8.4 Self test questions
-8.5 Reference to multidimensional arrays by pointers
--8.5 Reference to multidimensional arrays by pointers.mp4
--【Source program】Pointer variable pointing to one-dimensional array.
--8.5 Self test questions
-8.6 Referencing strings through pointers
--8.6 Referencing strings through pointers.mp4
--【Source program】Reference string by pointer.
--8.6 Self test questions
-8.7 Character pointer as function parameter
--8.7 Character pointer as function parameter.mp4
--【Source program】Character pointer as function parameter.
--8.7 Self test questions
-8.8 Pointer pointing to function
--8.8 Pointer pointing to function.mp4
--【Source program】Pointer variable pointing to the function.
--8.8 Self test questions
-8.9 Functions that return pointer values
--8.9 Functions that return pointer values.mp4
--【Source program】Intercept substring.
--8.9 Self test questions
-8.10 Pointer arrays and multiple pointers
--8.10 Pointer arrays and multiple pointers.mp4
--【Source program】Sorting of strings.
--8.10 Self test questions
-8.11 Dynamic memory allocation and pointer variables pointing to it
--8.11 Dynamic memory allocation and pointer variables pointing to it.mp4
--【Source program】Dynamic memory allocation.
--8.11 Self test questions
-Chapter 8 Self test questions
-9.1 Define and use structural variables
--9.1 Define and use structural variables.mp4
--【Source program】Structure variable
--9.1 Self test questions
-9.2 Using structure arrays
--9.2 Using structure arrays.mp4
--【Source program】Structure array
--9.2 Self test questions
-9.3 Structure pointer
--【Source program】Structure pointer
--9.3 Self test questions
-Chapter 9 Self test questions
-CodeBlocks Baidu online disk download address
-Final Exam
--final exam