当前位置:网站首页>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的tensorRT加速的yolov5
- New programmers use the isXXX form to define Boolean types in the morning, and are discouraged in the afternoon?
- BigVision代码
- Pytorch轻量级可视化工具wandb(local)
- [mathematical logic] normal form (conjunctive normal form | disjunctive normal form | major item | minor item | maximal item | minor item | principal conjunctive normal form | principal disjunctive no
- Spark on yarn resource optimization ideas notes
- VS 2019 配置tensorRT生成engine
- 二进制流转换成字节数组
- 【PyG】理解MessagePassing过程,GCN demo详解
- 监听对象中值变化及访问
猜你喜欢
Vs 2019 configuration tensorrt
Vs 2019 installation and configuration opencv
MySql实战45讲【事务隔离】
左连接,内连接
敏捷认证(Professional Scrum Master)模拟练习题-2
Elsevier latex 提交文章 pdftex.def Error: File `thumbnails/cas-email.jpeg‘ not found: using draf
MySQL practice 45 [global lock and table lock]
VS code配置虚拟环境
[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
Yiwen takes you to know ZigBee
随机推荐
PAT乙级常用函数用法总结
L'index des paramètres d'erreur est sorti de la plage pour les requêtes floues (1 > Nombre de paramètres, qui est 0)
com.fasterxml.jackson.databind.exc.InvalidFormatException问题
销毁Session和清空指定的属性
Solve high and send system Currenttimemillis Caton
VS 2019 配置tensorRT生成engine
softmax的近似之NCE详解
Elsevier latex 提交文章 pdftex.def Error: File `thumbnails/cas-email.jpeg‘ not found: using draf
com. fasterxml. jackson. databind. Exc.invalidformatexception problem
I2C subsystem (IV): I2C debug
基于QT的tensorRT加速的yolov5
Model transformation onnx2engine
I2C 子系统(四):I2C debug
From C to capable -- use the pointer as a function parameter to find out whether the string is a palindrome character
idea 加载不了应用市场解决办法(亲测)
[mathematical logic] normal form (conjunctive normal form | disjunctive normal form | major item | minor item | maximal item | minor item | principal conjunctive normal form | principal disjunctive no
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
Agile certification (professional scrum Master) simulation exercise-2
VS 2019 配置tensorRT生成engine
Do you really understand relays?