在這裡,我們將看到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中文網其他相關文章!