当前位置:网站首页>[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
边栏推荐
- A solution for PHP to implement image login verification code
- Towards End-to-End Lane Detection: an Instance SegmentationApproach
- Is the individual industrial and commercial door a legal person enterprise
- RTMP streaming +rtmp playback low delay solution in unity environment
- 【长时间序列预测】Aotoformer 代码详解之[4]自相关机制
- 43. Number of occurrences of 1 in 1 ~ n integers
- Why can't NAND flash be used as RAM while nor flash can
- March 4, 2021
- yolov5
- Beginning is an excellent emlog theme v3.1, which supports emlog Pro
猜你喜欢

CCF noi2022 quota allocation scheme

March 4, 2021

利用jieba库进行词频统计

Save the object in redis, save the bean in redis hash, and attach the bean map interoperation tool class

Tabulation skills and matrix processing skills

Automated test - dark horse headline test project

What is the difference between ArrayList and LinkedList?

38. appearance series
![[fastapi] use pycharm to configure and use environment variables for fastapi projects](/img/a5/47cabfed3f12bf70b4b047ef29fc9d.jpg)
[fastapi] use pycharm to configure and use environment variables for fastapi projects

Redis persistence
随机推荐
Optipng can optimize the compressed PNG picture file format
Why can't NAND flash be used as RAM while nor flash can
Automated test - dark horse headline test project
DMA RDMA technology details
Special materials | household appliances, white electricity, kitchen electricity
Kubernetes certificate online update
log4j 1. X upgrade to 2 Solution of X dependency incompatibility
14- II. Cutting rope II
[speech] how to customize ring back tone according to different countries
Word frequency statistics using Jieba database
Deep understanding of asynchronous programming
59 - I. maximum value of sliding window
20. string representing numeric value
GRE protocol details
Date ()
67. convert string to integer
merge sort
flex/fixed上中下(移动端)
[long time series prediction] the [4] autocorrelation mechanism of aotoformer code explanation
[machine learning] first day of introduction