注:博客中内容主要来自《狄泰软件学院》,博客仅当私人笔记使用。
测试环境:Ubuntu 10.10
GCC版本:4.4.5
一、单引号和双引号
1)C语言中的单引号用来表示字符字面量
2)C语言中的双引号用来表示字符串字面量
‘a'表示字符字面量
在内存中占1个字节
’a'+1表示'a'的ASCII码加1,结果为'b'
扫描二维码关注公众号,回复:
8829173 查看本文章
"a"表示字符串字面量
在内存中占2个字节
“a"+1表示指针运算,结果指向”a"结束符'\0'
3)下面的程序片段合法吗?
char* p1 = 1; //error
char* p2 = '1'; //error
char* p3 = "1";
实例分析
单引号和双引号的本质
14-1.c
#include <stdio.h>
int main()
{
char* p1 = 1 ; //访问会产生段错误
char* p2 = '1';
char* p3 = "1";
printf("%s, %s, %s", p1, p2, p3);
printf('\n');
printf("\n");
return 0;
}
操作:
1) gcc 14-1.c -o 14-1.out编译有警告:
14-1.c: In function ‘main’:
14-1.c:5:13: warning: initialization makes pointer from integer without a cast [enabled by default]
char* p1 = 1;
^
警告:用整型数初始化指针
14-1.c:6:13: warning: initialization makes pointer from integer without a cast [enabled by default]
char* p2 = '1';
^
14-1.c:11:2: warning: passing argument 1 of ‘printf’ makes pointer from integer without a cast [enabled by default]
printf('\n');
^
In file included from 14-1.c:1:0:
/usr/include/stdio.h:362:12: note: expected ‘const char * __restrict__’ but argument is of type ‘int’
extern int printf (const char *__restrict __format, ...);
^
14-1.c:11:2: warning: format not a string literal and no format arguments [-Wformat-security]
printf('\n');
^
运行出现段错误。
Segmentation fault (core dumped)
二、小贴士
1)字符字面量被编译为对应的ASCII码
2)字符串字面量被编译为对应的内存地址
3)printf的第一个参数被当成字符串内存地址
4)内存的低地址空间不能在程序中随意访问
程序实例分析1
p1指向0x00000001
p2指向0x00000031
p3指向0x080484d0
注:访问低于0x08048000地址时,会产生段错误。
程序实例分析2
'\n'指向0x00000010,访问时会产生段错误。
"\n"是合法显示。
实例分析
混淆概念的代码
14-2.c
#include <stdio.h>
int main()
{
char c = " ";
while( (c == "\t") || (c == " ") || (c == "\n") ) //键:tab、空格、回车
{
scanf("%c", &c);
}
return 0;
}
编译有警告,运行没有任何显示,直接结束(判断都是错误!)
这里用了双引号,变成地址占用4字节,但是char只占用1个字节,
后边的比较肯定会发生错误(1字节,最大255,肯定小于4字节)
修改:
""都改为''
二、程序实例分析
char c = "string";
发生了什么?
分析:
1)编译后字符串”string“的内存地址被赋值给变量c
2)内存地址占用4个字节,而变量C只占用1个字节
3)由于类型不同,赋值后产生截断
小结:
1)单引号括起来的单个字符代表整数
2)双引号括起来的字符代表字符指针
3)C编译器接受字符和字符串的比较,无任何意义
4)C编译器允许字符串对字符变量赋值,只能得到错误