Although C++ is designed to have backward compatibility with C there can be many C programs that would produce compiler error when compiled with a C++ compiler. Following are some of them.
1) In C++, it is a compiler error to call a function before it is declared. But in C, it may compile (See https://www.geeksforgeeks.org/g-fact-95/)
2) In C++, it is compiler error to make a normal pointer to point a const variable, but it is allowed in C. (See Const Qualifier in C)
3) In C, a void pointer can directly be assigned to some other pointer like int *, char *. But in C++, a void pointer must be explicitly typcasted.
This is something we notice when we use malloc(). Return type of malloc() is void *. In C++, we must explicitly typecast return value of malloc() to appropriate type, e.g., “int *p = (int *)malloc(sizeof(int))”. In C, typecasting is not necessary.
4) Following program compiles & runs fine in C, but fails in compilation in C++. const variable in C++ must be initialized but in c it isn’t necessary. Thanks to Pravasi Meet for suggesting this point.
Line 4 [Error] uninitialized const 'a' [-fpermissive]
5) This is the worst answer among all, but still a valid answer. We can use one of the C++ specific keywords as variable names. The program won’t compile in C++, but would compiler in C.
Similarly, we can use other keywords like delete, explicit, class, .. etc.
6) C++ does more strict type checking than C. For example the following program compiles in C, but not in C++. In C++, we get compiler error “invalid conversion from ‘int’ to ‘char*'”. Thanks to Pravasi Meet for adding this point.
7) C++ require main return ‘int’ type
8) A C/C++ Function Call Puzzle
This article is contributed by Abhay Rathi. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- How to compile 32-bit program on 64-bit gcc in C and C++
- How to Compile and Run C/C++/Java Programs in Linux
- Runtime and Compile-time constants in C++
- VS Code | Compile and Run in C++
- Write a C program to print "Geeks for Geeks" without using a semicolon
- Write a C program that does not terminate when Ctrl+C is pressed
- Write a program that produces different results in C and C++
- Write a URL in a C++ program
- Write a C program that displays contents of a given file like 'more' utility in Linux
- Write a C program to print "GfG" repeatedly without using loop, recursion and any control structure?
- C program to write an image in PGM format
- Write a one line C function to round floating point numbers
- Write a C macro PRINT(x) which prints x
- Write one line functions for strcat() and strcmp()
- When should we write our own copy constructor?
- Does C++ compiler create default constructor when we write our own?
- When should we write our own assignment operator in C++?
- fopen() for an existing file in write mode
- How to write a running C code without main()?
- Write your own memcpy() and memmove()