Incompatibilities between C and C++ codes

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.

// 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()'


Disclaimer: This does not belong to TechCodeBit, its an article taken from the below
source and credits.
source and credits: http://www.geeksforgeeks.org
We have built the accelerating growth-oriented website for budding engineers and aspiring job holders of technology companies such as Google, Facebook, and Amazon
If you would like to study our free courses you can join us at

http://www.techcodebit.com. #techcodebit #google #microsoft #facebook #interview portal #jobplacements
#technicalguide

rakesh

Leave a Reply

Your email address will not be published. Required fields are marked *

Skip to toolbar