当前位置:网站首页>Learning notes of C programming [compiled by Mr. Tan Haoqiang] (Chapter III sequence programming) 04 C sentence
Learning notes of C programming [compiled by Mr. Tan Haoqiang] (Chapter III sequence programming) 04 C sentence
2022-07-03 03:14:00 【ihan001】
List of articles
One 、C Sentence structure
1. Control statement :
① if()…else…( Conditional statements )
② for()…( Loop statement )
③ while()…( Loop statement )
④ do…while ()( Loop statement )
⑤ continue( End the loop )
⑥ break( To suspend execution switch Or loop statement )
⑦ switch( Multi branch select statements )
⑧ return( Return statement from function )
⑨ goto( Turn to statement , In structured programs, there is basically no need to goto sentence )
() Indicates that there is a discriminant condition in brackets
… Represents an embedded statement
2. Function call statements
A function call statement consists of a function call plus a semicolon .
printf("I am ihan001. ");
among printf("I am ihan001. "); It's a function call , Add a semicolon to make a statement .
3. Expression statement
An expression statement consists of an expression plus a semicolon , The most typical is that an assignment statement is composed of an assignment expression . for example :
a=3
Is an assignment expression , and
a=3;
It's an assignment statement .
4. Empty statement
;
A statement with only one semicolon is an empty statement .
Can be used as a turning point in the process ( The flow goes from other parts of the program to this sentence );
It can also be used as the loop body in a loop statement ( The loop body is an empty statement , Indicates that the loop does nothing ).
5. Compound statement
It can be used {} Put some sentences and statements together to form a compound sentence ( Also known as statement block ).
{
float pi=3.14159, r=2.5, area; // Defining variables
area=pi*r*r;
printf("area=%f",area);
}
Compound statements are often used in if Statement or loop , At this time, the program needs to execute a set of statements continuously .
The semicolon at the end of the last statement in a compound statement cannot be ignored without writing .
Two 、 Assignment statement
give an example 1: Give the three sides of a triangle , Find triangle area .
Ideas :area=√s(s−a)(s−b)(s−c), among s=(a+b+c)/2.
#include <stdio.h>
#include <math.h>
int main ()
{
double a,b,c,s,area; // Define variables , Are all double type
a=3.00; // To the side chief a assignment
b=5.00; // To the side chief b assignment
c=4.00; // To the side chief c assignment
s=(a+b+c)/2; // Calculation s
area=sqrt(s*(s-a)*(s-b)*(s-c)); // Calculation area
printf("a=%f\tb=%f\t%f\n",a,b,c); // Output trilateral a,b,c Value
printf("area=%f\n",area); // Output area area Value
return 0;
}
3、 ... and 、 Assignment operator
“=” The function of is to assign a data to a variable .
for example :a=3 The function of is to perform an assignment operation ( Or assignment operation ). Put constant 3 Assigned to a variable a.
You can also assign the value of an expression to a variable .
Four 、 Composite operators
In the assignment = Add the other operators before , Operators that can form compounds .
a+=3 Equivalent to a=a+3
x*=y+8 Equivalent to x=x*(y+8)
x%=3 Equivalent to x=x%3
Usually binary ( Two eyes ) Operator , Can be combined with the assignment character to form a composite assignment character .
The compound assignment operators related to arithmetic operations are +=,-=,=,/=,%=.
Be careful :
If the right side of the assignment character is an expression containing several items , It has parentheses . for example ,
x%=y+3 Equivalent to x=x%(y+3), Don't write it wrong x=x%y+3.
5、 ... and 、 Assignment expression
The function of assignment expression is to assign the value of an expression to a variable , Therefore, the assignment expression has the dual functions of calculation and assignment .
The process of solving the assignment expression is :
① Find “ expression ” Value ,② Assign to the variable to the left of the assignment operator . Since it's an expression , There should be a value , The value of the expression is equal to the value of the left variable after assignment .
The left side of the assignment operator should be a modifiable value “ The left value ”(left value, Shorthand for lvalue).
The expression that can appear to the right of the assignment operator is called “ Right value ”(right value, Shorthand for rvalue).
Be careful : Not all forms of data can be used as lvalues , Lvalue should be storage space and can be assigned . Variables can be used as lvalues , And arithmetic expression a+b Can't be used as an lvalue , Constants cannot be used as lvalues .
a=b=c=5 Expression value is 5,a,b,c Values are 5
a=5+(c=6) Expression value is 11,a The value is 11,c The value is 6
a=(b=4)+(c=6) Expression value is 10,a The value is 10,b be equal to 4,c be equal to 6
a=(b=10)/(c=2) Expression value is 5,a be equal to 5,b be equal to 10,c be equal to 2
a=(b=3*4) Expression value is 12,a,b Values are 12
The assignment expression makes the assignment operation not only appear in the assignment statement , And it can appear in other statements ( Such as output statement 、 Loop statement, etc )
Such as : printf("%d", a=b);
If b The value of is 3, The output a Value ( Also an expression a=b Value ) by 3. In a printf Function completes the dual functions of assignment and output .
6、 ... and 、 Type conversion during assignment
If the types on both sides of the assignment operator are the same , Then assign the value directly .
If the types on both sides of the assignment operator are inconsistent , But when they are all basic types , Type conversion is required during assignment . Type conversion is performed automatically by the system , The rule of transformation is :
Convert floating point data ( Including single 、 Double precision ) When you assign an integer variable , First round the floating-point number , That is, the decimal part is discarded , Then give an integer variable .
Assign integer data to a single 、 Double precision variable , Constant value , But it's stored in a variable as a floating point number .
Will a double Type data is assigned to float variable , First, convert double precision numbers to single precision numbers , That is to say, only 6~7 Significant digits , Store in float Type variable 4 In bytes . It should be noted that the size of the double precision value cannot exceed float The range of values for a variable of type ; Will a float Type data is assigned to double Type variable , Constant value , In memory with 8 Byte store , The number of significant digits extends to 15 position .
When character data is assigned to an integer variable , The ASCII Code assigned to integer variables .
When an integer with more bytes is assigned to an integer or character variable with less bytes , Only send its low byte intact to the assigned variable ( That is to say “ truncation ”).
边栏推荐
- Anhui University | small target tracking: large-scale data sets and baselines
- 900W+ 数据,从 17s 到 300ms,如何操作
- VS 2019 配置tensorRT生成engine
- The idea cannot be loaded, and the market solution can be applied (pro test)
- Basic information of Promethus (I)
- LVGL使用心得
- Le processus de connexion mysql avec docker
- Change cell color in Excel using C - cell color changing in Excel using C
- Unity3d RPG implementation (medium)
- 【PyG】理解MessagePassing过程,GCN demo详解
猜你喜欢
I2C 子系统(二):I3C spec
MySql实战45讲【事务隔离】
Segmentation fault occurs during VFORK execution
900W+ 数据,从 17s 到 300ms,如何操作
docker安装redis
Idea format code idea set shortcut key format code
Gavin teacher's perception of transformer live class - rasa project's actual banking financial BOT Intelligent Business Dialogue robot architecture, process and phenomenon decryption through rasa inte
Force deduction ----- the minimum path cost in the grid
softmax的近似之NCE详解
MySql實戰45講【SQL查詢和更新執行流程】
随机推荐
Docker install MySQL
900W+ 数据,从 17s 到 300ms,如何操作
MySql實戰45講【SQL查詢和更新執行流程】
Vs Code configure virtual environment
PAT乙级“1104 天长地久”DFS优化思路
Three.js本地环境搭建
解决高並發下System.currentTimeMillis卡頓
程序员新人上午使用 isXxx 形式定义布尔类型,下午就被劝退?
Creation and destruction of function stack frame
LVGL使用心得
MySql实战45讲【行锁】
How to make backgroundworker return an object
MySQL practice 45 lecture [transaction isolation]
【AI实战】应用xgboost.XGBRegressor搭建空气质量预测模型(一)
Spark on yarn resource optimization ideas notes
How to use asp Net MVC identity 2 change password authentication- How To Change Password Validation in ASP. Net MVC Identity 2?
Edit and preview in the back pipe to get the value writing method of the form
open file in 'w' mode: IOError: [Errno 2] No such file or directory
About HTTP cache control
MySQL practice 45 [SQL query and update execution process]