Output
Recall from the beginning of this text the demonstration program duplicated below:
#include <stdio.h>
int main(void)
{
printf("Hello, world!\n");
return 0;
}
If you compile and run this program, you will see the sentence below show up on your screen:
Hello, world!
This amazing accomplishment was achieved by using the function printf(). A function is like a "black box" that does something for you without exposing the internals inside. We can write functions ourselves in C, but we will cover that later.
You have seen that to use printf() one puts text, surrounded by quotes, in between the brackets. We call the text surrounded by quotes a literal string (or just a string), and we call that string an argument to printf.
As a note of explanation, it is sometimes convenient to include the open and closing parentheses after a function name to remind us that it is, indeed, a function. However usually when the name of the function we are talking about is understood, it is not necessary.
As you can see in the example above, using printf() can be as simple as typing in some text, surrounded by double quotes (note that these are double quotes and not two single quotes). So, for example, you can print any string by placing it as an argument to the printf() function:
printf("This sentence will print out exactly as you see it...");
And once it is contained in a proper main() function, it will show:
This sentence will print out exactly as you see it...
Printing numbers and escape sequences
Placeholder codes
The printf function is a powerful function, and is probably the most-used function in C programs.
For example, let us look at a problem. Say we don't know what 1905 + 31214 is. Let's use C to get the answer.
We start writing
#include <stdio.h> /* this is important, since printf
can't be used without this line */
(For more information about the line above, see The Preprocessor).
int main(void)
{
printf("1905+31214 is");
return 0;
}
but here we are stuck! printf only prints strings! Thankfully, printf has methods for printing numbers. What we do is put a placeholder format code in the string. We write:
printf("1905+31214 is %d", 1905+31214);
The placeholder %d literally "holds the place" for the actual number that is the result of adding 1905 to 31214.
These placeholders are called format specifiers. Many other format specifiers work with printf. If we have a floating-point number, we can use %f to print out a floating-point number, decimal point and all. An incomplete list is:
* %i - int (same as %d)
* %f - float
* %lf - double
* %s - string
* %x - hexadecimal
Tabs and newlines
What if, we want to achieve some output that will look like:
1905
31214 +
-----
printf will not put line breaks in at the end of each statement: we must do this ourselves. But how?
What we can do is use the newline escape character. An escape character is a special character that we can write but will do something special onscreen, such as make a beep, write a tab, and so on. To write a newline we write \n. All escape characters start with a backslash.
So to achieve the output above, we write
printf(" 1905\n31214 +\n-----\n%d", 33119);
or to be a bit clearer, we can break this long printf statement over several lines. So our program will be:
#include <stdio.h>
int main(void)
{
printf(" 1905\n");
printf("31214 +\n");
printf("-----\n");
printf("%d", 33119);
return 0;
}
There are other escape characters we can use. Another common one is to use \t to write a tab. You can use \a to ring the computer's bell, but you should not use this very much in your programs, as excessive use of sound is not very friendly to the user.
All text is available under the terms of the GNU Free Documentation License
Source : Wikibooks |