在这里,我们将看到C和C++之间的一些不兼容性。一些可以使用C编译器编译的C代码,在C++编译器中无法编译。并且会返回错误。
#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()'
以上是C和C++之间的不兼容性的详细内容。更多信息请关注PHP中文网其他相关文章!