Printf c library: The function int printf(const char *format, …); writes a formatted string to stdout. If format contains format specifiers (subsequences beginning with %), the additional arguments following format are inserted after formatting in the resulting string by replacing their respective format specifiers.
Function prototype of printf
int printf(const char *format, ...);
- format : This is a null-terminated string containing the text to be written to stdout. It may contains some embedded format specifiers.
- additional arguments : These arguments will substitute the value of format specifiers in output string.
Format is a null terminated string that contains the string to be written to stdout, it may contains some embedded format specifiers.
The syntax of the format specifier is as follows:
%[flags][width][.precision][length]specifier
Below is the description of each component of format specifier.
Flags
Flags | Description |
---|---|
+ | Forces to preceed the result with a plus or minus sign (- or +) even for positive numbers. By default, only negative numbers are preceded with a ‘-‘ sign. |
– | Left justify within the given field width. Right justification is the default (see width sub-specifier). |
0 | Left padding the numbers with zeroes instead of spaces when padding is specified. |
# | Used with o, x or X specifiers the value to preceeded with 0, 0x or 0X respectively for values other than zero. Used with A, E, F or G it forces the written output to contain a decimal point even if no more digits follow. By default, if no digits follow, no decimal point is written. |
Width Specifier
Width | Description |
---|---|
* | The width is specified as an additional integer value argument preceding the argument that has to be formatted. |
(number) | Minimum number of characters to be printed on console. If the value to be printed is shorter than this number, the result is padded with blank spaces. The value is result is larger than number it is not truncated. |
Precision Specifier
Precision | Description |
---|---|
.* | The precision is specified as an additional integer value argument preceding the argument that has to be formatted. |
.number | For integer specifiers precision specifies the minimum number of digits to be displayed. If the value is shorter than number, the result is padded with leading zeros(0). If the value is longer than number the result will not get truncated. A precision of 0 means that no character is written for the value 0. For a, A, e, E, f and F specifiers: this is the number of digits to be printed after the decimal point (this is 6, by default). For g and G specifiers: This is the maximum number of significant digits to be printed. For s: this is the maximum number of characters to be printed. By default all characters gets printed till null character(‘\0’) is encountered. If the period is specified without an explicit value for precision, 0 is assumed by default. |
Length Specifier
Length Specifier | Description |
---|---|
l | The is interpreted as unsigned long int or long int for integer specifiers (i, d, o, u, x and X), and as a wide character or wide character string for specifiers c and s. |
L | The is interpreted as a long double (only valid for floating point specifiers: e, E, f, g and G). |
h | The argument is a short int or unsigned short int (only applies to integer specifiers: i, d, o, u, x and X). |
Specifier Character
C printf library: Specifier character is the most significant component, since it defines the data type and it’s interpretation.
Specifier Character | Output |
---|---|
i or d | Signed decimal integer(653, -236). |
u | Unsigned decimal integer(7653). |
c | Character (‘T’). |
f | Decimal floating point, lowercase (786.425). |
F | Decimal floating point, uppercase (786.425). |
s | String of characters (For Example : “printf”). |
x | Unsigned hexadecimal integer (3ab2). |
X | Unsigned hexadecimal integer (uppercase) (3AB2). |
e | Scientific notation (mantissa/exponent), lowercase (7.8634e+3). |
E | Scientific notation (mantissa/exponent), uppercase (7.8634E+3). |
g | Use the shortest representation: %e or %f (786.43). |
G | Use the shortest representation: %E or %F (786.43). |
o | Unsigned octal (425). |
p | Pointer address (For Example : b6111111). |
% | A % Character (%). |
n | Nothing printed. |
Return value of printf
On success, this function returns the total number of characters written on stdout otherwise a negative number is returned in case of an error.
C program using printf function
The following program shows the use of printf function to write a formatted string on screen.
#include <stdio.h> int main(){ printf("Printing characters\n"); printf("%c %c %c %c\n\n", 'a', 'A', '#', '1'); printf("Printing integers\n"); printf("%d %ld %10d %010d\n\n", 2015, 2015L, 2015, 2015); printf("Printing floating point numbers\n"); printf("%f %5.2f %+.0e %E\n\n", 1.41412, 1.41412, 1.41412, 1.41412); printf("Printing string\n"); printf("%s\n\n", "TechCrashCourse"); printf("Printing radicals\n"); printf ("%d %x %o %#x %#o\n\n", 2105, 2015, 2015, 2015, 2015); return 0; }
Output
Printing characters a A # 1 Printing integers 2015 2015 2015 0000002015 Printing floating point numbers 1.414120 1.41 +1e+000 1.414120E+000 Printing string TechCrashCourse Printing radicals 2105 7df 3737 0x7df 03737