当前位置:网站首页>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
边栏推荐
- On the principle of concurrent programming and the art of notify / Park
- Identifier and type conversion
- Use the thread factory to set the thread name in the thread pool
- 什么是软件验收测试,第三方软件检测机构进行验收测试有什么好处?
- Let matlab2018b support the mex configuration of vs2019
- Xiaomi, which has set the highest sales record of domestic mobile phones in overseas markets, paid renewed attention to the domestic market
- The art of concurrent programming (6): explain the principle of reentrantlock in detail
- What if you encounter symbols you don't know in mathematical formulas
- The difference between lists, tuples, dictionaries and collections
- Who will answer the question?
猜你喜欢

Overview of knowledge map (II)

作为一名码农,女友比自己更能码是一种什么体验?
![[AI vision · quick review of robot papers today, issue 29] Mon, 14 Feb 2022](/img/a3/88b20f3e1be702f580169400e417f4.png)
[AI vision · quick review of robot papers today, issue 29] Mon, 14 Feb 2022

基于PHP的代步工具购物商城

Shopping mall for transportation tools based on PHP

Software testing process

Design and implementation of redis (4): what is the event driver of redis

MySQL is completely uninstalled and MySQL service is cleaned up

51 single chip microcomputer: D / a digital to analog conversion experiment

Design and implementation of redis (2): how to handle expired keys
随机推荐
Common exceptions
Three column layout (fixed width on both sides in the middle and fixed width on both sides in the middle)
Identifier, keyword, data type
[string] ranking of country names ----- problem solving notes
【NeurIPS 2019】Self-Supervised Deep Learning on Point Clouds by Reconstructing Space
RuntimeError: output with shape [4, 1, 512, 512] doesn‘t match the broadcast shape[4, 4, 512, 512]
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
What if you encounter symbols you don't know in mathematical formulas
The art of concurrent programming (2): synchronized usage scenarios
Xiaomi, qui a établi le plus grand volume de ventes de téléphones portables domestiques sur le marché d'outre - mer, se concentre de nouveau sur le marché intérieur
【NeurIPS 2019】Self-Supervised Deep Learning on Point Clouds by Reconstructing Space
创下国产手机在海外市场销量最高纪录的小米,重新关注国内市场
基于PHP的代步工具购物商城
The difference between lists, tuples, dictionaries and collections
Xiaomi, which has set the highest sales record of domestic mobile phones in overseas markets, paid renewed attention to the domestic market
Openvino only supports Intel CPUs of generation 6 and above
【ICCV 2019】MAP-VAE:Multi-Angle Point Cloud-VAE: Unsupervised Feature Learning for 3D Point Clouds..
Paddlepaddle model to onnx
The super large image labels in remote sensing data set are cut into specified sizes and saved into coco data set - target detection
What to pay attention to when writing the first code