











C Program that won’t Compile in C++
About C Program
On this page we will discuss about the C program that won’t compile in C++. There are a few key differences between C and C++ that can lead to different behaviour in programs written in these languages.

C Program that won’t compile in C++
Even though C++ was developed to be compatible with C, but there are many C programs that can lead to compilation errors when compiled using a C++ compiler.
Here, is list of some of the C programs that won’t compile in C++ :
1) Calling a function before declaration: In C, it is possible to call a function before it is declared, as long as its definition appears in the same file before it is called. However, in C++, this is not allowed and will result in a compiler error.
Example:
#include <stdio.h> int main() { print_message("PrepInsta Prime"); // function is called before its declaration return 0; } void print_message(char *message) { printf ("%s\n", message); }
Output:
PrepInsta Prime
2) Using normal pointer with const variable: In C, it is possible to use a normal pointer (non-const pointer) to point to a const variable. However, in C++, this is not allowed and will result in a compiler error.
Example:
#include <stdio.h> int main() { const int x = 5; int *ptr = &x; // non-const pointer pointing to a const variable *ptr = 10; // The below assignment is invalid in C++,results in error. // In C, the compiler may throw a warning, but casting is implicitly allowed printf ("x = %d\n", x); return 0; }
Output:
x = 10
3) Using typecasted pointers: In C, it is possible to use typecasting to convert between different types of pointers. However, in C++ this is generally not allowed and will result in a compiler error.
Example:
#include <stdio.h> int main() { int x = 5; double* d_ptr = (double *) &x; printf ("x = %f\n", *d_ptr); return 0; }
Output:
x = -0.000000
4) Declaring constant values without initializing: In C, it is possible to declare a constant variable without initializing it. The variable will still have a memory space allocated to it, but it’s value will be undefined and could contain any random value. However, in C++, this is not allowed, and the compiler will raise an error
Example:
#include <stdio.h> int main() { const int x; printf("x = %d\n", x); return 0; }
Output:
x = 0
5) Strict type checking: C does not have strict type checking when it comes to arithmetic operations and it allows you to perform operations between different data types, like int and float. However, in C++ this is generally not allowed and will result in a compiler error.
Example:
#include <stdio.h> int main() { int x = 5; float y = 2.5; int result = x + y; // mixing int and float type printf ("result = %d\n", result); return 0; }
Output:
result = 7
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment