Summary
single quotation mark in C language is used to represent the character literal of
; the character literal
is essentially an integer;
2) C language double quotation marks to indicate
string literal; string literals
is essentially a pointer (memory address);
3) 0x08048000
segmentation fault (the accessible address value needs to be greater than or equal to this value, 32-bit system);
4) Note: char type and
char* type should be
with correct literal for 1614f175290305; although char a = ""; char* s ='a'; can be compiled, but it will be out when used Unexpected
.
5) The confusion between character literals and string literals can be edited in the C compiler, but you should pay attention to . In the 1614f17529031b project, all warnings must be treated as errors.
Single and double quotes
C language single quotes used to represent
character literals;
nature character literal is an integer, 'a' is 1 byte in memory, 'a' + 1 denotes 'a' ASCII value Add 1, and the result is'b'.
double quotation marks in the C language are used to represent the
string literal; the essence of the string literal is a memory address (address value), "a" occupies 2 bytes in the memory, and "a"+1 represents a pointer Operation, the result points to the terminator'\0' in "a".
Code reading:
char* p1 = 1; char* p2 = '1'; char* p3 = "1"; printf("%s, %s, %s", p1, p2, p3); printf('\n'); printf("\n");
Code analysis:
p1 用一个整数字面量1进行初始化,有warning,但可以编的过,因为指针本质也是一个32(64)位的整数值 p2 用一个字符字面量'1'进行初始化,同p1。字符'1'本质也是一个整数,值为49 p3 用一个字符串字面量"1"进行初始化,字符串字面量的本质是一个指针(内存地址),没有问题 printf(param1, ...)的第一个参数是一个指针,地址值 printf('\n')可以编的过,但是warning,同p2,使用10作为一个地址值传给printf函数 printf("\n")可以编的过,使用一个字符串字面量作为参数,换行 综上:p1 p2 printf('\n')都是可以编的过,但是有问题的语句,在进行打印的使用就会段错误, 因为使用了错误的地址值,访问了不该访问的内存,野指针!!!
Code reading:
char c = " "; while(c == "\t" || c == " " || c == "\n") { scanf("%c", &c); }
Code analysis:
c的初始化:使用一个只有空格字符的字符串字面量进行初始化;实际上是用一个指针(内存地址值)进行初始化 由于char类型占1个字节,指针类型占4字节(32位),所以会发生截断 再去进行while循环的判断,第一次就为false,不会进到循环中 所以要正确判断字符类型和字符串类型,确保使用的类型是正确的: 修改,将"\t"等都改成'\t'。
This article is summarized from "C Language Advanced Course" by Tang Zuolin from "Ditai Software Academy".
If there are any errors or omissions, please correct me.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。