Question

As I read ISO/IEC 9899:TC3 6.9.1 -> EXAMPLE 13

And the note:

extern int max(int a, int b)
{
    return a > b ? a : b;
}

[...]

extern int max(a, b)
int a, b;
{
    return a > b ? a : b;
}

Here int a, b; is the declaration list for the parameters. The difference between these two definitions is that the first form acts as a prototype declaration that forces conversion of the arguments of subsequent calls to the function, whereas the second form does not.

So I wrote my own test code to compile it that way. (I figgured out, both ways would need to be declared before or had to ahve an prototype with the type definition mentioned anyway)

size_t foo (size_t a, size_t b);

int main(int argc, char** argv)
{
    /*some call to foo*/
    return 0;
}

So I'm asking now: is there any performance related difference between this way:

size_t foo (a, b)
size_t a, b;
{   
    return a > b ? a : b;
}

and this one?

size_t foo (size_t a, size_t b)
{   
    return a > b ? a : b;
}

Because as I understood it, there is a difference, because the first way avoids multiple conversions at function call, as it says there has to be no conversion to the parameter type, as it's guaranteed the type will be of type (in this case) size_t.

But I was confused as I figured out, the prototype has to look similar in both cases and I thought the prototype is the source where the compiler takes his treatment from.

So: whats the difference exactly? and if there is one, why is the first way so rarely (never) seen?

Was it helpful?

Solution

Doing this:

size_t foo (a, b)
size_t a, b;
{   
    return a > b ? a : b;
}

means that if you now call it like this:

int x = 3;
int y = 4;

foo(x,y);

The arguments will not be converted to size_t before being passed to the function. This means you'll get undefined behaviour (assuming sizeof(size_t) > sizeof(int)). And the compiler won't warn you. That's why no-one does this in practice.

OTHER TIPS

The first way is an old ISO standard. It's not used any more in any production code, but you may still find it in some legacy code. I doubt there is any difference between the two excepting the syntax.

This method

size_t foo (a, b)
size_t a, b;
{   
    return a > b ? a : b;
}   

of defining functions comes from K&R C, So you may encounter this in older books and programs. C89 and C99 support this style. Programmers avoid using it in new programs for a couple of reasons.

First, functions that are defined in the older way aren't subject to the same degree of error checking. When a function is defined in older way- and no prototype is present- the compiler won't check that the function is called with the right number of arguments, nor will it check that the arguments have proper types. Instead, it will perform the default argument promotion.

Second: 6.11.6 Function declarators says

The use of function declarators with empty parentheses (not prototype-format parameter type declarators) is an obsolescent feature

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top