C Programs that won’t Compile in C++
About C Program
On this page we will discuss about the C programs 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 Programs that won’t compile in C++
Here, is list of some of the C programs that won’t compile in C++ :
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
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
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
4) 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