
在这里,我们将看到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中文网其它相关文章!
c++怎么学习?c++怎么入门?c++在哪学?c++怎么学才快?不用担心,这里为大家提供了c++速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号