Output of C++ programs | Set 24 (C++ vs C)

Predict the output of the following C++ programs.

  1. Program 1
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <iostream>
    using namespace std;
    int main()
    {
        char array[3] = "abc";
        cout << array;    
        return 0;
    }

    chevron_right

    
    

    Output:

    error: initializer - string for array of chars is too long
    

    Explanation: The application of array in C++ is similar to that in C. The only exception is the way character arrays are initialized. When initializing character array in ANSI C, the compiler will allow the following declaration —

    char array[3] = "abc"    // allowed in ANSI C

    This is because C assumes that the programmer intends to leave out the NULL (‘\0’) character in the definition. But in C++ the size should be 1 larger then the number of characters hence following statement is correct.

    char array[4] = "abc"    // O.K. for  C++
  2. Program 2
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <iostream>
    using namespace std;
    int main()
    {
        cout << sizeof('x');
        cout << sizeof(char);    
        return 0;
    }

    chevron_right

    
    

    Output:

    1 1
    

    Explanation: It is notable that character constants are stored as character type in C++ but this is not the case in C. In ANSI C, the same program would produce the result —

    4 1     // result when code is executed in C

    because character constants are promoted to int.
    Recommended that you execute the same code in C

  3. Program 3
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <iostream>
    using namespace std;
    int main()
    {
        void *ptr1;
        char *ptr2;
        ptr2 = ptr1; // statement 1
        return 0;
    }

    chevron_right

    
    

    Output:

    error: invalid conversion from 'void*' to 'char*
    

    Explanation: Assigning any pointer type to a void pointer without using a cast is allowed in C or C++. But we can not assign a void pointer to a non-void pointer without using a cast to non-void pointer type in C++. Hence statement one should be —

    ptr2 = (char*)ptr1;    // valid in C++

    Note — It should be noted that when the same code is compiled in C, it will not produce any error i.e. in C we can assign a void pointer to a non-void pointer without using any type casting.

  4. Program 4
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <iostream>
    using namespace std;
    int main()
    {
        const int size;
        cout << size;
        return 0;
    }

    chevron_right

    
    

    Output:

    error: uninitialized const 'size'
    

    Explanation: At first glance its obvious to think that output will be some garbage value, but it is not the case. Unlike C in C++ any variable declared as constant needs to be initialized at the time of declaration. Hence the given code will not compile and throw the error. It should be noted that the same code will get compiled in ANSI C and will produce output as 0.

  5. Program 5
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <iostream>
    using namespace std;
    int main()
    {
        enum season { spring, summer, autumn, winter };
          
        season myFavSeason_1 = spring;
        season myFavSeason_2 = 4;  // statement 1
          
        cout << myFavSeason_1;
        cout << endl;
        cout << myFavSeason_2;
      
        return 0;
    }

    chevron_right

    
    

    Output:

    error: invalid conversion from 'int' to 'main()::season'
           season myFavSeason_2 = 4;
    

    Explanation: Unlike C in C++, each enumerated data type retains its own separate type. This means C++ does not permit an int value to be automatically converted to an enum value. So there will be error in executing the code. However if type casting is done the code will work change statement 1 to the following statement —

    season myFavSeason_2 = (season) 4; 
    

Related Articles: C vs C++
This article is contributed by Avinash Kumar Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up