当前位置:网站首页>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
边栏推荐
- 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
- 57. The core principle of flutter - layout process
- Xuri 3sdb, installing the original ROS
- Peak store app imitation station development play mode explanation source code sharing
- [tcapulusdb knowledge base] tcapulusdb operation and maintenance doc introduction
- Wait, how do I use setmemorylimit?
- C # WPF realizes undo redo function
- 等等, 怎么使用 SetMemoryLimit?
- Summary of qstype class usage (III)
- C/s architecture
猜你喜欢

巅峰小店APP仿站开发玩法模式讲解源码分享

Drive to APasS!使用明道云管理F1赛事

面试突击60:什么情况会导致 MySQL 索引失效?

Rxjs mergeMap 的使用场合

pull request

57. The core principle of flutter - layout process
![Jerry's DAC output mode setting [chapter]](/img/2e/62bc74e216ed941bd2a0a191db63f0.png)
Jerry's DAC output mode setting [chapter]

StarCraft's Bug King ia retired for 2 years to engage in AI, and lamented that it was inferior

等等, 怎么使用 SetMemoryLimit?

Excel中输入整数却总是显示小数,如何调整?
随机推荐
Interviewer: with the for loop, why do you need foreach?
[tcapulusdb knowledge base] Introduction to tmonitor system upgrade
R语言使用epiDisplay包的poisgof函数对泊松回归(Poisson Regression)执行拟合优度检验、检验是否存在过度离散问题(overdispersion)
[tcapulusdb knowledge base] tcapulusdb doc acceptance - transaction execution introduction
Shell script learning notes
After Jerry's sleep, the regular wake-up system continues to run without resetting [chapter]
56. Core principle of flutter - flutter startup process and rendering pipeline
Build the Internet of things system from scratch
C语言0长度数组的妙用
Four memory areas (stack, heap, global, code area)
如何修改 node_modules 裏的文件
MIT6.031 软件构造 Reading7阅读笔记Designing Specifications(设计规范)
数学知识——博弈论(巴什博奕、尼姆博奕、威佐夫博奕)思路及例题
Dynamic programming [4] (counting class DP) example: integer partition
namespace ‘rlang’ 0.2.0 is being loaded, but &gt;= 0.3.0 is required
The wonderful use of 0 length array in C language
R language uses GLM function to build Poisson logarithm linear regression model, processes three-dimensional contingency table data to build saturation model, uses step function to realize stepwise re
Unity Shader学习(一)认识unity shader基本结构
Summary of qstype class usage (II)
[tcapulusdb knowledge base] tcapulusdb operation and maintenance doc introduction