当前位置:网站首页>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 ”).
边栏推荐
- QT based tensorrt accelerated yolov5
- 45 lectures on MySQL [index]
- C # general interface call
- Cron表达式介绍
- TCP 三次握手和四次挥手机制,TCP为什么要三次握手和四次挥手,TCP 连接建立失败处理机制
- open file in 'w' mode: IOError: [Errno 2] No such file or directory
- softmax的近似之NCE详解
- Sous - système I2C (IV): débogage I2C
- 后管中编辑与预览获取表单的值写法
- PAT乙级“1104 天长地久”DFS优化思路
猜你喜欢

I2C subsystem (IV): I2C debug

Nasvit: neural architecture search of efficient visual converter with gradient conflict perception hypernetwork training

umi 路由拦截(简单粗暴)

Idea format code idea set shortcut key format code

Thunderbolt Chrome extension caused the data returned by the server JS parsing page data exception

Sous - système I2C (IV): débogage I2C

分布式事务

你真的懂继电器吗?

Use of El tree search method

Yiwen takes you to know ZigBee
随机推荐
Bigvision code
[Fuhan 6630 encodes and stores videos, and uses RTSP server and timestamp synchronization to realize VLC viewing videos]
How do you adjust the scope of activerecord Association in rails 3- How do you scope ActiveRecord associations in Rails 3?
@Accessors注解作用指定前缀遵守驼峰命名
模糊查询时报错Parameter index out of range (1 > number of parameters, which is 0)
Application of derivative in daily question
用docker 連接mysql的過程
Update and return document in mongodb - update and return document in mongodb
The difference between componentscan and componentscans
Yiwen takes you to know ZigBee
力扣------网格中的最小路径代价
Le processus de connexion mysql avec docker
二维数组中的元素求其存储地址
[shutter] monitor the transparency gradient of the scrolling action control component (remove the blank of the top status bar | frame layout component | transparency component | monitor the scrolling
umi 路由拦截(简单粗暴)
文件重命名
解决高并发下System.currentTimeMillis卡顿
Hi3536C V100R001C02SPC040 交叉编译器安装
Vs 2019 configuration tensorrt
[combinatorics] number of solutions of indefinite equations (number of combinations of multiple sets R | number of non negative integer solutions of indefinite equations | number of integer solutions