当前位置:网站首页>[some special grammars about C]
[some special grammars about C]
2022-07-06 06:57:00 【A thousand years in a dream】
List of articles
About C Some special grammars of
Sentence block
- All statements written in a brace , It is called a statement block . Usually used in
if、loopAfter the statement , When there is only one statement after , It saves . - special , It can be written almost anywhere , But the scope of action is limited , It only takes effect in the statement block :
int a = 1;
{
int a = 2;
printf("%d\n", a);
}
printf("%d\n", a);
Such as removing braces , Will compile errors .
int a = 1;
int a = 2;
printf("%d\n", a);
printf("%d\n", a);
printf And Ternary expression In combination with
For some usual needs , Want to judge according to different results , Output different data , So you may have the following code :
if(i>0)
printf("%d Greater than 0",i);
else
printf("%d Less than or equal to 0",i);
For just one function , It's too cumbersome , actual printf The following syntax is supported , Using ternary expressions , The code can be much simpler .
printf("%d", 1 > 0 ? 1 : 0);
printf(1 < 0 ? "%d" : "%c", 65);
printf Execution order of
int a = 1;
printf("%d,%d,%d\n", a, a + 1, a++);
- If you press from left to right , The output should be :
1,2,1 - But the actual output :
2,3,1
int a = 1;
printf("%d,%d,%d\n", a = a + 1, a + 1, a);
- It comes from right to left output , Guess that the output is :
2,2,1 - But the reality is :
2,2,2 - reason : For variables a , It should be after all expressions are executed , Print together come out . Instead of printing one by one .
- So for
a=a+1、aA similar expression , Will be printed with the same value , Its value is after executing all expressions from right to left , Variable a Value . - And for something like
a+1The expression of , Variables are not printed a Value , It is not assigned to a Variable , It can only be printed as a constant .
printf The return value of
Take a chestnut :
printf("%d-%d-%d-%d-%d", printf("1"),printf("22"),printf("333"),printf("4444"),printf("55555"));
- Output results :
5555544443332211-2-3-4-5 - According to the output , Also proved that printf The function is executed from right to left , First output
555554444333221, Output again printf The return value of , Namely1-2-3-4-5, according to printf Parameters in function , It's easy to find rules and find printf The return value of : The number of printed characters .
if Condition judgment in
- about
ifConditional expressions in statements , There are many ways to express , There are different ways , But use the same . example :i!=0It can be directly written as ai, It can also have the same effect , And more concise. . because C Specified in the language , In numerical terms0Express " false ", The rest are expressed as “ really ”. But notice 0 Different representations of , character'\0'、NULLAll represent values 0, But the character zero'0', It does not mean numerical value0, stay ASCII In code , Characters zero with numeric value48Express . - So the following expressions are not executed
if(0)
printf("0");
if('\0')
printf("\'\\0\'");
if(NULL)
printf("NULL");
if And else The problem of pairing
elseOnly with the front lately Unmatched Ofifpairing , one-on-one . Pay attention to three points :1、 stayelseBefore ;2、 leaveelselately ;3、 Unpairedif.- It has nothing to do with indentation !!! This is not Python.
switch Medium break
- Basic grammar : General form
switch(express) {
case 1:
printf("1");
break;
case 2:
printf("2");
break;
case 3:
printf("3");
break;
default
printf("default");
}
expressIt should be an integer expression ,caseSelected value , Can only be constant , Be careful : Characters are also plastic , AndcaseThe values of are mutually exclusive .- When the first one case After the value of successfully matches the expression , To perform its
caseThe following sentence , Again by break Jump out of , Do not execute the following statement .
switch (express){
case 1:
case 2:
printf("2");
default:
printf("default");
case 3:
printf("3");
break;
}
- More special ,
breakIt saves ,caseThere can be no statement after , Sentences can be saved ,defaultThe position of the statement is arbitrary . however , IfdefaultPrior to case The value of matches , Will continue to execute backwardsdefaultWhat's in the sentence ,defaultThe statement of no longer has no value matching , Just executed . - For all
caseThe statement in , There are two situations that will be implemented :1、caseThe value of matches the expression ;2、 At presentcasePreviouscaseThe value of the statement matches the expression , In the statement before execution NonebreakJump out ofswitch; - about default There are also two situations in which statements will be executed :1、 be-all case Do not match the value of the expression ;2、 Previous case The value of the statement matches the expression , In the statement before execution None
breakJump out ofswitch; - Such as
switchstay Loop statement in , stayswitchMediumbreakOnly rightswitchtake effect , Can't jump out of the loop .
for Cycle default
- C In language , Altogether 3 A loop statement , among
whileAnddo……whileMust contain an expression , andforEmpty can be saved in the loop . however for Semicolons in a loop cannot be omitted ! - Infinite loop writing :
for(;;)
printf("for If the expression in is not written, it defaults to ' really '");
- In almost all cases , The three loops can be used interchangeably . According to the actual needs, generally choose a more concise one .
define Macro definition
- define Just a simple character replacement , Pay special attention to the difference between brackets .
- Can be nested , With parameters , It can be a statement .
边栏推荐
- Missing monitoring: ZABBIX monitors the status of Eureka instance
- BIO模型实现多人聊天
- 《从0到1:CTFer成长之路》书籍配套题目(周更)
- 这个高颜值的开源第三方网易云音乐播放器你值得拥有
- SSO process analysis
- WPF之MVVM
- 【Hot100】739. 每日温度
- At the age of 26, I changed my career from finance to software testing. After four years of precipitation, I have been a 25K Test Development Engineer
- RichView TRVStyle 模板样式的设置与使用
- Simple use of JWT
猜你喜欢

Leetcode daily question (971. flip binary tree to match preorder traversal)

Monotonic stack

C language_ Double create, pre insert, post insert, traverse, delete

开源的网易云音乐API项目都是怎么实现的?

Hydra common commands

The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower

Fedora/REHL 安装 semanage

顶测分享:想转行,这些问题一定要考虑清楚!

Do you really know the use of idea?

leetcode1020. 飞地的数量(中等)
随机推荐
[daily question] 729 My schedule I
雲上有AI,讓地球科學研究更省力
Fedora/rehl installation semanage
leetcode6109. 知道秘密的人数(中等,周赛)
Apache dolphin scheduler source code analysis (super detailed)
开源的网易云音乐API项目都是怎么实现的?
Call, apply, bind rewrite, easy to understand with comments
【刷题】怎么样才能正确的迎接面试?
UWA pipeline version 2.2.1 update instructions
【Hot100】739. Daily temperature
Simple query cost estimation
接口自动化测试框架:Pytest+Allure+Excel
LeetCode Algorithm 2181. 合并零之间的节点
MySQL high frequency interview 20 questions, necessary (important)
详解SQL中Groupings Sets 语句的功能和底层实现逻辑
万丈高楼平地起,每个API皆根基
ROS2安装及基础知识介绍
Facebook AI & Oxford proposed a video transformer with "track attention" to perform SOTA in video action recognition tasks
Latex文字加颜色的三种办法
A method to measure the similarity of time series: from Euclidean distance to DTW and its variants