خرید بک لینک

Vote count: 0

Does this iocent looking program invoke undefined behavior:

int main(void) {
    printf("%dn", 1);
    retu 0;
}

asked 36 secs ago

1 Answer

Vote count: 0

Yes invoking printf() without a proper prototype from standard header <stdio.h> invokes undefined behavior.

As documented in C11 Aex J (informative only)

J2 Undefined Behavior

  • For call to a function without a function prototype in scope where the function is defined with a function prototype, either the prototype ends with an ellipsis or the types of the arguments after promotion are not compatible with the types of the parameters (6.5.2.2).

This aex is not normative, but clearly documents the above code as an example of undefined behavior.

In more pragmatic words, in the absence of a prototype for printf, the compiler generates the calling sequence as if printf was defined as int printf(const char*, int); which may be quite different and incompatible with the actual implementation of printf in the standard library.

Ancient ABIs were regular enough that this would not cause a problem, but mode (eg 64-bit) ABIs use more efficient calling sequences that make the above code definitely incorrect.

answered 36 secs ago

برچسب: نویسنده: استخدام کار تاريخ: دوشنبه 11 مرداد 1395 ساعت: 0:29

صفحه بندی