Guides:C/C Crash Course/Standard Output (stdout)
From CoderGuide
Contents |
Standard Output (stdout)
printf(char *format, ...)
You've already seen the printf() function in action, so lets explain it in more detail. printf() allows you to output formatted, and unformatted text to the screen, and will convert numbers into printable text for you. The printf() function has a very special definition which allows you to specify as many, and whatever type, of variable after the initial formatting string. That formatting string, which you provide, tells printf() what the other parameters you passed it are, along on how to display them.
The format string can have whatever text in it you like, but every time it comes across the percent sign, %, it treats it as an escape character. The characters that follow the escape character tell printf() what type of data to display (another string, a single character, or a number), and how to display it. If you want to just display a percent sign by it's self, you have to give two percent signs as so to display just one: %%. A letter following a percent sign indicates what type of data is to be displayed (s for string, d for signed integer, etc).
A positive number x between the percent sign, and the letter specifying the data type, indicates that the text should be right justified to fit in a minimum x characters. For left justification, you can specify a padding character (like period, star, and many others) to use between the percent sign and the number x (you can not do this with a negative number). A negative number indicates right justification (what you normally see on a printed page). Confused? Yeah, it took me a while to figure it out too.
Here's a partial list of escape codes, and what they do, followed by an example:
| Code | Function |
|---|---|
| %s | display a string |
| %d or %i | display a signed integer |
| %u | display an unsigned integer |
| %ld | display a signed long integer |
| %lu | display an unsigned long integer |
| %lld | display a signed long long integer |
| %h | display a short integer |
| %hu | display an unsigned short integer |
| %f | display a float or double |
| %L | display a long double |
| %dx | Display an int in hexadecimal (lower case) |
| %dX | display an int in hexadecimal (upper case) |
| %e | display a float in scientific notation |
| %E | same as above, but the 'e' is upper case |
| %'.2f | display a float, rounding up to the nearest 1/100th place |
| %5.2f | Same as above, but also left justify to fit in a minimum space of 5 characters |
| %8d | right justify an int to fit in 8 characters |
| %08d | Same as above, but pad space with zeros |
| % 8d | Same as above, but pad with spaces |
| %-10d | Left justify the number to fit in 10 characters |
| %% | displays a single percent sign |
#include <stdio.h> int main(void){ long num1=12345; long num2=123456789; double num3=12345.6789; double num4=123456789.123456789; char *string="Hello!"; printf("\n123456789+123456789+123456789+123456789\n"); printf("%9d % 13d\n",num1,num2); printf("%0-13d%08d\n",num2,num1); printf("%15s\n",string); printf("%f %'.2f %e %'.4e\n",num3,num3,num4,num4); }
And it's output should look like this:
123456789+123456789+123456789+123456789
12345 123456789
123456789 00012345
Hello!
12345.678900 12345.68 1.234568e+08 1.2346e+08
puts(char *)
puts() prints a single string to the stdout, along with a newline character (so we don't need to add that '\n' escape code when printing). And, all you have to do to use it is this:
#include <stdio.h> int main(){ char *s="World!" puts("Hello"); puts(s); }
putchar()
Prints a single character to a string. Nothing fancy. Here's an example:
#include <stdio.h> int main(){ char *s="World!" int i; /*print all the characters in the string, until the end is reached, as indicated by the null-terminator which is zero, or \0)*/ for (i=0;s[i];i++) putchar(s[i]); putchar('\n'); }

