我说得对吗,C 中的 'A' 和 "A" 之间的区别是:
'A' - 表示包含 A 的 1 字节(8 位)长字符。
“A” - 表示长度为 2 个字节(16 位)的字符串,其中包含 A 和 NULL 字符。
我的解释正确吗?
谢谢。
最佳答案
你是绝对正确的。但是你必须记住,当你将它们用作rvalues
时,它们是不同的。其中一个具有char
类型,并且可以使用作为 int
类型,因为它隐式转换为 ASCII 值,而另一个类型为 char*
。
如果有帮助的话,让我用代码来说明我的观点:
int num='A'; //Valid, assigns 65 to num
char test=65; //Valid, as test will be 'A' after this
char *ptr="A" //Valid, assigns address of string "A" to pointer ptr
printf("%c,%d",'A','A'); // Output will be A,65
printf("%p",(void*)"A"); //Will print address where string "A" is
printf("%c","A"); ///WRONG
printf("%s","A"); //Works
编辑对于更细微的差别,如果您觉得您的理解还达到了这个标准,请参阅 Mat 的评论。否则,当您在 C 学习中取得进一步进展时,请在几周后阅读它.
关于c - C 中 'A' 和 "A"文字之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16386485/