Write a C program that won’t compile in C++
Although C++ is designed to have backward compatibility with C, there can be many C programs that would produce compiler errors when compiled with a C++ compiler. Following is the list of the C programs that won’t compile in C++:
- Calling a function before the declaration
- Using normal pointer with const variable
- Using typecasted pointers
- Declaring constant values without initializing
- Using specific keywords as variable names
- Strict type checking
- The return type of main()
These points are discussed in detail below:
1) Calling a function before declaration: In C++, it is a compiler error to call a function before it is declared. But in C, it may compile. (See What happens when a function is called before its declaration in C?)
C
// C Program to demonstrate calling // a function before declaration #include <stdio.h> // Main starts int main() { // fun() is called before its // declaration/definition fun(); } // Function Declaration int fun() { printf ( "Hello" ); return 0; } |
2) Using a normal pointer with const variable: In C++, a compiler error is generated when a normal pointer is used to point to a const variable, however, it is allowed in C. (Must Read – Const Qualifier in C)
C
// C Program to demonstrate using a // normal pointer with const variable #include <stdio.h> // Main starts int main() { // A normal pointer points to const int const j = 20; int * ptr = &j; // The below assignment is invalid in C++, // results in error.In C, the compiler may // throw a warning, but casting is implicitly allowed printf ( "*ptr: %d\n" , *ptr); return 0; } |
3) Using typecasted pointers: 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 typed cast.
C
// C Program to demonstrate // using typecasted pointers #include <stdio.h> // Main starts int main() { void * vptr; // In C++, it must be // replaced with int *iptr=(int *)vptr; int * iptr = vptr; return 0; } |
Note: 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) Declaring constant values without initializing: In C++, the const variable must be initialized but in C it is not necessary. The following program compiles & runs fine in C, but fails in the compilation in C++.
C
// C Program to demonstrate declaring // constant values without initializing: #include <stdio.h> // Main starts int main() { const int a; return 0; } |
5) Using specific keywords as variable names: In C, specific keywords can be used as variable names, however, it is not possible in C++. The following program won’t compile in C++ but would compile in C.
C
// C Program to demonstrate using // specific keywords as variable names #include <stdio.h> // Main starts int main( void ) { // new is a keyword in C++ // but not in C int new = 5; printf ( "%d" , new ); } |
Similarly, we can use other keywords like delete, explicit, class, etc.
6) Strict type checking: 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*'”.
C
// C Program to demonstrate // strict type checking #include <stdio.h> // Main starts int main() { char *c = 333; printf ( "c = %u" , c); return 0; } |
7) Return type of main(): In C++, the main function requires the return type of ‘int’, however not the case in C. In C++, we cannot use the return type as ‘void’.
C
// C Program to demonstrate that // 'void' can be used as a return type // for main() #include <stdio.h> // Main starts void main() { printf ( "Hello World" ); } |
8) The following program compiles in C but doesn’t compile in C++. (See this article for more reference.)
C
// C Program that won't compile in C++ #include <stdio.h> void func() { // definition } // Main starts int main() { func(); func(2); } |
Explanation: In C++, func() is equivalent to func(void), however in C, func() is equivalent to func(…).
Contact Us