当前位置:网站首页>[C language basics] macro definition usage
[C language basics] macro definition usage
2022-06-12 05:42:00 【Amos-Chen】
| Macro definition form and use |
Macro definition format
#define your_macro_name the_replacement about #define The name of your_macro_name, It is named in the same way as the variable name , replace text the_replacement It could be any string .
Usually define Instructions take only one line , If the replaced text is long , It can be a backslash \ Line break .
Define constants
#define e 2.71828Any text
#define forever for( ; ; )Use macros forever
int main() {
forever{
printf(1);
}
return 1;
}
Macro definition with parameters
#define max(a,b) ((a)>(b)?(a):(b))Use macros max
int main() {
int x = max(1, 2);
printf("%d",x);
return 1;
}
An argument is an argument character
Formal parameters cannot be replaced by quoted strings , But if you want to pass in an argument, it is the corresponding argument character ( That is, the arguments are automatically quoted ) Well ?
For example, an argument is passed in amos( No quotation marks ), What I actually want to use is "amos"
#define dprintf("hello " #str)Use dprint
int main() {
dprint(amos);
return 1;
}
When calling this macro , This macro is extended to :
printf( "hello " "amos") Equivalent to
printf( "hello amos") Join arguments
#define paste(name) char_##nameint main() {
char paste(a);
char paste(b);
// char_a char_b There is no need to define , The definition has been completed in the macro
char_a = 'x';
char_b = 'y';
printf("%c%c\n", char_a, char_b) ;
return 1;
}
Multiline macro replacement
#define loop(a, b) for(int i=0;i<(b);++i)\
{
\
(a)+=i;\
}\loop Use of macros :
int main() {
int a = 100, b = 100;
loop(a, b);
printf("%d\n", a);
return 1;
}
Print as 5050
| matters needing attention |
Add parentheses to each shape as much as possible
#define square(x) x*x
int main() {
printf("%d\n", square(1 + 5));
return 1;
}
The above code , It feels like it should be printed 36, Not really
Because the macro is replaced with :
printf("%d\n", 1+5*1+5) The result is 11, So add brackets as much as possible .
Cancel macro definition
#define square(x) x*x
#undef square
边栏推荐
- March 23, 2021
- Performance test - GTI application service performance monitoring platform
- [daily question on niuke.com] two point search
- Lldp protocol
- How much Ma is the driving current of SIM card signal? Is it adjustable?
- Introduction to sringmvc
- XML参数架构,同一MTK SW版本兼容两套不同的音频参数
- C language - how to define arrays
- Select gb28181, RTSP or RTMP for data push?
- Towards End-to-End Lane Detection: an Instance SegmentationApproach
猜你喜欢

深入理解异步编程

WebRTC AEC 流程解析

Is the individual industrial and commercial door a legal person enterprise

Webrtc AEC process analysis

How Wireshark decrypts WiFi data packets

CCF noi2022 quota allocation scheme

Beginning is an excellent emlog theme v3.1, which supports emlog Pro

yolov5

16. Somme des trois plus proches

Legal liabilities to be borne by the person in charge of the branch
随机推荐
Selenium crawler automatically captures TOEFL test position of NEEA website
29. print matrix clockwise
Is the individual industrial and commercial door a legal person enterprise
什么是工程预付款
WiFi protocol and ieee905 protocol learning details
Codis 3. X expansion and contraction
March 23, 2021
Beginning is an excellent emlog theme v3.1, which supports emlog Pro
FPGA语法的细节
16. sum of the nearest three numbers
Tabulation skills and matrix processing skills
Please remove any half-completed changes then run repair to fix the schema history
IO stream introduction
[grpc development] go language builds simple server and client
Word frequency statistics using Jieba database
C language - how to define arrays
GRP development: four communication modes of GRP
国企为什么要上市
【长时间序列预测】Aotoformer 代码详解之[4]自相关机制
Towards End-to-End Lane Detection: an Instance SegmentationApproach