Home > Backend Development > C++ > body text

Incompatibility between C and C++

PHPz
Release: 2023-08-28 18:33:06
forward
1117 people have browsed it

Incompatibility between C and C++

Here we will see some incompatibilities between C and C. Some C code that can be compiled using a C compiler cannot be compiled in a C compiler. and will return an error.

  • We can define functions using a syntax that optionally specifies parameter types after the parameter list.

Example

#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);
}
Copy after login

Output

x = 10, y = 20
Copy after login
Copy after login

Output

Error in C++ :- x and y was not declared in this scope
Copy after login
  • In C language or some older versions of C, the default variables Type is integer. But in new versions of C, an error occurs.

Example

#include<stdio.h>
main() {
   const x = 10;
   const y = 20;
   printf("x = %d, y = %d", x, y);
}
Copy after login

Output

x = 10, y = 20
Copy after login
Copy after login

Output

Error in C++ :- x does not name a type
y does not name a type
Copy after login
  • In C language, global data objects can be declared multiple times without Use the extern keyword. The C compiler will treat this as one declaration among many.

Example

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

Output

x = 10
Copy after login

Output

Error in C++ :- Redefinition of int x
Copy after login
  • In C language, we can use void pointer as assignment operator The right operand, or used to initialize any variable of pointer type.

Example

#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);
}
Copy after login

Output

Array created. Size: 10
Copy after login

Output

Error in C++ :- Invalid conversion of void* to int*
Copy after login
  • In C language, if the parameter type is not specified, we can pass Multiple parameters.

Example

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

Output

Inside my_function
Copy after login

Output

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

The above is the detailed content of Incompatibility between C and C++. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!