To get this answer, we just need to think about the difference in C & C++.
Examples:
1)
There is automatic implicit type conversion in C which is not available in C++.
void *vptr;
int *iPTR = vptr; //allowed in C, not in C++
To compile in C++:
void *vptr;
int *iPTR = static_cast<int*> vptr;
2)
There is automatic implicit type conversion in C which is not available in C++.
int temp = malloc (10); //allowed in C, not in C++
int temp = malloc (sizeof (int) * 10); //allowed in C, not in C++
To Compile in C++:
int temp = static_cast<int*> malloc (10);
int temp = static_cast<int*> malloc (sizeof (int) * 10);
There are many additional keywords available in C++ which is not available in C.
int new; //Compile in C not in C++ as new is keyword in C++
int class; //keyword in c++
3)
Function without argument is considered as arguments are not provided and it is legal to call the function with arguments. While C++ it is considered no argument and not legal to call the function with error .
int foo()
void main()
{
foo(2,3); //error in C++
}
4)
sizeof(‘ch’) is equal to sizeof(int) in C
sizeof(‘ch”) is equal to sizeof(char) in C++
5)
It is required to initialize the const variable while declaring it in C++ but not necessary in C.
const int a; //through error in C++
6)
Variable length array is not allowed in C++ while allowed in C.
int a = func(2);
int b[a];
To handle the same thing in C++ you need to use vector.
vector<int> b(a);
7)
Assignment of const integer pointer to integer pointer is not allowed without type casting in C++ but allowed in c .
const int x = 12;
int* a = &x;
Some of the c compiler generate warnings.
8)
Function without return type is considered as integer return type by default in C but not in C++.
func(int);
void main()
{
int abc = func(5);
}
This works well with C but not with C++.
9)
You cannot skip or cross the definition using goto or switch statement in C++ while possible in C.
goto yourLabel:
int ab =5;
yourLabel:
printf(“%d”, ab);
Please share your comments and feedback on above programs.
0 comments :
Post a Comment