Default argument promotions in C function calls

前端 未结 3 482
小蘑菇
小蘑菇 2020-11-22 05:15

Setup

I have a few questions about the default argument promotions when calling a function in C. Here\'s section 6.5.2.2 \"Function calls\" Paragraphs 6, 7, and 8

3条回答
  •  忘了有多久
    2020-11-22 05:58

    Upvoted AProgrammer's answer—those are the real goods.

    For those of you who are wondering why things are this way: in the dark ages before 1988, there was no such thing as a function prototype in classic "K&R" C, and the default argument promotions were instituted because (a) there were essentially "free", as it costs no more to put a byte in a register than to put a word in a register, and (b) to cut down on potential errors in parameter passing. That second reason never quite cut it, which was why the introduction of function prototypes in ANSI C was the single most important change ever in the C language.

    As to when default promotions kick in: default argument promotions are used exactly when the expected type of the argument is unknown, which is to say when there's no prototype or when the argument is variadic.

提交回复
热议问题