Two or more incompatible declarations of the same function or object that appear in the same program shall be diagnosed because they result in undefined behavior.
The C Standard [ISO/IEC 9899:2011] identifies three distinct situations in which undefined behavior (UB) may arise as a result of incompatible declarations of the same function or object:
...
In this noncompliant code example, variable i
is declared to have type int
in file a.c
but defined to be of type short
in file b.c
. The declarations are incompatible, resulting in undefined behavior undefined behavior 15. Furthermore, accessing the object using an lvalue of an incompatible type as done in function f()
results in undefined behavior 37 with possible observable results ranging from unintended information exposure to memory overwrite to a hardware trap.
...
In this noncompliant code, the variable a
is declared to have array type in file a.c
but defined to have pointer type in file b.c
. The two declarations are incompatible, resulting in undefined behavior 15. As before, accessing the object in function f()
results in undefined behavior 37 with the typical effect of triggering a hardware trap.
...
In this noncompliant code example, function f()
is declared in file a.c
with one prototype but defined in file b.c
with another. The two prototypes are incompatible, resulting in undefined behavior 15. Furthermore, invoking the function results in undefined behavior 41 with typically catastrophic effects.
...
In this noncompliant code example, the length of the identifier declaring the function pointer bash_groupname_completion_function
in file bashline.h
exceeds by 4 the minimum implementation limit of 31 significant initial characters in an external identifier, introducing the possibility of colliding with the bash_groupname_completion_func
integer variable defined in file b.c
, which is exactly 31 characters long. On an implementation that exactly meets this limit, the behavior of the program is undefined (see undefined behavior 15. In addition, invoking the function leads to to undefined behavior behavior 41 with typically catastrophic effects.
...
Code Block | ||||
---|---|---|---|---|
| ||||
/* a.c: */ int x = 0; /* the definition */ /* b.c: */ extern char x; /* incompatible declaration */ /* but no other references to 'x' */ |
Related Guidelines
Bibliography
[Hatton 1995] | Section 2.8.3 |
---|---|
[ISO/IEC 9899:2011] | Section 6.7.6.2, "Array |
...
Declarators," and section 6.2.2, "Linkages of |
...
Bibliography
...
Identifiers" |
...