The C/C++ incompatibilities that cause most real problems are not subtle. Most are easily caught by compilers.
This section gives examples of C code that is not C++ :
1) In C, functions can be defined using a syntax that optionally specifies argument types after the list of arguments:
// 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) In C and in pre-standard versions of C++, the type specifier defaults to 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) In C, a global data object may be declared several times without using the extern specifier. As long as at most one such declaration provides an initializer, the object is considered defined only once.
// 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) In C, a void* may be used as the right-hand operand of an assignment to or initialization of a variable of any pointer type.
// 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) In C, an array can be initialized by an initializer that has more elements than the array requires.
// 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; } |
Output:
Geeks
Error in C++ :- Initializer-string for array of chars is too long
6) In C, a function declared without specifying any argument types can take any number of arguments of any type at all. Click here to know more about this.
// 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()'
Related Articles:
- Is it fine to write “void main()” or “main()” in C/C++?
- Difference between “int main()” and “int main(void)” in C/C++?
- Output of C++ programs | Set 24 (C++ vs C)
This article is contributed by Sakshi Tiwari. If you like GeeksforGeeks (We know you do!) and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Similarities and Differences between Ruby and C language
- Similarities and Differences between Ruby and C++
- Similarities and Difference between Java and C++
- Difference and Similarities between PHP and C
- What are the differences between bitwise and logical AND operators in C/C++?
- Difference Between malloc() and calloc() with Examples
- What is the difference between single quoted and double quoted declaration of char array?
- Difference between "int main()" and "int main(void)" in C/C++?
- Difference between ++*p, *p++ and *++p
- What’s difference between header files "stdio.h" and "stdlib.h" ?
- Diffference between #define and const in C?
- What is the difference between "char a" and "char a[1]"?
- What is the difference between printf, sprintf and fprintf?
- Difference between getc(), getchar(), getch() and getche()
- What’s difference between “array” and “&array” for “int array[5]” ?
- What's difference between char s[] and char *s in C?
- Difference between C structures and C++ structures
- Difference between Structure and Union in C
- Difference between strlen() and sizeof() for string in C
- Difference between const char *p, char * const p and const char * const p

