Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confused over function call in pre-ANSI C syntax

Tags:

c

syntax

function

I'm dealing with some pre-ANSI C syntax. See I have the following function call in one conditional

 BPNN *net;
 // Some more code
 double val;
 // Some more code, and then,
 if (evaluate_performance(net, &val, 0)) {

But then the function evaluate_performance was defined as follows (below the function which has the above-mentioned conditional):

evaluate_performance(net, err)
BPNN *net;
double *err;
{

How come evaluate_performance was defined with two parameters but called with three arguments? What does the '0' mean?

And, by the way, I'm pretty sure that it isn't calling some other evaluate_performance defined elsewhere; I've greped through all the files involved and I'm pretty sure the we are supposed to be talking about the same evaluate_performance here.

Thanks!

like image 607
skytreader Avatar asked Sep 29 '11 18:09

skytreader


1 Answers

If you call a function that doesn't have a declared prototype (as is the case here), then the compiler assumes that it takes an arbitrary number and types of arguments and returns an int. Furthermore, char and short arguments are promoted to ints, and floats are promoted to doubles (these are called the default argument promotions).

This is considered bad practice in new C code, for obvious reasons -- if the function doesn't return int, badness could ensure, you prevent the compiler from checking that you're passing the correct number and types of parameters, and arguments might get promoted incorrectly.

C99, the latest edition of the C standard, removes this feature from the language, but in practice many compilers still allow them even when operating in C99 mode, for legacy compatibility.

As for the extra parameters, they are technically undefined behavior according to the C89 standard. But in practice, they will typically just be ignored by the runtime.

like image 79
Adam Rosenfield Avatar answered Oct 04 '22 04:10

Adam Rosenfield