Why should I use standard library functions instead of writing my own?

The standard library functions have three advantages: they work, they’re efficient, and they’re portable. They work: Your compiler vendor probably got them right. More important, the vendor is likely to have done a thorough test to prove they’re right, more thorough than you probably have time for. (There are expensive test suites to make that job easier.)

They’re efficient: Good C programmers use the standard library functions a lot, and good compiler vendors know that. There’s a competitive advantage for the vendor to provide a good implementation. When competing compilers are compared for efficiency, a good compiler implementation can make all the difference. The vendor has more motivation than you do, and probably more time, to produce a fast implementation.

They’re portable: In a world where software requirements change hourly, the standard library functions do the same thing, and mean the same thing, for every compiler, on every computer. They’re one of the few things you, as a C programmer, can count on.

The funny thing is, one of the most standard pieces of information about the standard library is hard to find. For every function, there’s one header file (or, rarely, two) that guarantees to give you that function’s prototype. (You should always include the prototype for every function you call;) What’s funny? That header file might not be the file that actually contains the prototype. In some (sad!) cases, it’s not even the header file recommended by the compiler manual. The same is true for macros, typedefs, and global variables.

Tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.