What is the easiest way to write data to the screen?
The C programming language contains literally hundreds of functions designed to write data to the screen.It can be difficult to decide which of them might be “best” for writing to the screen at a particular time. Many programmers simply pick one or two of the printing functions and use them exclusively. This is an acceptable practice, although it means that the programmer might not alwaysproduce the best possible code.
What a programmer should do is review what each printing function is designed for, and what it does best. Thereafter, whenever he needs to print something to the screen, he can use the function that best suits his needs. He might even create some printing functions of his own.
Learning to correctly use the printing functions contained in the standard C library is part of the first step to becoming a truly proficient programmer. Let’s examine some of the functions in detail.
printf( <format string> , variables );
printf is the most widely used printing function. Some programmers use it exclusively to send text to the screen. Despite this fact, the function was designed only to print formatted text to the screen. In fact, printf is short for “print formatted.” Formatted text is text that contains not just the character string that you placed into your code, but also numbers, characters, and other data dynamically created by your program. Additionally, it can make these appear in a particular way. For instance, it can make real numbers appear with a specific number of digits on either side of the decimal point. For this purpose, the printf function simply cannot be beat!
Why, then, might one choose not to use printf? There are several reasons.
The first reason is that the programmer might want to make what he is doing more clear. Perhaps the programmer is interested only in performing a small subset of the actions provided by the printf function. In this case, he might want to use a specific function that provides just that subset, such as
putchar( char );
This function is designed to send one character to the screen. It is great if this is what you want to do, butit’s not really good for anything else. However, by using this function, you are making it exceedingly clear that what this section of code is doing is sending single characters to the screen.
puts( char * );
This function writes a string of characters to the screen. It does not attempt to accept extra data, as printf does, and it does not process the string that has been passed to it. Again, by using this function, you make it abundantly clear what your code is doing.
The second reason the programmer might choose not to use printf is that he might want to make his code more efficient. The printf function has a lot of overhead; what this means is that it needs to do a great deal of work to perform even a simple operation. It needs to search the string that has been passed to it for format specifiers, it needs to check how many arguments were passed to it, and so forth. The two functions already presented here do not have such overhead. They have the potential for being substantially faster. This fact is not very important for most programs that write data to the screen. It can become an important issue, however, if you are handling large amounts of data from a disk file.
The third reason not to use printf is that the programmer wants to reduce the size of his executable. When you use a standard C function in your program, it must be “linked in.” This means that it must be included into the executable file you are producing. Whereas the code for the simple printing functions, such as putch or puts, is quite small, the code for printf is substantially larger—especially because it might include the other two as a matter of course!
This consideration is probably the least important of those presented so far. Still, if you are using a static linker and you want to keep your executable files small, this can be an important trick. For example, it is very desirable to keep the size of TSRs and some other programs to a minimum. In any case, the programmer should decide which functions he needs to use based on his purposes.
Cross Reference:
None.
No comments:
Post a Comment