C program that won’t compile in C++


The C++ language is designed by adding some additional features like Object Oriented concepts with C. Most of the C programs can be compiled using C++ compiler also. Though there are some programs that cannot be compiled using C++ compiler.

Let us see some code, that will compile in C compiler, but not in C++ compilers.

In this program there will be one compilation error for C++ code. Because it is trying to call a function that is not declared before. But in C it may compile

Live Demo For C.

Example

 Live Demo

#include<stdio.h>
int main() {
   myFunction(); // myFunction() is called before its declaration
}
int myFunction() {
   printf("Hello World");
   return 0;
}

Output(C)

Hello World

Output (C++)

[Error] 'myFunction' was not declared in this scope

In C++ one normal pointer cannot point some constant variables, but in C, it can point.

Live Demo For C.

Example

 Live Demo

#include<stdio.h>
int main() {
   const int x = 10;
   int *ptr;
   ptr = &x;
   printf("The value of x: %d", *ptr);
}

Output(C)

The value of x: 10

Output (C++)

[Error] invalid conversion from 'const int*' to 'int*' [-fpermissive]

In C++, we have to explicitly typecast when we want to assign some other pointer types like int*, char* to void pointer, but in C, if it is not type casted, it will be compiled.

Live Demo For C.

Example

 Live Demo

#include<stdio.h>
int main() {
   void *x;
   int *ptr = x;
   printf("Done");
}

Output(C)

Done

Output (C++)

[Error] invalid conversion from 'void*' to 'int*' [-fpermissive]

In C++, we must have to initialize the constant variables but in C, it can be compiled without initializations.

Live Demo For C.

Example

 Live Demo

#include<stdio.h>
int main() {
   const int x;
   printf("x: %d",x);
}

Output(C)

x: 0

Output (C++)

[Error] uninitialized const 'x' [-fpermissive]

In C we can use some variable named ‘new’. But in C++, we cannot use this name as variable name because in C++, the ‘new’ is a keyword. This is used to allocate memory spaces.

Live Demo For C.

Example

 Live Demo

#include<stdio.h>
int main() {
   int new = 10;
   printf("new: %d",new);
}

Output(C)

new: 10

Output (C++)

[Error] expected unqualified-id before 'new'
[Error] expected type-specifier before ')' token

We cannot compile the following code in C++. This will return an error when we are trying to convert int to char*. But in C, it will work fine.

Live Demo For C.

Example

 Live Demo

#include<stdio.h>
int main() {
   char *c = 123;
   printf("c = %u", c);
}

Output(C)

c = 123

Output (C++)

[Error] invalid conversion from 'int' to 'char*' [-fpermissive]

In C we can use void as return type of main(), but in C++, we have to use int as return type of main().

Live Demo For C.

Example

 Live Demo

#include<stdio.h>
void main() {
   printf("Hello World");
}

Output(C)

Hello World

Output (C++)

[Error] '::main' must return 'int'

Updated on: 30-Jul-2019

321 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements