Calling a function
Consider the following example:
#include <stdio.h>
int main()
{
example();
return 0;
}
void example()
{
printf("Are you a Ninja?");
}

You can also try this code with Online C Compiler
Run Code
Output in C:

Output in C++:

C allows calling a function before it is declared. But, doing the same in C++ gives a compilation error(as shown above).
Although, in C, if a function is called before the declaration, the compiler automatically assumes its return type to be ‘int’. If it is not an ‘int’ later, it returns an error.
In C++, the compiler encourages forward declaration. All the entities that are being used or called should be at least declared, if not defined, beforehand. Not following this rule leads to the generation of Forward Declaration Error.
The following code is correct for C++:
#include <stdio.h>
void example();
int main()
{
example();
return 0;
}
void example()
{
printf("Are you a Ninja?");
}

You can also try this code with Online C++ Compiler
Run Code
Output:

Function parameters
Have a look at the following program,
#include <stdio.h>
void example()
{
printf("Are you a ninja?\n");
}
int main()
{
example();
example(1);
return 0;
}

You can also try this code with Online C Compiler
Run Code
Output in C:

Output in C++:

This program works fine in C but throws an error in C++ because if the signature of a function has no parameters mentioned in the C language, it can take any number of parameters of any data type. So, in C, example() implies example (void), example (int), example (int, char), example (int,int,char), etc.
But, in C++, whatever parameters are specified in the signature of the function, only those can be used during parameter passing. So, in C++, example() only implies example(void).
The following code runs in C++:
#include <stdio.h>
void example()
{
printf("Are you a ninja?\n");
}
int main()
{
example();
return 0;
}

You can also try this code with Online C++ Compiler
Run Code
Output:

Typecasting pointers
Let’s see the following code:
#include<stdio.h>
#include<malloc.h>
int main()
{
int *p=malloc(sizeof(int));
printf("Success");
return 0;
}

You can also try this code with Online C Compiler
Run Code
Output in C:

Output in C++:

Here, what should be noted is the return type of malloc() function is void. So, explicit typecasting needs to be done for the C++ language. Whereas in C, it is not required as it happens implicitly.
This concludes that in C, the void pointer can directly be assigned to a pointer of any type. But in C++, doing so generates an error.
The following code works in C++:
#include<stdio.h>
#include<malloc.h>
int main()
{
int *p=(int *)malloc(sizeof(int));
printf("Success");
return 0;
}

You can also try this code with Online C++ Compiler
Run Code
Output:

Constant variables and non-constant pointers
Carefully look at the following code:
#include <stdio.h>
int main()
{
int const ex=1;
int* pt=&ex;
printf("%d",*pt);
return 0;
}

You can also try this code with Online C Compiler
Run Code
Output in C:

Output in C++:

In C++, a non-constant pointer cannot point to the constant variable. This returns a compiler error. But, in C, this is allowed. Although a warning may be thrown, but no error is generated as implicit typecasting is not restricted by the C compiler.
The following code will run perfectly in C++:
#include <stdio.h>
int main()
{
int const ex=1;
const int* pt=&ex;
printf("%d",*pt);
return 0;
}

You can also try this code with Online C++ Compiler
Run Code
Output:

Constant variables and their initialization
Look at the below code:
#include <stdio.h>
int main()
{
const int num;
printf("Succesful");
return 0;
}

You can also try this code with Online C Compiler
Run Code
Output in C:

Output in C++:

Outputs in both languages are different and erroneous in C++ because it is mandatory to initialize the constant variable in C++ while there is no such compulsion in C. They can be just declared and not initialized.
The correct code for C++ will be:
#include <stdio.h>
int main()
{
const int num=6;
printf("Succesful");
return 0;
}

You can also try this code with Online C++ Compiler
Run Code
Output:

Keywords
There are 32 keywords in C and around 60 (this figure may vary) in C++. So, a few keywords are not common in both languages. Therefore, these keywords can be used as identifiers in C. Few of them are new, delete, etc.
Apart from all these cases, there can be other cases where the program may compile successfully in C but not in C++. You may also incur situations where a program runs in a compiler but is erroneous in another. The reason could be that both of those compilers follow different standards of the same language. Some of them may even be version dependent.
Also check out this article - Pair in C++
Must Read Passing Arrays to Function in C
FAQs
-
Do all programs that compile in C also get compiled by the C++ compiler?
No, both the languages have their own rules. So, few compiled programs of C may return errors when compiler by C++ compiler.
-
Are all the features provided by C present in C++?
No, some of the features of C are missing in C++. Although, C++ has its own features to overcome those.
-
Do all the C++ programs run in C as well?
No, some of the C++ programs may run in C. But, C++ syntax is rarely recognized in C.
Key Takeaways
This article explains different programs that compile perfectly in C language but may generate errors with C++ compilers by taking example code snippets and discussing them in detail. We also look at what changes can be made in the programs so that they work with the compilers of both languages.
We hope that this blog has helped you enhance your knowledge regarding Programs that compile in C but not in C++, and if you would like to learn more, check out our articles on More on C++. Do upvote our blog to help other ninjas grow.
Recommended Readings:
Happy Coding!