geeksforgeeks 004-相同的程序,在c和c++中的运行结果却不同

  1. character literals(字符)在C和C++中的处理方式是不同的

    1
    2
    3
    4
    5
    6
    #include<stdio.h>
    int ()
    {
    printf("%d", sizeof('a'));
    return 0;
    }

    在C中,是sizeof(int);在C++中,是sizeof(char)

  2. 在C中,需要使用struct来声明一个结构体变量,在C++中不是必须的。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    #include <stdio.h>
    int T;

    int ()
    {
    struct T { double x; };
    // but not in C
    printf("%d", sizeof(T));
    return 0;
    }

    在C中是sizeof(int),在C++中是sizeof(struct)

  3. 布尔操作的结果不同

    1
    2
    3
    4
    5
    // output = 4 in C (which is size of int)
    printf("%d", sizeof(1==1));

    // output = 1 in c++ (which is the size of boolean datatype)
    cout << sizeof(1==1);