C语言编程学习课件 (9).pdf
Programming In CProgramming In C Programming In CProgramming In C A huge tree that fills ones arms grows from a tiny seedling;a nine-storied tower rises from a heap of earth.-Lao Tzu(the founder of the Taoist)Programming In CProgramming In C C programs can be composed of multiple functions Program#include stdio.h#define PI 3.14159 float CalcArea(float r);void main()float r,area;scanf(%f,&r);area=CalcArea(r);printf(Area=%f,area);float CalcArea(float r)float a;if(r=0)a=0;else a=PI*r*r;return(a);A function consists of a series of statements The statement performs a specific operation C statements-declarations/The C90 standard#include stdio.h#define PI 3.14159 float CalcArea(float r);void main()float r,area;scanf(%f,&r);area=CalcArea(r);printf(Area=%f,area);float CalcArea(float r)float a;if(r=0)a=0;else a=PI*r*r;return(a);/The C99 standard void main()char ch;printf(Enter Age);int age;scanf(%d,&age);printf(%c,%dn,ch,age);void main()float r,area;scanf(%f,&r);area=CalcArea(r);printf(Area=%f,area);C statements-expression statement An expression statement is formed by adding a semicolon after the expression.C statements-control statement The statements used to control and change the execution flow of a program are called control statements.float CalcArea(float r)float a;if(r=0)a=0;else a=PI*r*r;return(a);C statements-compound statement In control statements,there are usually multiple statements executed after a certain condition is met.They are either all executed or not,logically forming a whole,which is called a subprogram or a compound statement.These statements are enclosed in a pair of braces to form a compound statement.float CalcArea(float r)float a;if(r=0)a=0;else a=PI*r*r;return(a);C statements-null statement A single semicolon forms null statement.It does not perform any operation.It is often used as the entry of program transfer or the breakpoint during debugging.It does not perform any operation.It is often used as the entry of program transfer or the breakpoint during debugging.expression In C,constants,variables,and function calls are all called the expressions.The legal expressions combining them with operators are also called the expressions.1515、b b、a/3a/3-b*100b*100 printf(Stop)printf(Stop)the constant 15,the variable b,and the function printf()are all the expressions.The last output is also an expression.the possible side effects pay a special attention to the possible side effects after expression operations.#include stdio.h void main()int num,a,b;num=printf(Enter:)+scanf(%d%d,&a,&b);printf(n Num=%d a=%d,b=%dn,num,a,b);6 2 Enter:Num=8 a=123,b=7234 123 7234 Operators Operators are used with one or more operands to perform some action in C.Operators are the verbs of the C language.Operators while learning,we need to pay special attention to the following five points:(1)The writing form and function of the operator.(2)Restrictions on the numbers and types of operations(3)Precedence:The operator precedence is divided into 15 levels.(4)Association direction:left association and right association.(5)Type conversion:Different types of operations are automatically converted before the operation.a simple example Program#include stdio.h#define PI 3.14159 void main()float r,area;scanf(%f,&r);/s1 area=PI*r*r;/s2 printf(Area=%f,area);/s3 Area=314.159000 10 10 314.15900 r area output Programming In CProgramming In C