Why use function prototypes?

前端 未结 3 1886
慢半拍i
慢半拍i 2021-02-07 13:48

Why use function prototypes in C? It seems sort of redundant because we already declare the function name, argument types, and return type in the definition. Do the prototypes h

3条回答
  •  北海茫月
    2021-02-07 14:16

    With the declaration of a function the compiler can check the consistent use of parameters and return value, and can compile the code even if the function is not implemented in this module. If the function is only declared but not implemented in the respective module, this gap will be closed by the linker, not the compiler.

    It's similar to declaring extern variables. If you'd define them, the memory for them would be allocated multiple times. That's why you should never define variables in h-files, but declare them there. Including the h-file would result in multiple allocations of memory.

提交回复
热议问题