printf() function in C There are many applications when programming. Here's what you need to know about printf() in C.
Programming is currently one of the “hot” industries today. Therefore, the need to learn about programming languages is increasing. If you are just starting to learn programming, you need to know that there are many languages available to develop an application, program or software. To successfully create a program, you need to master how to use the commands and functions of a language.
C is one of the popular programming languages and its printf function is used quite a lot. In C language, printf function is used to print output on the screen. This function is part of the C standard library “stdio.h” and it can allow formatting the result in many ways.
If there are specified formats, they are replaced by their respective arguments in cstring to the printf call. These format specifiers may also contain length, precision, and other flags.
Declare printf() function in C
Below is the declaration for the printf() function in the C programming language:
int printf(const char *format, ...)
Printf() function parameters in C
format: This is the string containing the text written to stdout. It may contain format tags that can be embedded which are replaced by values defined in subsequent additional parameters and formatted as required. The prototype of format tags is %[flags][width][.precision][length]specifieris explained as below:
specifier | Result |
---|---|
c | Characters |
d or i | Signed decimal integer |
e | Scientific notation (mantissa/exponent) uses the letter e |
E | Scientific notation (mantissa/exponent) uses the letter E |
f | Decimal floating point numbers |
g | Use shortened %e or %f |
G | Use shortened %E or %f |
o | Signed octal number |
S | Character string |
u | Unsigned decimal integer |
x | Unsigned hexadecimal integer |
X | Unsigned hexadecimal integer (uppercase letters) |
p | Pointer address |
n | Don't print anything |
% | Characters |
flags. flags | Describe |
---|---|
– | Align left inside the given field width. Alignment should be default |
+ | Force prefixing the result with a plus or minus sign (+ or -) even for positive numbers. By default, only negative numbers are preceded by a – |
(space) | If no symbols are written, a space is inserted before the value |
# | Used with specifiers o, x or accepts a decimal pointer even if no digits follow. By default, if no digits follow, no decimal pointer is written. Used with g or G, the result is the same as e or E but the trailing zeros are not removed |
0 | Pad the left-pad of the number with zeros instead of spaces |
width | Describe |
---|---|
(number) | Minimum number of characters to be printed. If the value to be printed is shorter than this number, the result is padded with spaces. The value is not clipped even if the result is too large |
* | Width is not specified in the format string, but as an additional integer value parameter placed before that parameter that must be formatted |
.precision | Describe |
---|---|
.number | With Integer Specifiers (d, i, o, u, x, X) − Precision determines the smallest number of digits recorded. If the value recorded is shorter than this number, the result is padded with leading zeros. The value is not truncated even if the result is longer. A precision of 0 means no characters are recorded for the value 0. For e, E and f specifier: this is the number of digits to be printed after the decimal point. With g and G specifier: this is the maximum number of significant digits to be printed. With s specifier: this is the minimum number of characters to be printed. By default, all characters are printed until the last null character is encountered. With type c: it has no effect. When no precision is specified, the default is 1. If period is specified without an explicit precision value, 0 is assumed |
.* | Precision is not specified in the format string, but as an additional integer value parameter placed before that parameter that must be formatted |
length. length | Describe |
---|---|
h | The parameter is interpreted as a short int or unsigned short int (only applies to integer specifiers: i, d, o, u, x and X) |
l | The parameter is interpreted as a long int or unsigned long int for integer specifiers (i, d, o, u, x and X), and as a wide char or wide char string for specifiers c and s |
L | Parameter is interpreted as a long double (only applies to floating point specifiers: e, E, f, g and G) |
Additional parameters − Depending on the format string, this function may have a series of additional parameters, each containing a value to be inserted in place of each %-tag specified in the format parameter, if any. This number of parameters should be the same number as the number of %-tags that expect a value.
Return value:
If successful, the total number of characters written is returned. If it fails, a negative number is returned.
Example 1:
The following C program illustrates the use of the printf() function in C:
#include
int main ()
{
int ch;
for( ch = 75 ; ch
Compiling and running the above C program will produce the following results:
Gia tri ASCII = 75, Ky tu = K
Gia tri ASCII = 76, Ky tu = L
Gia tri ASCII = 77, Ky tu = M
Gia tri ASCII = 78, Ky tu = N
Gia tri ASCII = 79, Ky tu = O
Gia tri ASCII = 80, Ky tu = P
Gia tri ASCII = 81, Ky tu = Q
Gia tri ASCII = 82, Ky tu = R
Gia tri ASCII = 83, Ky tu = S
Gia tri ASCII = 84, Ky tu = T
Gia tri ASCII = 85, Ky tu = U
Gia tri ASCII = 86, Ky tu = V
Gia tri ASCII = 87, Ky tu = W
Gia tri ASCII = 88, Ky tu = X
Gia tri ASCII = 89, Ky tu = Y
Gia tri ASCII = 90, Ky tu = Z
Gia tri ASCII = 91, Ky tu = [
Gia tri ASCII = 92, Ky tu = \
Gia tri ASCII = 93, Ky tu = ]
Gia tri ASCII = 94, Ky tu = ^
Gia tri ASCII = 95, Ky tu = _
Gia tri ASCII = 96, Ky tu = `
Gia tri ASCII = 97, Ky tu = a
Gia tri ASCII = 98, Ky tu = b
Gia tri ASCII = 99, Ky tu = c
Gia tri ASCII = 100, Ky tu = d
Example 2:
To output values or print text in C, here is a simple example of how to use the printf() function:
#include
int main() {
printf("Hello World!");
return 0;
}
You can use as many printf() functions as you want. However, note that it does not insert a newline at the end of the output:
#include
int main() {
printf("Xin chào bạn tới Quantrimang.com.");
printf("Cùng nhau học C nhé!");
return 0;
}
Nesting printfs in C
You can nest printf within printf when programming in C language. For example, predict the result of the following C program with printf inside printf.
#include
int main()
{
int x = 1987;
printf("%d", printf("%d", printf("%d", x)));
return(0);
}
Result:
198741
Detailed explanation:
First, the innermost printf is run so the print result you get is 1987.
Print returns the total number of digits in the year 1987 i.e. 4. printf() returns the number of characters successfully printed on the screen. The entire order reduces to:
printf("%d", printf("%d", 4));
The second printf then prints 4 and returns the total number of digits in 4. For example: 1 (4 is the number of single digits).
In the end, the entire command simply reduces to:
printf("%d", 1);
It prints only 1 and the result will be: 198741
Therefore, when multiple printfs appear inside another printf, the inner printf prints its output and returns the length of the string printed on the screen to the outer printf.
In short, in C language programming, the printf() function is used to print (“character, string, float value, integer, octal, and hexadecimal”) to the output screen.
- printf() function with format %d to display the value of an integer variable.
- Similarly, %c is used to display characters, %f for float variables, %s for string variables, %lf for double variables, and %x for hexadecimal variables.
- To create a new line, we use the “\n” statement in C printf().