当前位置:网站首页>C语言 字符常量
C语言 字符常量
2022-04-23 03:51:00 【飘飘飄飘】
在做C语言词法分析器的时候遇到了字符常量识别的问题。在这里讨论一下一般字符常量的词法规则,不设计宽字符和通用字符。
字符常量
C语言字符常量由单引号包含的一个或多个字符组成,包含多个字符时具体值与编译器有关(比如VS2019的编译器是获取最后一个字符)。 如:‘a’ ‘XY’ ‘0’ ‘*’
所有源代码字符集中的字符都可以成为字符常量,除了单引号、反斜线和换行符,这三个字符必须是用转义符表示:‘\’’ ‘\’ ‘\n’ 。在编译阶段,字符常量和字符串字面量中的字符和转义序列会转换成运行字符集中对应的字符。
字符常量的类型和值
对于所有非宽字符字符常量,类型为int,即C语言中字符常量和整型没有什么区别,可以混用。如下的代码都是可以正常运行的。
int a = 'P';char c = 80;
需注意的是,若采用带符号的char类型,那么字符常量的值可为负,如下:
int c = '\xA3';
printf("%c %d\r\n",c,c);
结果为£ -93 可以采用unsigned char这样值均为正数
转义符
转义序列 | 字符值 | 在输出设备上的动作 |
---|---|---|
\’ | 单引号 | 输出字符 |
\" | 双引号 | 输出字符 |
\? | 问号 | 输出字符 |
\\ | 反斜线 | 输出字符 |
\a | 警告 | 产生声音或者视觉信号 ,windows报错声音 |
\b | 退格 | 前移一个字符 |
\f | 换页 | 移动到下一页的开端 |
\n | 换行 | 移动到下一行开端 |
\r | 回车 | 移动到当前行开始 |
\t | 水平制表符 | 移动到下一个垂直的制表对齐点 |
\v | 垂直制表符 | 移动到下一个垂直的制表对齐点 |
\o,\oo,\ooo o为八进制字符 |
此八进制对应字符 | 输出字符 |
\xh,\xhh h为十六进制字符 |
次十六进制对应字符 | 输出字符 |
参考了C语言规范 第二版
版权声明
本文为[飘飘飄飘]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_53191711/article/details/124353334
边栏推荐
- Three types of jump statements
- 网络原理 | TCP/IP中的连接管理机制 重要协议与核心机制
- Identifier and type conversion
- Common net HP UNIX system FTP server listfiles returns null solution.
- Raspberry pie 3B logs into the wired end of Ruijie campus network through mentohust, creates WiFi (open hotspot) for other devices, and realizes self startup at the same time
- [AI vision · quick review of robot papers today, issue 28] wed, 1 Dec 2021
- A function second kill 2sum 3sum 4sum problem
- Leetcode punch in diary day 01
- UDP协议与TCP协议
- Idea debug debugging tutorial
猜你喜欢
【ICCV 2019】MAP-VAE:Multi-Angle Point Cloud-VAE: Unsupervised Feature Learning for 3D Point Clouds..
Identifier, keyword, data type
Win10 boot VMware virtual machine boot seconds blue screen problem perfect solution
基于PHP的代步工具购物商城
Photoshop installation under win10
AI CC 2019 installation tutorial under win10 (super detailed - small white version)
Wechat applet cloud database value assignment to array error
ROS series (IV): ROS communication mechanism series (1): topic communication
MySQL zip installation tutorial
Download and configuration of idea
随机推荐
VS Studio 修改C語言scanf等報錯
Common auxiliary classes
CRF based medical entity recognition baseline
Identificateur, mot - clé, type de données
【ICCV 2019】MAP-VAE:Multi-Angle Point Cloud-VAE: Unsupervised Feature Learning for 3D Point Clouds..
UDP协议与TCP协议
[Li Hongyi 2022 machine learning spring] hw6_ Gan (don't understand...)
ROS series (II): ROS quick experience, taking HelloWorld program as an example
vscode删除卸载残余
A function second kill 2sum 3sum 4sum problem
[AI vision · quick review of robot papers today, issue 29] Mon, 14 Feb 2022
Three types of cyclic structure
伦敦银最新价格走势图与买卖点
Man's life
2021-09-03 crawler template (only static pages are supported)
一个函数秒杀2Sum 3Sum 4Sum问题
[mathematical modeling] my mathematical memory
[AI vision · quick review of NLP natural language processing papers today, issue 29] Mon, 14 Feb 2022
【李宏毅2022 机器学习春】hw6_GAN(不懂..)
Vscode delete uninstall residue