导致大多数实际问题的C/C++不兼容并不微妙。大多数都很容易被编译器捕获。 本节给出C++代码的实例,而不是C++。
null
1) 在C语言中,可以使用一种语法来定义函数,该语法可以在参数列表之后选择性地指定参数类型:
// C code that uses argument types after // the list of arguments. #include<stdio.h> void fun(a, b) int a; int b; // Invalid in C++ { printf ( "%d %d" , a, b); } // Driver code int main() { fun(8, 9); return 0; } |
Output: 8 9
Error in C++ :- a and b was not declared in this scope
2) 在C++和标准的C++版本中,类型说明符默认为int。
// C code to show implicit int declaration. #include<stdio.h> int main() { // implicit int in C, not allowed in C++ const a = 7; printf ( "%d" , a); return 0; } |
Output: 7
Error in C++ :- a does not name a type
3) 在C语言中,一个全局数据对象可以在不使用 外人 说明符。只要最多有一个这样的声明提供了一个初始值设定项,对象就被认为只定义了一次。
// C code to demonstrate multiple global // declarations of same variable. #include<stdio.h> // Declares single integer a, not allowed in C++ int a; int a; int main() { return 0; } |
Error in C++ :- Redefinition of int a
4) 在C语言中,void*可以用作任何指针类型变量赋值或初始化的右操作数。
// C code to demonstrate implicit conversion // of void* to int* #include<stdio.h> #include<malloc.h> void f( int n) { // Implicit conversion of void* to int* // Not allowed in C++. int * p = malloc (n* sizeof ( int )); } // Driver code int main() { f(7); return 0; } |
Error in C++ :- Invalid conversion of void* to int*
5) 在C语言中,数组可以由初始值设定项初始化,该初始值设定项的元素数超过数组所需的元素数。
// C code to demonstrate that extra character // check is not done in C. #include<stdio.h> int main() { // Error in C++ char array[5] = "Geeks" ; printf ( "%s" , array); return 0; } |
输出:
Geeks
Error in C++ :- Initializer-string for array of chars is too long
6) 在C语言中,一个未指定任何参数类型的函数可以接受任意数量的任何类型的参数。点击 在这里 想知道更多关于这件事。
// In C, empty brackets mean any number // of arguments can be passed #include<stdio.h> void fun() { } int main( void ) { fun(10, "GfG" , "GQ" ); return 0; } |
Error in C++ :- Too many arguments to function 'void fun()'
相关文章:
本文由 萨希·提瓦里 .如果你喜欢极客(我们知道你喜欢!)如果你想投稿,你也可以用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。
如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END