当前位置:网站首页>[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
、loop
After 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
、a
A 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+1
The 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
if
Conditional expressions in statements , There are many ways to express , There are different ways , But use the same . example :i!=0
It can be directly written as ai
, It can also have the same effect , And more concise. . because C Specified in the language , In numerical terms0
Express " false ", The rest are expressed as “ really ”. But notice 0 Different representations of , character'\0'
、NULL
All represent values 0, But the character zero'0'
, It does not mean numerical value0
, stay ASCII In code , Characters zero with numeric value48
Express . - 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
else
Only with the front lately Unmatched Ofif
pairing , one-on-one . Pay attention to three points :1、 stayelse
Before ;2、 leaveelse
lately ;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");
}
express
It should be an integer expression ,case
Selected value , Can only be constant , Be careful : Characters are also plastic , Andcase
The values of are mutually exclusive .- When the first one case After the value of successfully matches the expression , To perform its
case
The 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 ,
break
It saves ,case
There can be no statement after , Sentences can be saved ,default
The position of the statement is arbitrary . however , Ifdefault
Prior to case The value of matches , Will continue to execute backwardsdefault
What's in the sentence ,default
The statement of no longer has no value matching , Just executed . - For all
case
The statement in , There are two situations that will be implemented :1、case
The value of matches the expression ;2、 At presentcase
Previouscase
The value of the statement matches the expression , In the statement before execution Nonebreak
Jump 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
break
Jump out ofswitch
; - Such as
switch
stay Loop statement in , stayswitch
Mediumbreak
Only rightswitch
take effect , Can't jump out of the loop .
for Cycle default
- C In language , Altogether 3 A loop statement , among
while
Anddo……while
Must contain an expression , andfor
Empty 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 .
边栏推荐
- 26岁从财务转行软件测试,4年沉淀我已经是25k的测开工程师...
- Fedora/REHL 安装 semanage
- Office doc add in - Online CS
- Cif10 actual combat (resnet18)
- Missing monitoring: ZABBIX monitors the status of Eureka instance
- Pallet management in SAP SD delivery process
- What is the difference between int (1) and int (10)? Senior developers can't tell!
- 攻防世界 MISC中reverseMe简述
- leetcode35. 搜索插入位置(简单,找插入位置,不同写法)
- Supporting title of the book from 0 to 1: ctfer's growth road (Zhou Geng)
猜你喜欢
Visitor tweets about how you can layout the metauniverse
机器学习植物叶片识别
Introduction and underlying analysis of regular expressions
Reflex WMS medium level series 3: display shipped replaceable groups
What is the difference between int (1) and int (10)? Senior developers can't tell!
AI on the cloud makes earth science research easier
Market segmentation of supermarket customers based on purchase behavior data (RFM model)
Attributeerror: can 't get attribute' sppf 'on < module' models. Common 'from' / home / yolov5 / Models / comm
Chapter 7 - thread pool of shared model
After working for 10 years, I changed to a programmer. Now I'm 35 + years old and I'm not anxious
随机推荐
C语言_双创建、前插,尾插,遍历,删除
Bitcoinwin (BCW): 借贷平台Celsius隐瞒亏损3.5万枚ETH 或资不抵债
Leetcode daily question (1997. first day where you have been in all the rooms)
Attributeerror successfully resolved: can only use cat accessor with a ‘category‘ dtype
Practical guidance for interface automation testing (Part I): what preparations should be made for interface automation
基于PyTorch和Fast RCNN快速实现目标识别
[brush questions] how can we correctly meet the interview?
[daily question] 729 My schedule I
成功解决AttributeError: Can only use .cat accessor with a ‘category‘ dtype
Day 245/300 JS forEach 多层嵌套后数据无法更新到对象中
AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/home/yolov5/models/comm
Day 246/300 SSH connection prompt "remote host identification has changed!"
Arduino tutorial - Simon games
Attributeerror: can 't get attribute' sppf 'on < module' models. Common 'from' / home / yolov5 / Models / comm
[unity] how to export FBX in untiy
Oracle数据库11gr2使用tde透明数据加密报错ora28353,如果运行关闭wallet会报错ora28365,运行打开wallet就报错ora28353无法打开wallet
Fast target recognition based on pytorch and fast RCNN
Proteus -- Serial Communication parity flag mode
My creation anniversary
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