This function prints the value of LIST interpreted via the format specified by FORMAT to the current output filehandle, or to the one specified by FILEHANDLE.
Effectively equivalent to print FILEHANDLE sprintf(FORMAT, LIST)
You can use print in place of printf if you do not require a specific output format. Following is the list of accepted formatting conversions.
|Sr.No.||Format & Result||1||
A percent sign
A character with the given ASCII code
A signed integer (decimal)
An unsigned integer (decimal)
An unsigned integer (octal)
An unsigned integer (hexadecimal)
An unsigned integer (hexadecimal using uppercase characters)
A floating point number (scientific notation)
A floating point number, uses E instead of e
A floating point number (fixed decimal notation)
A floating point number (%e or %f notation according to value size)
A floating point number (as %g, but using .E. in place of .e. when appropriate)
A pointer (prints the memory address of the value in hexadecimal)
Stores the number of characters output so far into the next variable in the parameter list
Perl also supports flags that optionally adjust the output format. These are specified between the % and conversion letter. They are shown in the following table −
|Sr.No.||Flag & Result||1||
Prefix positive number with a space
Prefix positive number with a plus sign
Left-justify within field
Use zeros, not spaces, to right-justify
Prefix non-zero octal with .0. and hexadecimal with .0x.
Minimum field width
Specify precision (number of digits after decimal point) for floating point numbers
Interpret integer as C-type .long. or .unsigned long.
Interpret integer as C-type .short. or .unsigned short.
Interpret integer as Perl.s standard integer type
Interpret the string as a series of integers and output as numbers separated by periods or by an arbitrary string extracted from the argument when the flag is preceded by *.
Following is the simple syntax for this function −
printf FILEHANDLE FORMAT, LIST printf FORMAT, LIST
Following is the example code showing its basic usage −
#!/usr/bin/perl -w printf "%d\n", 3.1415126; printf "The cost is \$%6.2f\n",499; printf "Perl's version is v%vd\n",%^V; printf "%04d\n", 20;
When above code is executed, it produces the following result −
3 The cost is $499.00 Perl's version is v 0020