what is the difference between c and c++? explain it with example.

what is difference between c and cpp

Let us try to compile below sample code with gcc and g++. 

When we run the program after compilation from gcc ( cits output would be

When we run the program after compilation from g++ its output would be

In C, the type of a character constant like ‘a’ is actually an int, with size of 4 (or some other implementation-dependent value). In C++, the type is char, with size of 1.

 what is difference between c and cpp enum

In C language, an enum assign integers to names while in C++, an enum defines an actual type, which results in strong type checking. so following example will compile in c++ and would not compile in c.

When we compile above program in c (gcc compiler) it would give error during compilation. we will get below error.

If we replace line 12 with “enum surname alt = dk” then the above program would compile in c successfully.

c++ provides strong type checking compare to c language.

Let us compile the below code in c and c++

The above code will compile in c while it will not compile in c++. ‘g++ t.c’ command results in error like following

In c , the void * is promoted to char * ( in above program), while in c++ we require to typecast explicitly.  so the above program needs modification to  be compiled in c++. Below is modified code , that will compile in c and c++ without any error.



Related Contents to follow