当前位置:网站首页>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
边栏推荐
- How Zotero quotes in word jump to references / hyperlink
- Jupiter notebook modify configuration file setting startup directory is invalid
- Design and implementation of redis (5): master-slave replication strategy and optimization
- Process seven state transition diagram
- Instructions for fastmock
- Application and definition of interface
- 【李宏毅2022 机器学习春】hw6_GAN(不懂..)
- Add the compiled and installed Mysql to the path environment variable
- Installation and configuration of clion under win10
- ROS series (IV): ROS communication mechanism series (5): Service Communication Practice
猜你喜欢
Process seven state transition diagram
Digital image processing third edition Gonzalez notes Chapter 2
STM32 advanced timer com event
[latex] formula group
Design and implementation of redis (3): persistence strategy RDB, AOF
Use of rotary selector wheelpicker
[latex] differences in the way scores are written
[AI vision · quick review of NLP natural language processing papers today, issue 28] wed, 1 Dec 2021
Source code and update details of new instance segmentation network panet (path aggregation network for instance segmentation)
Software testing process
随机推荐
Process seven state transition diagram
UDP协议与TCP协议
Xshell、Xftp连接新创建的Unbutu系统虚拟机全流程
使用大华设备开发行AI人流量统计出现时间不正确的原因分析
On the principle of concurrent programming and the art of notify / Park
Leetcode 617 merge binary tree
Common exceptions
一个函数秒杀2Sum 3Sum 4Sum问题
The whole process of connecting the newly created unbutu system virtual machine with xshell and xftp
What if you encounter symbols you don't know in mathematical formulas
Three types of jump statements
[AI vision · quick review of robot papers today, issue 29] Mon, 14 Feb 2022
Photoshop installation under win10
What is software acceptance testing? What are the benefits of acceptance testing conducted by third-party software testing institutions?
Commonly used classes
Installation and configuration of MinGW under win10
Variables, constants, operators
VS Studio 修改C语言scanf等报错
Jupiter notebook modify configuration file setting startup directory is invalid
A hundred dollars for a hundred chickens