Why is the function prototype inside a different function block?

The prototype should be added before the actual function is used for first time. In this case, I do not think its a general practice to have prototype in qsort() function, however, it still serves the purpose. The prototype for swap() could also be added before main() too, don't think it will make a difference.


Placing function prototypes in the definitions of other functions enforces the principle of least privilege by restricting proper function calls to the functions in which the prototypes appear.


You write a function prototype so that the compiler knows that function exists, and can use it. swap() is used inside qsort(), so it must appear before the line it is used. In this case, the swap() prototype is declared inside the qsort() function, but it could as well be declared before the function itself. Or you could define swap() before qsort() and remove the prototype.

Tags:

C

Prototype