cong cong - 1 month ago 8
C Question

Advanced Programming in the Unix Environment 3rd Edn, §7.8, the default return value for undeclared functions is int?

In APUE 3rd Edn, §7.8, Memory Allocation, I read about this:


Because the three alloc functions return a generic void * pointer, if we #include (to obtain the function prototypes), we do not explicitly have to cast the pointer returned by these functions when we assign it to a pointer of a different type. The default return value for undeclared functions is int, so using a cast without the proper function declaration could hide an error on systems where the size of type int differs from the size of a function’s return value (a pointer in this case).


My question is: Can we use a undeclared function? I think we couldn't, then why is "the default return value for undeclared functions is int"? I'm totally confused...

Answer

C89/C90 did not require functions to be pre-declared; C99 and C11 do. The book Advance Programming in the Unix Environment, 3rd Edn was presumably last updated before C99 became prevalent.

Or, this section was not updated since C99 became prevalent — it appears that the third edition was published in 2013, which is after C99 became prevalent, though C11 was borderline at the time. (My 2nd Edn is from 2005, but §7.8 doesn't have the "default return value for undeclared functions is int" sentence. So, maybe that is an addition in the 3rd Edn, in which case, it is less than clear why it was added other than to cause confusion.)

Regardless of the book's update history, it remains accurate that C90 did not require functions to be declared before use, and such undeclared functions were assumed to return an int. Since C99, strict compliance with the C standard requires all functions to be declared before they are used.


There were systems on Motorola M680x0 chips where functions returned addresses in the An (address) registers, but they returned numbers in the Dn (data) registers (usually A0 and D0). On such systems, not declaring a function correctly could end up with the code looking in the wrong register for the returned value.

Comments