Advance C99 Default Arguments

I have a question about pushing the default arguments in the C99 standard. In the book "C-programming - a modern approach, second edition" I read that:

Converting Arguments:

[...]

1) The compiler met the prototype before the call. [...]

2) The compiler did not meet the prototype before the call . The compiler performs promotions by default: (1) float arguments are converted to double . (2) Integral promotions are performed, resulting in the conversion of the char and short arguments to int . (C99 runs entire promotions.)

A few lines below show an example in which there is no function prototype or definition before calling it. It is commented as follows:

Of course, a much better solution is to provide a square prototype before calling it. In C99, calling square without first declaring or defining a function is an error.

Do these two speed offers not contradict each other? I mean, if C99 forbids calling functions without a previous declaration / definition, how can it determine progress in this function call?

+4
source share
3 answers

No, they do not contradict each other.

An ad is not necessarily a prototype:

 int f(); 

declares a function f , but is not a prototype, since nothing is known about the types of arguments.

 int (a) in a; { ... } 

is a definition, but also not a prototype.

+7
source

C99 does not forbid you to call functions that do not have a prototype.

I can’t provide details because you did not send the code for square() or its call, but it is assumed that the progress made for the arguments in the square() call leads to different types being passed to the function, which it actually declares in implementation.

This will lead to undefined behavior and will be an error.

For instance:

 // in foo.c: int foo(void) { char x = 42; return square(x); // x is promoted to int, but is wrong } // in bar.c #include <stdio.h> void bar(int x) { square( &x); // this call is fine - what is being passed is what square() expects printf("The result is %d\n", x); } // in square.c void square( int* p) { *p *= *p; return; } 

And if square() is defined by a prototype declaring a parameter to have an argument of type char , there is no way to call it correctly if the prototype was not a "visible" calling code, since in such cases the argument will be increased to int .

+2
source

I can not find anything about the fact that this is a function call error without providing a preliminary declaration or definition on C99.

Closes, it looks like the type of the default return type int is no longer accepted. those. declaration foo(int a); resolved to -99, but error on C99. pre-C99 this would mean int foo(int a);

+1
source

All Articles