C和C++中的字符大小是多少?据我所知,char的大小在C和C++中都是1个字节.
#include <stdio.h>
int main()
{
printf("Size of char : %d\n", sizeof(char));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
#include <iostream>
int main()
{
std::cout << "Size of char : " << sizeof(char) << "\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
没有惊喜,它们都给出了输出: Size of char : 1
现在我们知道,字符表示为'a','b','c','|',...所以我只是修改了上面的代码对这些:
在C:
#include <stdio.h>
int main()
{
char a = 'a';
printf("Size of char : %d\n", sizeof(a));
printf("Size of char : %d\n", sizeof('a'));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Size of char …Run Code Online (Sandbox Code Playgroud) 在C中,以下代码:
#include<stdio.h>
int main()
{
char c='a';
printf("%d %d",sizeof(c),sizeof('a'));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
产生结果1和4?请解释逻辑?
另外,为什么会sizeof(main())导致4但sizeof(main)导致1:
#include<stdio.h>
int main()
{
printf("%d %d\n",sizeof(main), sizeof(main()));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在C++中为什么会sizeof('a')导致1而sizeof('av')导致4?