C语言字符输出不一样

C语言 码拜 9年前 (2015-10-22) 1191次浏览
#include<stdio.h>
void main()
{
	char a=""c"";
	printf("%d\n",sizeof(a));
	printf("%d\n",sizeof(""c""));

}

为什么他们的输出是不一样的在C语言中

解决方案:20分
字符型常量一律按整型常量处理,见C标准
§6.4.4.4
10 An integer character constant has type int. The value of an integer character constant
containing a single character that maps to a single-byte execution character is the
numerical value of the representation of the mapped character interpreted as an integer.
The value of an integer character constant containing more than one character (e.g.,
“”ab””), or containing a character or escape sequence that does not map to a single-byte
execution character, is implementation-defined. If an integer character constant contains
a single character or escape sequence, its value is the one that results when an object with
type char whose value is that of the single character or escape sequence is converted to
type int.
解决方案:10分
#include<stdio.h>
int main()
{
	printf("%d, %d, %d, %d\n", sizeof(""a""), sizeof(""ab""), sizeof(""abc""), sizeof(""abcd""));  // 结果都是4
	printf("%d", sizeof(""abcde"")); // 这句VC会报错,MinGW是个警告,原因是int型的只能放4个字节
	return 0;
}
解决方案:10分
当然可能会丢失,但视情况。
float f = 1.1
int i = f;
这样会丢失,但假如f是1就不会丢失

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明C语言字符输出不一样
喜欢 (0)
[1034331897@qq.com]
分享 (0)