不同编译器的不同输出 - C和C++

Adi*_*369 12 c c++ compilation

你能想到"一个程序",它为C和C++编译器提供了"不同的输出"(但是在同一种语言下提供了一致的输出)吗?

Ada*_*eld 20

ISO C和ISO C++之间的不兼容性

一个常见的例子是sizeof('A'),通常是在4 C,但总是在C++ 1,因为像字符常量'A'具有类型int在C,但该类型char在C++:

#include <stdio.h>

int main(void)
{
    printf("%d\n", sizeof('A'));
}
Run Code Online (Sandbox Code Playgroud)


caf*_*caf 19

该程序12使用C++或C99以及6C89生成:

#include <stdio.h>

int main()
{
    int a = 12//**/2;
    ;

    printf("%d\n", a);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)


Ben*_*igt 9

int main() { return sizeof 'a'; }
Run Code Online (Sandbox Code Playgroud)

  • 但不是完全便携.如果`sizeof(int)== 1`,那么`sizeof'a'`在C中可以是1(只有在`CHAR_BIT> = 16`时才有可能). (2认同)

Chr*_*odd 5

typedef char X;
int main() {
    struct X { double foo; }
    printf("%d\n", sizeof(X));
    return 0;
}
Run Code Online (Sandbox Code Playgroud)