The use of printf is to print out a string with no blank fields to be filled.
For example,
printf ("An ordinary string.."); printf ("Testing 1,2,3...");
The next simplest case that has been used before now is to print out a single integer number.
int number = 48; printf ("%d",number);
The two can be combined as shown below −
int number = 48; printf ("Some number = %d",number);
The result of this last example is to print out the following on the screen −
Some number = 48
Here is a list of the different letters for printf −
- d − signed denary integer
- u − unsigned denary integer
- x − hexadecimal integer
- o − octal integer
- s − string
- c − single character
- f − fixed decimal floating point
- e − scientific notation floating point
- g − use f or e, whichever is shorter
Example
Following is the C program for the use of printf conversion characters and types −
/* printf Conversion Characters and Types */ #include <stdio.h> main (){ int i = -10; unsigned int ui = 10; float x = 3.56; double y = 3.52; char ch = ’z’; char *string_ptr = "any old string"; printf ("signed integer %d\n", i); printf ("unsigned integer %u\n",ui); printf ("This is wrong! %u",i); printf ("See what happens when you get the "); printf ("character wrong!"); printf ("Hexadecimal %x %x\n",i,ui); printf ("Octal %o %o\n",i,ui); printf ("Float and double %f %f\n",x,y); printf (" ditto %e %e\n",x,y); printf (" ditto %g %g\n",x,y); printf ("single character %c\n",ch); printf ("whole string -> %s",string_ptr); }
Output
When the above program is executed, it produces the following result −
signed integer -10 unsigned integer 10 This is wrong! 4294967286See what happens when you get the character wrong!Hexadecimal fffffff6 a Octal 37777777766 12 Float and double 3.560000 3.520000 ditto 3.560000e+000 3.520000e+000 ditto 3.56 3.52 single character z whole string -> any old string