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
#include <stdio.h>
int main()
{
fun();
}
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
#include <stdio.h>
int main()
{
int const j = 20;
int* ptr = &j;
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
#include <stdio.h>
int main()
{
void* 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
#include <stdio.h>
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
#include <stdio.h>
int main(void)
{
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
#include <stdio.h>
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
#include <stdio.h>
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
#include <stdio.h>
void func()
{
}
int main()
{
func();
func(2);
}
|
Explanation: In C++, func() is equivalent to func(void), however in C, func() is equivalent to func(âĶ).
Learn to code easily with our course
Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
22 Nov, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...