当前位置:网站首页>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
边栏推荐
- Counting and sorting (C language implementation) -- learning notes
- Software testing process
- Does China Mobile earn 285 million a day? In fact, 5g is difficult to bring more profits, so where is the money?
- SQL learning record
- 列表、元组、字典和集合的区别
- 对象和类的概念
- Application and definition of interface
- Installation and configuration of MinGW under win10
- ROS series (II): ROS quick experience, taking HelloWorld program as an example
- 使用大华设备开发行AI人流量统计出现时间不正确的原因分析
猜你喜欢

Openvino only supports Intel CPUs of generation 6 and above

Wechat applet cloud database value assignment to array error

創下國產手機在海外市場銷量最高紀錄的小米,重新關注國內市場

Solve the technical problems in seq2seq + attention machine translation

Retrieval question answering system baseline

(valid for personal testing) compilation guide of paddedetection on Jetson

Use of rotary selector wheelpicker

Common net HP UNIX system FTP server listfiles returns null solution.
![[AI vision · quick review of NLP natural language processing papers today, issue 30] Thu, 14 APR 2022](/img/cc/21b20f56a7eb3e43b9c23792c597ef.png)
[AI vision · quick review of NLP natural language processing papers today, issue 30] Thu, 14 APR 2022

Create virtual machine
随机推荐
Summary of knowledge map (3)
深度學習筆記(二)——激活函數原理與實現
CRF based medical entity recognition baseline
51 single chip microcomputer: D / a digital to analog conversion experiment
Common auxiliary classes
If statement format flow
What to pay attention to when writing the first code
Identifier, keyword, data type
2021-09-03 crawler template (only static pages are supported)
ROS series (IV): ROS communication mechanism series (5): Service Communication Practice
Websites frequented by old programmers (continuously updated)
Laboratory safety examination
Using VBA interval to extract one column from another in Excel
[mathematical modeling] my mathematical memory
QT program integration easyplayer RTSP streaming media player screen flicker what is the reason?
Variables, constants, operators
Design and implementation of redis (1): understand data structures and objects
将编译安装的mysql加入PATH环境变量
A hundred dollars for a hundred chickens
Vscode download and installation + running C language