Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is C++ more restrictive regarding forward declaration of function prototypes (signatures)? [closed]

I noticed that C++ is more restrictive than C with regards to declaring function signatures before using them even if function definitions are declared after the function that actually calls them?

I always thought that C is more restrictive but it seems like this is not the case.

Why has the philosophy changed when making the standards for C++ programming language?

For example the following code runs compiles fine on gcc command but outputs an error when trying to compile with g++

#include<stdio.h>

int main()
{
    int a=sum(4,6);
    printf("%d",a);
    return 0;
}

int sum(int a,int b)
{
    return a+b;
}

The error is

‘sum’ was not declared in this scope

1 Answers

In older (before C99) C standard, there's a thing called "implicit function declaration" which has been removed since C99. So if you compile in C90 mode, a compiler has to support that "feature". Whereas in C++, "implicit function declaration" has never been there. So GCC errors out. Your code is not valid in modern C (C99 or later) either.

Compile with stricter compiler switches (e.g. -std=c99 -Wall -Wextra -pedantic-errors) and pay attention to all diagnostics.

like image 128
P.P Avatar answered Sep 14 '25 14:09

P.P