When you want to print an expression to an output device, you must
specify the type of the expression and how you want it to be formatted.
Conversion specifications are the symbols you use in the printf() control
string to do this. Each expression whose value you want to output must
have a corresponding conversion specification in the control string.
Conversion specifications for printf() and their uses:
| Conversion Specification |
Output |
| %c |
character |
| %s |
string of characters |
| %d or
%i |
decimal integer |
| %e |
floating point number in e-notation |
| %f |
floating point number in decimal notation |
| %g |
uses %f or %e whichever is shorter |
| %p |
pointer |
| %u |
unsigned decimal integer |
| %o |
octal integer |
| %x |
hexadecimal integer, using lower case |
| %X |
hexadecimal integer, using upper case |
| %% |
Prints a % sign |
Here is an example of printf() using conversion specifications and another notation:
#include
void main()
{
int numa, numb;
numa = 1;
numb = 2;
printf("this is ");
printf("on %d line", numa);
printf(" even though we used more than 1 printf() function\n");
printf("However, This \nwill be on %d lines", numb);
}
Output:
this is on 1 line even though we used more than 1 printf() function
However, This
will be on 2 lines
You may notice that the "\n" caused a line break. This is called an escape sequence. The following is a
list of the escape sequences:
| Escape Sequences |
Meaning |
| \n |
New break |
| \a |
Alert. Plays a system beep |
| \b |
Backspace |
| \f |
Form feed |
| \r |
Carriage return |
| \t |
Horizontal tab |
| \v |
Vertical tab |
| \\ |
Prints a \ |
| \' |
prints a ' |
| \" |
prints a " |
| \0oo |
Prints an octal value (o represents an octal digit) |
| \xhh |
Prints a hexadecimal value (h represents a hex digit) |