Here we will see some incompatibilities between C and C. Some C code that can be compiled using a C compiler cannot be compiled in a C compiler. and will return an error.
#include<stdio.h> void my_function(x, y)int x;int y; { // Not valid in C++ printf("x = %d, y = %d", x, y); } int main() { my_function(10, 20); }
x = 10, y = 20
Error in C++ :- x and y was not declared in this scope
#include<stdio.h> main() { const x = 10; const y = 20; printf("x = %d, y = %d", x, y); }
x = 10, y = 20
Error in C++ :- x does not name a type y does not name a type
#include<stdio.h> int x; int x; int main() { x = 10; printf("x = %d", x); }
x = 10
Error in C++ :- Redefinition of int x
#include<stdio.h> #include<malloc.h> void my_function(int n) { int* ptr = malloc(n* sizeof(int)); //implicitly convert void* to int* printf("Array created. Size: %d", n); } main() { my_function(10); }
Array created. Size: 10
Error in C++ :- Invalid conversion of void* to int*
#include<stdio.h> void my_function() { printf("Inside my_function"); } main() { my_function(10, "Hello", 2.568, 'a'); }
Inside my_function
Error in C++ :- Too many arguments to function 'void my_function()'
The above is the detailed content of Incompatibility between C and C++. For more information, please follow other related articles on the PHP Chinese website!