【问题标题】:why does following code give different output in C , C++? [duplicate]为什么以下代码在 C、C++ 中给出不同的输出? [复制]
【发布时间】:2014-10-08 00:57:12
【问题描述】:

以下代码给出的输出为 C 中的 1、4、4 而 C++ 中的 1、1、4

#include<stdio.h> 
int main() 
{  
    char ch = 'A'; //initialise 
    //printing output
    printf("%d, %d, %d", sizeof(ch), sizeof('A'), sizeof(3.14f));    
    return 0; 
} 

【问题讨论】:

  • 但它不在stackoverflow 我在stackoverflow 搜索它n 没有找到任何类似的。好吧,我得到了对我来说很重要的答案。谢谢
  • i searched it here at stackoverflow n didn't found any similar one。不明白。 stackoverflow 的答案和那个不同吗?
  • 不,但那是在 geeksforgeeks.org n 人们将这个标记为重复,我认为你也在其中,这就是为什么

标签: c++ c


【解决方案1】:

因为字符文字在 C 中属于 int 类型,但在 C++ 中属于 char 类型。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-12
    相关资源
    最近更新 更多