当前位置:网站首页>接续符介绍及用法和++和--操作符分析介绍及用法
接续符介绍及用法和++和--操作符分析介绍及用法
2022-04-22 21:53:00 【不停歇向前^-^蜗牛】
1 接续符的意义
C语言中的接续符()是指示编译器行为的利器。
如下的代码是合法的么?
#in\ clud\ e <st\ dio.h>
in\
t m\
ain(\
)
{
pri\
ntf\
(\
"Hello D.T.\n"
)\
;
ret\
urn 0;
}
预处理之后的结果如下:
int
main()
{
printf
(
"Hello D.T.\n"
)
;
return
0;
}
2 接续符的作用
编译器会将反斜杠剔除,跟在反斜杠后面的字符自动接续到前一行。
在接续单次时,反斜杠之后不能有空格,反斜杠的下一行之前也不能有空格。
接续符适合在定义宏代码块时使用。
接续符的应用
#include <stdio.h>
#define SWAP(a,b) \ {
\ int temp = a; \ a = b; \ b = temp; \ }
int main()
{
int a = 1;
int b = 2;
int c = 3;
SWAP(a,b);
printf("a = %d, b = %d\n", a, b);
SWAP(b,c);
printf("b = %d, c = %d\n", b, c);
return 0;
}
接续符实验
//接续符测试
//接续符后面无空格同时下一行开始时也无空格
hello\
lemon
//接续符后面无空格同时下一行开始时有空格
hello\
lemon
//接续符后面有空格同时下一行开始时无空格
hello\
lemon
//接续符后面有空格同时下一行开始时有空格
hello\
lemon
//测试宏中的接续符(包含代码块)
#define SWAP(a, b) \ {
\ int tmp; \ tmp = a; \ a = b; \ b = tmp; \ }
SWAP(a, b);
//测试普通代码块中含有接续符
{
\
int tmp; \
tmp = a; \
a = b; \
b = tmp; \
}
预处理之后的结果:
hellolemon
hello
lemon
warning: backslash and newline separated by space
hello\
^
hellolemon
warning: backslash and newline separated by space
hello\
^
hello
lemon
# 27 "test.c"
{
int tmp; tmp = a; a = b; b = tmp; };
{
int tmp;
tmp = a;
a = b;
b = tmp;
}
/* 结论:当接续符后面或者下一行有空格时(接续符后面有空格时,会有警告信息),编译器并不会将下一行接续过来。 另外编译器对于宏代码块和普通代码块的接续是不一样的。宏里接续符的下一行有空格能成功接续,而其他则不行。 */
3 小结
C语言中的反斜杠()同时具有接续符和转义字符的作用:
作为接续符使用是可以直接出现在程序中;
作为转义字符使用时需出现在单引号或双引号之间。
1 ++,- -操作符的本质
++和- -操作对应两条汇编指令。
前置
变量自增(减)1
取变量值
后置
取变量值
变量自增(减)1
2 ++,- -操作符使用分析
一对令人头疼的兄弟:
int i = 0;
(i++) + (i++) + (i++);
(++i) + (++i) + (++i);
你觉得这两个表达式的值分别是多少?
#include <stdio.h>
int main()
{
int i = 0;
int r = 0;
r = (i++) + (i++) + (i++);
printf("i = %d\n", i);
printf("r = %d\n", r);
r = (++i) + (++i) + (++i);
printf("i = %d\n", i);
printf("r = %d\n", r);
return 0;
}
vc编译器中:
i= 3,r = 0, i = 6, r = 18。
r = i + i + i, i += 1,i += 1,i += 1。
i += 1,i += 1,i += 1,r = i + i + i。
gcc编译器中:
i= 3,r = 0, i = 6, r = 16。
r = i + i + i, i += 1,i += 1,i += 1。
i += 1,i += 1,r = Ii + i, i += 1, r += i。
java编译环境中:
i= 3,r = 3, i = 6, r = 15。
3 ++、- -注意事项
C语言只规定了++和- -对应指令的相对执行次序(意思是先取值还是先自增,并没有规定两条汇编指令必须连续执行)。
++和- - 对应的汇编指令不一定连续运行。
在混合运算中,++和- -的汇编指令可能被打断执行(如gcc编译器)。
++和- - 参与混合运算结果是不确定的。
4 笔试中的“奇葩”题
++i+++i+++i。
a+++b a++ + b ? a + ++b
编译器究竟如何解释?
贪心法:++,- -表达式的阅读技巧
编译器处理的每个符号应该尽可能多的包含字符。
编译器以从左向右的顺序一个一个尽可能多的读入字符。
当读入的字符不可能和已读入的字符组成合法字符为止。
实例分析:贪心阅读示例
#include <stdio.h>
int main()
{
int i = 0;
int j = ++i+++i+++i; // ++i++ ==>1++ (error)
int a = 1;
int b = 4;
int c = a+++b; //a+++b; 1 + 4 ==> 5 a ==> 2
int* p = &a;
b = b/*p; //此处会被当成注释 printf("i = %d\n", i); printf("j = %d\n", j); printf("a = %d\n", a); printf("b = %d\n", b); printf("c = %d\n", c); return 0; }
注意:
空格可以作为C语言中一个完整符号的休止符。
编译器读入空格后立即对之前读入的符号进行处理。
5 小结
++和- -操作符在混合运算中的行为可能不同。
编译器通过贪心算法处理表达式中的子表达式。
空格可以作为C语言中一个完整符号的休止符。
编译器读入空格后立即对之前读入的符号进行处理。
版权声明
本文为[不停歇向前^-^蜗牛]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_48321071/article/details/124323572
边栏推荐
- 01 knapsack problem
- SMB+MSSQL
- Tan Xiang, CEO of Kechuang · Pera software: the essence of zero trust is digital security. To B should also deeply study the user's mind
- TCP/IP 协议及网络分层模型
- Why is bi so important to enterprises?
- Function pointer and callback function
- 一夜爆红的Moonbirds NFT,究竟有何魔力?
- 为什么BI对企业这么重要?
- Basic practice of C language (001-1)
- 7. 堪比JMeter的.Net压测工具 - Crank 总结篇 - crank带来了什么
猜你喜欢

加法逆元(a^a=0)异或操作,这个并没有性能上的优势,只是一个智力游戏

Sign up to open QKE container engine hosting version and container ecological Conference!

Secret of 66% performance surge: AMD 25000 yuan 768mb 3D cache Xiaolong opened the cover for the first time

Tcp/ip protocol and network layered model

Crashsight general function & feature function introduction

Want to day browser: recommend several easy-to-use desktop browsers

Typical application scenarios of alicloud log service SLS

vickers威格士比例阀的特点

【MMUB】基于Hidden Markov model的手机用户行为建模——Hidden Markov model

Youqilin 22.04 lts version is officially released | ukui 3.1 opens a new experience!
随机推荐
强化学习(实践):REINFORCE,AC
LeetCode-238-除自身以外数组的乘积
百思买Best Buy 网站EDI 测试流程
Secret of 66% performance surge: AMD 25000 yuan 768mb 3D cache Xiaolong opened the cover for the first time
Node error reporting record: cannot find module are we there yet \ index js
In the era of Internet of all things, how should smart home ecosystems such as Xiaomi, apple and Huawei be selected?
How should enterprises make disaster recovery plans for Cloud Computing?
io_uring技术在分布式云原生数据库中的应用
2.58-编写程序is-little-endian,当在小端法机器上编译和运行时返回1,在大端法机器上编译和运行时则返回0。这个程序应该可以运行在任何机器上,无论机器的字长是多少。
都是做全屋智能的,Aqara和HomeKit到底有什么不同?
Transport layer - connectionless transport: UDP (2)
一夜爆红的Moonbirds NFT,究竟有何魔力?
RPCX源码学习-client端
4 / 21 Kunming supplementary questions + mathematics
论文笔记: BRITS: Bidirectional Recurrent Imputation for Time Series
IPO of China first science and Technology Shenzhen Stock Exchange: another listed enterprise was born in Hubei with a market value of 8.3 billion
hawe哈威液压泵站的液压冲击分析
js 截取文件后缀名
[*CTF2022]oh_ my_ lotto_ revenge
Leetcode 04 Median of Two Sorted Arrays