为什么sizeof()方法给出不同的结果? [重复]

问题描述 投票:0回答:1

当我在C中运行下面的程序时,输出结果为4

#include <stdio.h>

//using namespace std;

int main()
{
    printf("%d", sizeof('a')); 
  return 0; 
}

但是当我在C ++中运行下面的代码时,输​​出结果为1

#include <iostream>

using namespace std;

int main()
{
    printf("%d", sizeof('a')); 
  return 0; 
}

您能否解释为什么我为相同的代码获得不同的输出?

c++ c sizeof
1个回答
1
投票

在C中,字符表示形式(如'a')的类型为int。因此,sizeof运算符返回整数的大小。

© www.soinside.com 2019 - 2024. All rights reserved.