相关疑难解决方法(0)

C/C++中的字符大小('a')

C和C++中的字符大小是多少?据我所知,char的大小在C和C++中都是1个字节.

在C:

#include <stdio.h>
int main()
{
    printf("Size of char : %d\n", sizeof(char));
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

在C++中:

#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 c++ types

287
推荐指数
4
解决办法
27万
查看次数

sizeof()后面的逻辑用于字符常量和函数名称

C中,以下代码:

#include<stdio.h>
int main()
{
   char c='a';
   printf("%d %d",sizeof(c),sizeof('a'));
   return 0;
}
Run Code Online (Sandbox Code Playgroud)

产生结果14?请解释逻辑?

另外,为什么会sizeof(main())导致4sizeof(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?

c c++ sizeof

4
推荐指数
1
解决办法
360
查看次数

标签 统计

c ×2

c++ ×2

sizeof ×1

types ×1