C prototype functions

前端 未结 4 1833
予麋鹿
予麋鹿 2020-12-07 02:21

As a beginner to C, I can understand the need for function prototypes in the file, but am unsure of a couple things.

First, does every function call outside of the m

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-07 03:04

    Others have already pointed out that C doesn't require prototypes for functions. I'll just add a couple minor points.

    First of all, without a prototype, the arguments to a function always undergo "default promotions" before being passed as parameters, so (for example) all the smaller integer types get promoted to int, and float gets promoted to double. Therefore, without a prototype, it's impossible for a function to receive (for example) a char, short, or float. If the function is defined to receive one of those types, there will be a mismatch between what's passed and what the function expects, giving undefined behavior (any other mismatch gives undefined behavior as well).

    Second, if a function is defined with a variable argument list, then you do need a prototype for it, or else calling it will result in undefined behavior.

提交回复
热议问题