当前位置:网站首页>Operators are also important if you want to learn the C language well
Operators are also important if you want to learn the C language well
2022-06-27 12:08:00 【Program ape teaches you to play basketball】

Fortune favors the bold.
Fortune favors the brave .
Catalog
2、 Continuation and transition characters
4.3 A written test of logic and logic or
1、 Annotation symbols
1.1 Basic notes for notes
For a better demonstration, the following code will be in Linux Demonstration under the platform ( \ Is a continuation character ):
This code , Which line is problematic ? 
There may be a little friend here who has questions , Why is this the only line that has gone wrong ? We know that annotations are recognized by the compiler in the preprocessing phase , Here we can use Linux An order of :gcc -E test.c -o test.i That's right test.c Program translation , Finally, the preprocessing results are preserved , Then you can use vim Open our preprocessing result file .

Finally, we can come to the conclusion that : A comment that is replaced is essentially replaced with a space !
Here we want to make another simple point ,C Language annotations cannot be nested ,/* Always with the nearest */ matching , Interested partners can go to test :
int main()
{
/*
/*printf("hello world");
printf("hello world");*/
*/
return 0;
}Here's another interesting code , If you use C Language notes , If only one appears /* The following... Could not be found */ Words , Then he will default that all the following code is annotated !
int main()
{
int x = 10;
int y = 5;
int ret = 0;
int* p = &y;
ret = y/*p;
return 0;
}So how to solve the above situation ?
- stay / And * Add a space in : ret = y / *p;
- hold *p Enclosed in brackets : ret = y / (*p);
Recommend the second , Because it looks more intuitive
1.2 How to write good notes ?
[ Suggest 1 ] Notes should be accurate and easy to understand , There must be no controversy , Wrong comments are harmful .
[ Suggest 2 ] Make sure that the comments are consistent with the code , Useless comments should be deleted in time .
[ Suggest 3 ] For global data ( Global variables 、 Constant definition ) It must be annotated .
[ Suggest 4 ] A clear statement can be left uncommented , such as :i++; // i Self increasing 1.
[ Suggest 5 ] The position of the comment should be adjacent to the code being described , Can be on the same line as the statement , Not below .
[ Suggest 6 ] When the code is long , Especially when there are multiple sets of them , There should be a note at the end of some paragraphs .
[ Suggest 7 ] The indentation of comments should be consistent with that of code .
[ Suggest 8 ] Annotation code should focus on ,“ Why do ” , instead of “ How do you do it? ”.
[ Suggest 9 ] The unit of value must be annotated .
[ Suggest 10 ] In complex functions , In branch statement 、 Appropriate comments are required after the loop statement ends , It is convenient to distinguish each branch or loop .
2、 Continuation and transition characters
2.1 Continuation function
C Use backslashes in language ( \ ) It means the flight is cut off . The compiler will remove the backslash , The character following the backslash is automatically continued to the previous line . But notice : No space after backslash , Interested partners can come down and test :
int main()
{
int a = 1;
int b = 2;
int c = 3;
if (1 == a && \
2 == b && \
3 == c)
{
printf("You can see me!\n");
}
else
{
printf("You can not see me!\n");
}
return 0;
}Maybe some friends will be curious , Obviously I don't have to \ You can also change lines without reporting errors , Then why use \ Means line feed ? As a programmer , Others may also read our code , If not \ Straight line breaks can be strange , You can also tell the compiler that this place is a continuation line , More semantic !
2.2 Escape character
C In language , There are some characters , It's his literal meaning , such as 'n','b','t'. There are also some characters , It has a special meaning , such as :" , ', \ The essential meaning of escape is : Literally to special , Or special to literal .
\ There are two uses :
- When the continuation character uses
- Escape characters use (a. Literally to special b. Special to literal )
Let's focus on \n and \r Maybe all my friends have heard of it Line break and enter Well ! So what they essentially mean is different ! But now many compilers make Line break ( \n ) It also contains enter ( \r ) The function of ! What is the difference between them ?
Line break : Move the cursor to the next line
enter : The cursor returns to the beginning of the current line
Here you can try it C Language to write a simple countdown function , You can clearly distinguish between line feed and carriage return !

Here we can also take a look at a pen test :
// What does the following program output ?
#include <string.h>
#include <stdio.h>
int main()
{
printf("%d\n", strlen("abcdef"));
// \62 Parsed into an escape character
printf("%d\n", strlen("c:\test\628\test.c"));
return 0;
}
Here is the first print function , If we know ,strlen() The function is to find the string \0 The number of characters before , So the first one to print is 6 , The second print function , First of all \t Parsed into an escape character , \62 It is also parsed as an escape character (8 Base number ) So the second print should be 14 !
3、 Single and double quotes
3.1 Basic concepts
about C Everyone who knows the language knows it , Single quotation marks are all character constants , The double quotation marks are all string constants , But it is still easy for beginners to make mistakes , such as 'a', and "a" Is a completely different concept , In the string , With \0 For the end , It doesn't belong to the element content of string in essence , It can only be said that it is a string end identifier , But it takes up space ! That is to say 'a' Will take up one byte and "a", It will occupy two bytes .
With the above simple concept, let's take a look at an example :
There may be a little friend who has questions , The first two and the last one printf I can still understand the printed results , But why does the third one occupy four bytes ? This is obviously not a character constant ?(C++ The third size in is 1 Bytes , Because this is about C Language so we don't talk about other languages )
Actually in C99 Provisions of the standard ,'1' It's called an integer character constant (integer character constant), To be seen as int type .
Just use “ The standard stipulates ” To prove infeasible , We will use another example to prove :

Don't be surprised to see this result , I just said that , The standard stipulates , Integer character constants are treated as int type , So he will have four bytes of space , And my current computer is a small end storage , So the low byte order is placed at the low address , Those in charge , Will a 4 Bytes of data into char Data truncation occurs in variables of type , A byte of the low address will be put into char In variables of type , So in the end c There is only one in the variable 'd' character !
But I don't recommend the above way !!! If only we knew the truth .
Then some of the little friends here started to experiment again , So I wrote this code :

Because an integer character constant has only 4 Byte space , How can he let go 5 Bytes or more ?
3.2 A special case
Obvious , Null integer character constants are not allowed , But an empty string can appear , Because the string end identifier \0 Although it can not count the contents of string elements , But it also takes up a lot of space .
4、 Logical operators
4.1 && ( Logic and )
Concept : Cascade two ( Multiple ) Logical expression , It has to be true at the same time , The result is true .
Example : 
For the short circuit of logic and , Because our compiler scans from left to right , So if the left side of the expression is false , It will not execute the logic and the expression on the right , This is what we call short circuit phenomenon :

4.2 || ( Logic or )
Concept : Cascade two ( Multiple ) Logical expression , At least one must be true , The result is true .
Example :
The above example also contains the short circuit phenomenon of logical or , In logic or , Because the compiler scans the code from left to right , So as long as the left side of the expression is true , You don't need to make the right judgment .
4.3 A written test of logic and logic or
Because for the front ++ And post ++ I haven't said yet , So the students who have the foundation can have a look first , If you don't have a foundation, you can wait until I issue the symbol in the next issue. The second issue will come back to see this problem .

- The first logic and expression ,a The initial value of the variable is 0 , And it's post ++, Use the value of the expression first , In the process of self increasing , Logic and must satisfy that the values on both sides of the expression are true , If one is false, the following expression will not be executed , So only a The variable has changed .
- The second logical or expression , Logic or as long as one of the two sides of the expression is true ,a++ To judge as false , Will continue to judge ++b, In front of ++ It's self increasing first , therefore ++b Expression is true , You won't execute the following expression , in other words , Only a and b The value of the variable has changed .

Future and roses , The coming days would be long
边栏推荐
- 2022ciscn central China Web
- The R language uses the DOTPLOT function of epidisplay package to visualize the frequency of data points in different intervals in the form of point graph, specifies the grouping parameters with the b
- Open source model library of flying propeller industry: accelerating the development and application of enterprise AI tasks
- 如何修改 node_modules 里的文件
- 数学知识——博弈论(巴什博奕、尼姆博奕、威佐夫博奕)思路及例题
- Deep understanding of happens before principle
- 2022CISCN华中 Web
- 盘点一些好用且小众的 Markdown 编辑器
- 深入理解 happens-before 原则
- Topic37——64. 最小路径和
猜你喜欢

Qstype implementation of self drawing interface project practice (I)
![Jerry's serial port communication serial port receiving IO needs to set digital function [chapter]](/img/d1/5beed6c86c4fdc024c6c9c66fbffb8.png)
Jerry's serial port communication serial port receiving IO needs to set digital function [chapter]

Wechat applet realizes five-star evaluation
![[tcapulusdb knowledge base] tcapulusdb doc acceptance - Introduction to creating game area](/img/b7/2358e8cf1cdaeaba77e52d04cc74d4.png)
[tcapulusdb knowledge base] tcapulusdb doc acceptance - Introduction to creating game area
![[tcapulusdb knowledge base] tcapulusdb operation and maintenance doc introduction](/img/04/b1194ca3340b23a4fb2091d1b2a44d.png)
[tcapulusdb knowledge base] tcapulusdb operation and maintenance doc introduction

动态规划【四】(计数类dp)例题:整数划分

数学知识——博弈论(巴什博奕、尼姆博奕、威佐夫博奕)思路及例题

AUTOCAD——三种修剪方式

Detailed explanation of interprocess communication
![[tcapulusdb knowledge base] tcapulusdb doc acceptance - transaction execution introduction](/img/d9/f6735906a130834c4b3e28de2b2617.png)
[tcapulusdb knowledge base] tcapulusdb doc acceptance - transaction execution introduction
随机推荐
[tcapulusdb knowledge base] Introduction to tcapulusdb system management
Jerry's adding timer interrupt [chapter]
最大路径和问题(摘樱桃问题)
Qstype implementation of self drawing interface project practice (I)
如何修改 node_modules 里的文件
Popular science of device review: popular science of innovative medical device series - sternum plate products
This privatized deployed enterprise knowledge base makes telecommuting a zero distance
居家办公被催之后才明白的时间管理
Leetcode 177 The nth highest salary (June 26, 2022)
i.mx6ull(单片机) c语言环境搭建
Take stock of some easy-to-use and niche markdown editors
"Internet +" contest topic hot docking | I figure to understand 38 propositions of Baidu
MapReduce原理剖析(深入源码)
【面试高频题】难度 1.5/5,LCS 模板题
In 2021, the global professional liability insurance revenue was about USD 44740million, and it is expected to reach USD 55980million in 2028. From 2022 to 2028, the CAGR was 3.5%
C/s architecture
Jerry's seamless looping [chapter]
[tcapulusdb knowledge base] tcapulusdb doc acceptance - transaction execution introduction
$15.8 billion! 2021 the world's top15 most profitable hedge fund giant
解开C语言的秘密《关键字》(第六期)