当前位置:网站首页>Embedded-c Language-3
Embedded-c Language-3
2022-07-05 17:04:00 【Orange peel does not stop school】
One 、 Conversion of data types
1. Implicit conversion
characteristic : If different numbers in the expression have different data types ,gcc The compiler first converts different data types , Convert to the same data type before operation
Three cases of implicit conversion :
<1>. During implicit conversion, you must Convert the type with small memory into the type with large memory
<2>. If there are both integer data types and floating-point data types ,gcc The compiler automatically converts integer data types to floating-point types
<3>. If there are both unsigned and signed data types ,gcc The compiler automatically converts signed to unsigned data types
Be careful : The fatal flaw of implicit conversion is that the code is not readable
2. Coercive transformation
Cast syntax format : Target type variable = ( The target type ) Source type variable ;
for example :
char a = 90;
int b = (int)a; //gcc Force will a convert to int Types are assigned to variables b
Be careful : Forcing data type conversion may cause data loss
for example :
char a = (char)300; // Data lost
printf("a = %d\n", a); //a=44 = 300( It's too big ) - 128 = 172( It's too big )-128=44 ( Roll back )
therefore : Force conversion is from small to large , Or equal turns
Bear in mind : Either type conversion does not modify the value of the variable itself
for example :
int a = 555;
char b = (char)a;
printf("b = %d, a = %d\n"); //b = ? a = 555
Two 、C Process control of procedure
2.1 Branching structure : Conditional branch and switch branch
Conditional branch : Grammar format :
form 1: semantics : If the expression 1 It's true , Then execute the statement 1
if ( expression 1) {
sentence 1;
}
form 2: semantics : If the expression 1 It's true , Then run the statement 1, Otherwise, run the statement 2
if( expression 1) {
sentence 1;
}
else {
sentence 2;
}
form 3: semantics : If the expression 1 It's true , Then execute the statement 1, If the expression 1 Not true , So execute the expression 2, If the expression 2 It's true , Then execute the statement 2, If the expression 2 Not true , Judge down in turn
if( expression 1) {
sentence 1;
}
else if( expression 2) {
sentence 2;
}
...
else if( expression N) {
sentence N;
}
form 4: semantics : If the expression 1 It's true , Then execute the statement 1, If the expression 1 Not true , So execute the expression 2, If the expression 2 It's true , Then execute the statement 2, If the expression 2 Not true , Judge down in turn , If none of the above holds true , Last run statement M
if( expression 1) {
sentence 1;
}
else if( expression 2) {
sentence 2;
}
...
else if( expression N) {
sentence N;
}
else {
sentence M;
}
2.2 Precautions for using conditional branches
1.if and else Matching principle of :else perhaps else if And recent if pairing
2. If there is only one statement ,{} It can be omitted ( Sincerely suggest adding )
if(1 == a) { if (1 == a)
printf("a be equal to 1.\n"); Equivalent to -> printf("a be equal to 1.\n");
}
3.{} Whether to monopolize the same problem : According to the coding specification of the company
principle : As long as it is linux System software development ,{} Don't monopolize a line
//linux The programmer
if(1 == a) {
printf("a be equal to 1.\n");
}
// Company code specification
if(1 == a)
{
printf("a be equal to 1.\n");
}
Switch branch : Grammar format :
switch( Control expression ) {
case Constant expression 1:
sentence 1;
break;
case Constant expression 2:
sentence 2;
break;
...
case Constant expression N:
sentence N;
break;
default:
sentence M;
break;
}
Execution process : Control the value of the expression and the following case The value of the corresponding constant expression is the same ( equal )
Then the corresponding statement will be executed , Once encountered break,switch...case The switch branch ends immediately
If case It's not equal , Finally, only default The corresponding statement
Precautions for use of switch branch structure
The control expression is treated as an integer (int), Can be character , however Cannot be floating point numbers or strings ( for example :"abc")
for example :
switch(5) // Sure
switch(5.5) // Can not be
switch("abc") // Can not be
switch('a') // Sure
Constant expression must be constant , for example :5,'A',2+5 etc. , Duplicate branches are not allowed case
int cmd = 250;
switch(cmd) {
case 250:
break;
case 250: //gcc Compiler error
break;
}
If one Some case Corresponding break Does not add , When this case When established , And after running its statement Will continue with the next case So it is recommended that if the code needs break, finish writing sth. case After the list , Write a first break come out , Prevent forgetting !
int cmd = 250;
switch(cmd) {
case 250:
printf("1.\n");
case 251:
printf("2\n");
break;
...
default:
break;
}
result : Print 1 and 2
default Keywords can be written in any case In front of , But you need to add break, If default Key words are written at the end ,break It can be omitted
Branch case perhaps default If you define variables in , Remember to add {}, In the future, this variable can only be given to the corresponding case Use !
for example :
int cmd = 250;
switch(cmd) {
case 250:
printf("1.\n");
case 251: {
char c = '2'; // Defining character variables
printf("%c\n", c);
break;
}
...
default:
break;
}
summary :if...else and switch..case contrast :
1.switch...case What can be done ,if...else Can do it
2.if...else What can be done ,switch...case Not necessarily able to do , For example, dealing with floating-point numbers or strings
3.switch...case Some occasions are extremely tedious
for example : Judge 0~1000 The scope of the , If you use switch...case, You must add 1000 strip case
But if you use if sentence , Take one :if(xxx > 0 && xxx < 1000)
4.gcc The compiler to switch...case The amount of code generated is greater than if...else Less , therefore switch Efficient code execution
3、 ... and 、 The loop structure of the three structures of a program
3.1. Loop structure function : Repeat a set of statements multiple times
There are three types of loops :for loop ,while loop ,do...while loop
3.2.for loop
a) Grammar format :
for( expression 1; expression 2; expression 3) {
Loop statement ;
}
semantics ( Execute the process ):
The first step : First calculate , Run the expression 1( Only once )
The second step : And then calculate , Run the expression 2 If the expression 2 The result is true , Then execute the run loop statement if the expression 2 The result is false , that for Loop end exit
The third step : If if the expression 2 The result is true , Then execute the run loop statement , And when the loop statement is executed , Then calculate , Run the expression 3, expression 3 Operation completed , Repeat the second step
b)for The use of recycling :
form 1:( Be careful : here i There are corresponding technical terms : Loop variable )
int i;
for(i = 0; i < 5; i++) {
printf("i = %d\n", i);
}
form 2:
int i = 0;
for(; i < 5; i++) {
printf("i = %d\n", i);
}
form 3:
int i = 0;
for(; i < 5; ) {
printf("i = %d\n", i);
i++; // Loop variables are placed in loop statements
}
form 4: Dead cycle ( Be careful : Press ctrl+c Key exit program )
for(;;) {
printf("hello,world\n");
}
int i = 0;
for(;;) {
printf("i = %d\n", i);
i++; // Loop variables are placed in loop statements
if(i >= 5) {
break; // interrupt , end for loop , If for Loops are nested , Just interrupt the innermost layer for loop
}
}
form 5:
int i, j;
for(i = 0, j = 0; i < 5 && j < 5; i++, j++) {
printf("i = %d, j = %d\n", i, j);
}
form 6:
for(int i = 0; i < 5; i++) {
printf("i = %d\n", i);
}
printf("i = %d\n", i); //gcc Report errors
Be careful : If the loop variable is defined in the expression 1 Finish in , that Loop variables can only be used for for loop for Not available outside the loop
form 7:
int i;
for(i = 0; i < 5; i++) {
if(3 == i) {
continue;
// interrupt , End this cycle , So let's go to the next loop
// When i=3 when , perform continue keyword ,
// The following loop statements are no longer executed , Execute the expression directly 3,i++
// Then repeat the next cycle
}
printf("i = %d\n", i);
}
c)for If there is only one statement in a loop statement ,{} It can be omitted ( I don't recommend it )
for(int i = 0; i < 5; i++)
printf("i = %d\n", i);
3.3.while loop
a) Grammar format
while( Loop control expression ) {
Loop statement ;
}
semantics , Execute the process :
The first step : First calculate , Operation loop control expression if the result of calculation is true , Then execute the loop statement if the result of the calculation is false ,while The cycle ends directly
The second step : When the loop statement is completed , Continue to run the first step
for example :
int i = 0;
while(i < 5) {
printf("i = %d\n", i);
i++;
}
while Dead cycle :
while(1) {
printf("hello,world\n");
}
b) Be careful : If while There is only one loop statement in ,{} Can not add ( It's not recommended to do this )
int i = 0;
while(i++ < 5)
printf("i = %d\n", i);
3.4.do-while loop
a) Grammar format
do {
Loop statement ;
}while( Loop control expression ); // Remember that this semicolon cannot be omitted
semantics : Execute the process :
The first step : Execute the loop statement first ; // The first time I didn't make a judgment
The second step : And then calculate , Run the loop expression if true , Continue to execute the loop statement , Repeat in sequence , If it is false , The loop ends
characteristic :do-while At least once !
for example :
int i = 0;
do {
printf("i = %d\n", i);
}while(i++ < 5);
边栏推荐
- Get ready for the pre-season card game MotoGP ignition champions!
- Summary of PHP pseudo protocol of cisp-pte
- Precision epidemic prevention has a "sharp weapon" | smart core helps digital sentinels escort the resumption of the city
- [brush title] goose factory shirt problem
- 二叉树相关OJ题
- Wechat official account web page authorization login is so simple
- C how TCP restricts the access traffic of a single client
- Google Earth engine (GEE) -- a brief introduction to kernel kernel functions and gray level co-occurrence matrix
- C language to get program running time
- Facing new challenges and becoming a better self -- attacking technology er
猜你喜欢
Jarvis OJ simple network management protocol
Use JDBC technology and MySQL database management system to realize the function of course management, including adding, modifying, querying and deleting course information.
Deep learning plus
Benji Bananas 会员通行证持有人第二季奖励活动更新一览
项目引入jar从私服Nexus 拉去遇到的一个问题
深潜Kotlin协程(二十一):Flow 生命周期函数
Error in composer installation: no composer lock file present.
Detailed explanation of use scenarios and functions of polar coordinate sector diagram
Copy mode DMA
Application of threshold homomorphic encryption in privacy Computing: Interpretation
随机推荐
Iphone14 with pill screen may trigger a rush for Chinese consumers
二叉树相关OJ题
Flet tutorial 12 stack overlapping to build a basic introduction to graphic and text mixing (tutorial includes source code)
[wechat applet] read the life cycle and route jump of the applet
Android privacy sandbox developer preview 3: privacy, security and personalized experience
C how TCP restricts the access traffic of a single client
Twig数组合并的写法
Yarn common commands
Jarvis OJ Flag
干货!半监督预训练对话模型 SPACE
Sentinel flow guard
挖财股票开户安全吗?怎么开股票账户是安全?
精准防疫有“利器”| 芯讯通助力数字哨兵护航复市
[brush questions] effective Sudoku
[Jianzhi offer] 66 Build product array
Allusions of King Xuan of Qi Dynasty
Etcd build a highly available etcd cluster
浏览器渲染原理以及重排与重绘
【组队 PK 赛】本周任务已开启 | 答题挑战,夯实商品详情知识
齐宣王典故