Incompatibilities between C and C++

Here we will see some incompatibilities between C and C++. Some C codes that can be compiled using C compiler, but does not compile in C++ compiler and returns errors.

Syntax

// C allows various syntaxes that C++ rejects
// Old-style function declarations, implicit int, multiple declarations, etc.

Example 1: Old-Style Function Declarations

C allows defining functions with parameter types specified after the parameter list −

#include <stdio.h>

void my_function(x, y)
int x;
int y; 
{
    printf("x = %d, y = %d\n", x, y);
}

int main() {
    my_function(10, 20);
    return 0;
}
x = 10, y = 20

C++ Error: x and y was not declared in this scope

Example 2: Implicit int Type

In C, variables without explicit type default to int, but C++ requires explicit type declarations −

#include <stdio.h>

int main() {
    const x = 10;
    const y = 20;
    printf("x = %d, y = %d\n", x, y);
    return 0;
}
x = 10, y = 20

C++ Error: x does not name a type, y does not name a type

Example 3: Multiple Global Variable Declarations

C allows declaring the same global variable multiple times, but C++ treats it as redefinition −

#include <stdio.h>

int x;
int x;

int main() {
    x = 10;
    printf("x = %d\n", x);
    return 0;
}
x = 10

C++ Error: Redefinition of int x

Example 4: Implicit void Pointer Conversion

C automatically converts void* to any pointer type, but C++ requires explicit casting −

#include <stdio.h>
#include <stdlib.h>

void my_function(int n) {
    int* ptr = malloc(n * sizeof(int));
    if (ptr != NULL) {
        printf("Array created. Size: %d\n", n);
        free(ptr);
    }
}

int main() {
    my_function(10);
    return 0;
}
Array created. Size: 10

C++ Error: Invalid conversion of void* to int*

Example 5: Function Calls with Extra Arguments

C allows calling functions with more arguments than declared parameters −

#include <stdio.h>

void my_function() {
    printf("Inside my_function\n");
}

int main() {
    my_function(10, "Hello", 2.568, 'a');
    return 0;
}
Inside my_function

C++ Error: Too many arguments to function 'void my_function()'

Key Differences

  • Type Safety: C++ enforces stricter type checking than C
  • Function Declarations: C++ requires modern ANSI function syntax
  • Implicit Conversions: C++ eliminates many implicit conversions allowed in C
  • Variable Declarations: C++ requires explicit type specifications

Conclusion

These incompatibilities highlight C++'s stricter type safety and more rigorous syntax requirements compared to C. Understanding these differences helps when migrating C code to C++.

Updated on: 2026-03-15T10:39:27+05:30

217 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements