Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Type Checking 
3.
Calling a function 
4.
Function parameters
5.
Typecasting pointers 
6.
Constant variables and non-constant pointers
7.
Constant variables and their initialization
8.
Keywords 
9.
FAQs 
10.
Key Takeaways 
Last Updated: Mar 27, 2024
Easy

Programs that Compile in C but not C++

Author Vidhi Singh
0 upvote
Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

Usually, we have C as a language first and then C++ in the courses. Also, a lot of the syntax for both these languages is similar. C++ comes with all the features of C and adds more to it. So naturally, we tend to think that C++ is a superset of C. But, that’s not the case.
Let’s see a program: 

#include <stdio.h>
int main() 
{
	char *a=65; 
	printf("%d",a); 
	return 0;
}

 

Running the above program on the C compiler gives the following output:

Compilation Successful
65

 

Running the same program on the C++ compiler gives the following output:

Compilation error
prog.cpp: In function ‘int main()’:
prog.cpp:3:10: error: invalid conversion from ‘int’ to ‘char*’ [-fpermissive]
char *a=65;
          ^~
prog.cpp:4:9: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘char*’ [-Wformat=]
printf("%d",a);
         ^~~~ ~

 

This is not actually what we expected, right? It gave an output without any error; the same should have been when we tried it with C++. It is one such case where a program runs fine without giving any error in C but not C++.  
 

Let’s look at more such scenarios, where will help us understand about the programs that compile in C and not in C++. You can also do a free certification of Basics of C++.

You can also do a free certification of Basics of C++

Type Checking 

Type Checking in a programming language simply refers to ensuring that the operands of an operator are of compatible types. Compatible type implies any type that is not illegal or allowed under the specific language rule. Although, the compiler itself can do an implicit conversion to the legal type.

For example, in the statement, c=3+i*a, variable ‘a’ must be a type that allows multiplication operation on it, or implicit type conversion should happen to allow that operation. Otherwise, it leads to an error.

Taking the above example again:

#include <stdio.h>
int main() 
{
	char *a=65; 
	printf("%d",a); 
	return 0;
} 

 

Output in C:

Output in C++:

                                

You can try by yourself with the help of online c++ compiler.

As mentioned earlier, this program gives a compilation error in C++ but not in C. This is because the C++ type checker is more specific in nature. Since, in the above given example, ‘%d’, format specifier is for integer, but the variable ‘a’ is neither compatible with it nor converted to a compatible one. Therefore, it produces an error. 

Also see, Fibonacci Series in C++, Abstract Data Types in C++ 

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Calling a function 

Consider the following example: 

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


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?");
}

 

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;
}

 

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;
}

 

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;
}

 

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;
}

 

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;
}

 

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;
}

 

Output:

Constant variables and their initialization

Look at the below code:

#include <stdio.h>
int main()
{
    const int num; 
    printf("Succesful");
    return 0;
}

 

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;
} 

 

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 

  1. 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.
     
  2. 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. 
     
  3. 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!  

Live masterclass