Incompatibilities between C and C++ codes

2

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:

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.

GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



2 Average Difficulty : 2/5.0
Based on 1 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.