当前课程知识点:C Programming > Chapter 1 Introduction > 1.2 A simple C Language program > 1.2 A simple C Language program.mp4
大家好
我是云南大学信息学院的丁海燕老师
欢迎走进C语言程序设计课堂
今天我们讲解的是一个简单的C语言程序
下面来看最简单的C语言程序举例
例1.1
输出一行文字“Hello,world!”
程序如下
#include
int main(void)
printf("Hello,world! ");
return 0;
计算机是如何将我们看上去似懂非懂的几行文字
变成显示在屏幕上我们能一目了然的一行文字呢
在对案例中的文字进行解析之后
相信就会有答案了
在这里
我们称案例中的6行文字为代码
称在屏幕输出的一行文字为该段代码的执行结果
解题思路
在主函数中用printf函数原样输出以上文字
前面提到过
C语言是结构化的语言
且均以函数的方式来表现
所以用C语言编写的代码都是由函数组成的
不论是由系统提供的库函数
还是编程者自己定义的函数
这种结构在该案例中有很好的体现
下面来看例1.2
多个函数构成的程序
求两个整数中较大者
解题思路
用一个函数实现求两个整数中的较大者
在主函数中调用此函数并输出结果
程序如下
#include
int main(void)
int max(int x,int y)
者是声明被调用函数max
int a,b,c;表示定义3个变量a、b、c
6scanf(“%d,%d”,&a,&b)
表示通过键盘输入变量a、b的值
c=max(a,b)
表示调用函数max
将其返回值赋给c
printf(“max=%d ”,c);
输出c的值
9return 0;
函数结束
下面是定义max函数
定义整型变量z
if(x>y) z=x;
者是通过if语句判断出x、y中较大者
else z=y
将较大值赋给z
return(z)将z作为函数Max的返回值
在main函数中
首先对被调用函数max进行声明
定义3个变量a、b、c
通过键盘输入变量a、b的值
调过函数max
将其返回值赋给c 输出c的值
而在函数Max的定义中
通过if语句判断出x、y中较大者
并将其赋值给z
将z作为函数max的返回值
代码行执行的顺序图如下
顺序执行1到7行 到第7行的时侯
程序流程转向执行11-17行
再返回第7行
继续执行8-10行
程序结束运行
当输入10,20时,输出较大数20
C源程序的一般形式如下
预处理命令
全局变量声明
自定义函数声明
int main(参数列表) 表示定义主函数
{
局部变量声明
语句序列;
return 0;
表示函数结束
f1(参数列表) 表示自定义函数f1
{
局部变量声明
语句序列;
}
等等等等我们还可以定义各个函数
一直到fn(参数列表) 表示自定义函数fn
同样也是局部变量声明
语句序列;
}
C语言允许用两种注释方式
•//:表示单行注释
它可单独占一行
可出现在一行中其他内容的右侧
也可以使用块式注释
/*......*/
可以包含多行
下面介绍C语言程序的结构
C语言程序的结构特点有以下8点
1一个程序由一个或多个源程序文件组成
小程序往往只包括一个源程序文件
例1.1只有一个函数
例1.2有两个函数
一个源程序文件中可以包括三个部分
预处理指令 #include 等
第二 全局声明 在函数之外进行的数据声明
3函数定义 每个函数用来实现一定的功能
2.函数是C程序的主要组成部分
一个C程序是由一个或多个函数组成的
且必须包含一个main函数也只能有一个
每个函数都用来实现一个或几个特定功能
被调用的函数可以是库函数
也可以是自己编制设计的函数
3.一个函数包括两个部分:
第一个部分 函数首部(即函数的第1行)
例如int max(int x,int y)
int是函数类型(即函数返回值类型)
max是函数名,x,y是函数参数
x,y前面的int是参数类型
若函数无参数
在括弧中写void或空括弧
函数包括的第2个部分是
函数体
函数体又包括声明部分和执行部分
声明部分不是必须的
主要是定义在本函数中所用到的变量
以及对本函数所调用函数进行声明
2执行部分
由若干个语句组成
指定在函数中所进行的操作
C语言程序的结构特点还有
程序总是从main函数开始执行
5C程序对计算机的操作由C语句完成
C程序书写格式比较自由
一行内可以写几个语句
一个语句可以分写在多行上
为清晰起见
习惯上每行只写一个语句
6数据声明和语句最后必须有分号
7 C语言本身不提供输入输出语句
8 程序应当包含注释
增加可读性
好了
同学们
一个简单的C语言程序我们就学习到这儿
下节课再见
-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