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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With