当前位置:网站首页>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);
边栏推荐
- 干货!半监督预训练对话模型 SPACE
- Jarvis OJ webshell analysis
- Writing method of twig array merging
- C language to get program running time
- [61dctf]fm
- [first lecture on robot coordinate system]
- 关于new Map( )还有哪些是你不知道的
- Learnopongl notes (II) - Lighting
- 【beanshell】数据写入本地多种方法
- Combined use of vant popup+ other components and pit avoidance Guide
猜你喜欢
Machine learning compilation lesson 2: tensor program abstraction
dried food! Semi supervised pre training dialogue model space
调查显示传统数据安全工具面对勒索软件攻击的失败率高达 60%
Android privacy sandbox developer preview 3: privacy, security and personalized experience
如何将mysql卸载干净
Hiengine: comparable to the local cloud native memory database engine
Jarvis OJ shell流量分析
深潜Kotlin协程(二十一):Flow 生命周期函数
Scratch colorful candied haws Electronic Society graphical programming scratch grade examination level 3 true questions and answers analysis June 2022
Jarvis OJ shell traffic analysis
随机推荐
【Web攻防】WAF检测技术图谱
If you can't afford a real cat, you can use code to suck cats -unity particles to draw cats
Flet tutorial 12 stack overlapping to build a basic introduction to graphic and text mixing (tutorial includes source code)
Excuse me, is the redis syntax used in DMS based on the commands of the redis community version of the cloud database
Scratch colorful candied haws Electronic Society graphical programming scratch grade examination level 3 true questions and answers analysis June 2022
拷贝方式之DMA
精准防疫有“利器”| 芯讯通助力数字哨兵护航复市
Jarvis OJ 简单网管协议
PHP strict mode
Android 隐私沙盒开发者预览版 3: 隐私安全和个性化体验全都要
【微信小程序】一文读懂小程序的生命周期和路由跳转
项目引入jar从私服Nexus 拉去遇到的一个问题
sqlserver 做cdc 要对数据库性能有什么要求么
Jarvis OJ Telnet Protocol
【剑指 Offer】63. 股票的最大利润
Hiengine: comparable to the local cloud native memory database engine
PHP人才招聘系统开发 源代码 招聘网站源码二次开发
Writing method of twig array merging
[Jianzhi offer] 66 Build product array
composer安装报错:No composer.lock file present.