我的代码的输出如何以及是 842?

How and is the output of my code is 842?

#include <stdio.h>

int main(){
printf("%d\t",sizeof(6.5));
printf("%d\t",sizeof(90000));
printf("%d\t",sizeof('a'));

return 0;
}

当我编译我的代码时,输​​出将是:“842”。 有人可以解释为什么我得到这个输出吗?

首先你的代码有语法错误

printf("%d\t";sizeof('a')); 

将此更改为

printf("%zu\t",sizeof('a'));   //note the change in format specifier also
             ^
             |
            see here

那么,假设你的平台是 32 位的

  • sizeof(6.5) == sizeof(double) == 8
  • sizeof(90000) == sizeof(int) == 4
  • sizeof('a') == sizeof(int) == 4

澄清一下,a 表示 97,默认为 int。所以,sizeof('a') 应该给出 4 的值,而不是 2 或 1。


编辑:

添加,你会得到8 4 2的输出,if, in 16-bit arch

  • sizeof(6.5) == sizeof(double) == 8
  • sizeof(90000) == sizeof(long) == 4
  • sizeof('a') == sizeof(int) == 2

如果您使用的是 32 位编译器

printf("%d\t",sizeof(6.5));

6.5 是双精度数,所以 sizeof(double) 给出 8.

printf("%d\t",sizeof(90000));

90000 是一个 int (或 long ),所以 sizeof(int) 给出 4.

printf("%d\t";sizeof('a'));
             ^
             you left a semicolon here, change it to a comma

'a' 转换为 int,所以 sizeof(int) 给出 4.

所以实际输出是

8     4      4

ideone link

但是,如果您使用的是 16 位编译器,您将得到

sizeof(6.5) = sizeof(double) = 8
sizeof(90000) = sizeof(long) = 4
sizeof('a') = sizeof(int) = 2

这样就可以解释你的输出了。