Generally, you should always use the C libraries when possible. One real reason not to is when you are in an embedded environment and are EXTREMELY space limited (which is usually not the case, and virtually all embedded platforms provide C libraries for the platform).
An example may be that using the isalpha
function may actually drag in an object file containing all of the is...
functions and you don't need any of them (the object file is the typical minimum unit when linking although some linkers can go down to individual functions).
By writing your own isalpha
, you can ensure that it, and only it, is incorporated into your final binary.
In some limited cases you may get faster speeds where you have a very specific thing you are wanting to do and the library is handling a more general case. Again, only needed if a particular loop is a bottleneck in the system. You may also want to choose a different speed/space tradeoff than that chosen by the library writer, an example being changing:
int isalpha (int c) {
return ((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z'));
}
into:
int isalpha (int c) {
static int map[256] = {0,0,0,0,...,1,1,1,...,0,0,0};
return map[c & 0xff];
}
a (potentially) faster implementation at the cost of extra storage for the map (and you need to understand your execution environment since it's not portable).
Another reason not to use them is to provide a more secure way with dealing with things like strings where security/robustness is a CRITICAL factor. This will generally cost you a lot more time to prove the correctness.