ホームページ  >  記事  >  バックエンド開発  >  C と C++ 間の非互換性

C と C++ 間の非互換性

PHPz
PHPz転載
2023-08-28 18:33:061081ブラウズ

C と C++ 間の非互換性

ここでは、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
  • C 言語または一部の古いバージョンの C では、デフォルトの変数 Typeは整数です。しかし、C の新しいバージョンではエラーが発生します。

#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
  • C言語では、グローバル データ オブジェクトを使用せずに複数回宣言できます。 extern キーワード。 C コンパイラは、これを多数の宣言の中の 1 つとして扱います。

#include<stdio.h>
int x;
int x;
int main() {
   x = 10;
   printf("x = %d", x);
}

出力

x = 10

出力

Error in C++ :- Redefinition of int x
  • C言語では、voidポインタを代入演算子として使用できます。オペランド、またはポインタ型の変数を初期化するために使用されます。

#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*
  • C言語では、パラメータの型が指定されていない場合、次のことができます。複数のパラメータを渡します。

#include<stdio.h>
void my_function() {
   printf("Inside my_function");
}
main() {
   my_function(10, "Hello", 2.568, &#39;a&#39;);
}

出力

Inside my_function

出力

Error in C++ :- Too many arguments to function &#39;void my_function()&#39;

以上がC と C++ 間の非互換性の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はtutorialspoint.comで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。