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; see FAQ VIII.2.) 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.
To get the “right” header file, look up the function in a copy of the ANSI/ISO C standard. If you don’t have
a copy of the standard handy, use Table XII.2, shown in the next FAQ.
Cross Reference:
VIII.2: Why should I prototype a function?
XII.2: What header files do I need in order to define the standard library functions I use?
No comments:
Post a Comment