Introduction
If you're a C developer, you know that C89 is a standard that laid the foundation for many modern programming practices. However, it's not without its flaws. One such flaw, which persists to this day, concerns the behavior of implicit function declarations. This ambiguity was never resolved, even with the advent of C99, which removed this feature. So why does it matter? Because GCC and Clang, two of the most widely used compilers, don't interpret this feature the same way. Let's take a closer look.
Understanding Implicit Function Declarations in C89
In C89, if you call a function that hasn't been declared, the compiler doesn't immediately throw an error. Instead, it implicitly declares the function as one with unspecified parameters returning an int. For example:
``c int f(int [sizeof(f())]); ``
In this example, f() is called within an array declaration. At this point, f is not yet in scope, so it is implicitly declared as extern int (). Later, f is redeclared with the compatible type int (int *).
The Fun Edge Case
Let's add one character to the previous example:
``c int f(int f[sizeof(f())]); ``
This seems simple, but is it legal? According to Clang, yes, but GCC disagrees and throws an error. To clarify, we need to consult the ANSI X.3-159-1989 standard.
The Crux of the Problem: "Innermost Block"
The C89 standard mentions that if the expression preceding the parentheses in a function call consists solely of an identifier, and no declaration is visible, that identifier is implicitly declared as extern int () in the "innermost block." The question is, what exactly does "innermost block" mean? It could mean block scope or file scope. Depending on the interpretation, the code could be legal or not.
Implications for Developers
This ambiguity may seem minor, but it has real implications for developers maintaining legacy code or working with embedded systems where C89 is still used. Compiler choice can affect the behavior of your code.
Conclusion
While C99 removed implicit function declarations, the ambiguity of C89 persists and continues to influence developers' decisions. Understanding this nuance can help you avoid subtle bugs and choose the right tool for your project.
Let's discuss your project in 15 minutes.