Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

Predict the output of the following C++ programs.

Program 1 

CPP




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


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 than the number of characters hence following statement is correct.

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

Program 2 

CPP




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


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

Program 3 

CPP




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


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.

Program 4 

CPP




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


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.

Program 5 

CPP




#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;
}


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++



Last Updated : 10 Nov, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads