The C library is perhaps the most widely reused library in the world. However, it has numerous flaws and inconsistencies, from which we can learn.
Design is hard, and API design is harder. This is because applications and systems are designed to be used, while in the case of APIs, we design them not just to be used but also reused. Applications are designed for end customers to use; however, with the APIs in libraries, platforms and frameworks, the end user is another programmer who wants to get the work done via the API. There are more things to consider in API design than with application design. For example, compatibility issues (such as backward compatibility) are an additional concern for an API designer. In frameworks and libraries, fixLnJ D buJ Ls GLIfiFulW whHn FoPSDrHG Wo fixLnJ WhH sDPH bug in a stand-alone application. For example, if the bug fix LnYolYHs D FhDnJH Wo WhH SublLF LnWHrIDFH HxSosHG by WhH API, then the existing applications written based on that interface will fail. Hence, the evolution of an API is harder than evolving applications.
C is used in a wide range of devices, ranging from microwave ovens to air- planes. With the popularity of the C language and its widespread use, APIs in the C library have been used by millions of programmers worldwide over the last four decades. The C library has had a huge impact on the design of libraries in languages that arrived later. For example, the ‘ format string’ approach is not type- safe, but for formatted output, it is a very convenient approach. The C++ language provides a type- safe alternative to formatted output, but that is not as convenient as the C ‘ format style’ approach to use. The Java language’s println approach is type- safe, but is not convenient. Hence, Java introduced printf with the ‘ format string’ approach in its library. Similarly, C# has format strings, and only its format specifier is slightly different; instead of the B symbol, it uses {} symbols. This is just one example of the influence of C library design on languages that came up later.
Given the influence and widespread use it enjoyed, it is surprising to find that the C library has numerous flaws and mistakes! So, it would be interesting for us to understand these mistakes and learn from them, so that we can avoid them in future.
NoWH WhH orGHr oI WhH firsW Wwo DrJuPHnWs Ior WhHsH. As you can see, the order is interchanged. As a programmer, you should remember which the source string is and which the destination string is, because if you get confused, you may introduce a subtle bug by copying the string from the destination to the source! That is why consistency is an important factor in API design, and many C library functions are inconsistent.