Play Video
1
Printf format string
Printf format string
::2014/08/18::
Play Video
2
RUBY Formatting strings with SPRINTF and PRINTF
RUBY Formatting strings with SPRINTF and PRINTF
::2013/10/13::
Play Video
3
Printf and String format Guide
Printf and String format Guide
::2014/02/14::
Play Video
4
06 Using The printf Command To Format A String
06 Using The printf Command To Format A String
::2014/05/28::
Play Video
5
Print Formatting: printf() Precision Part 3 (Java)
Print Formatting: printf() Precision Part 3 (Java)
::2013/10/30::
Play Video
6
Learn Java Tutorial for Beginners, Part 20: StringBuilder and String Formatting
Learn Java Tutorial for Beginners, Part 20: StringBuilder and String Formatting
::2012/05/05::
Play Video
7
Perl printf & arrays
Perl printf & arrays
::2012/01/11::
Play Video
8
C Programming Tutorial 12 scanf printf char data types
C Programming Tutorial 12 scanf printf char data types
::2011/05/18::
Play Video
9
Format string kungfu via GOT table overwrite
Format string kungfu via GOT table overwrite
::2007/11/29::
Play Video
10
C Programming Tutorial: Input Output (printf, scanf, format specifiers) Lesson 1
C Programming Tutorial: Input Output (printf, scanf, format specifiers) Lesson 1
::2012/04/22::
Play Video
11
Print Formatting: printf() Conversion Type Characters Part 1 (Java)
Print Formatting: printf() Conversion Type Characters Part 1 (Java)
::2013/10/29::
Play Video
12
Java: Classes, Wrapper classes, Autoboxing, Print formatting
Java: Classes, Wrapper classes, Autoboxing, Print formatting
::2013/04/18::
Play Video
13
Java Tutorial 8 - Formatting Output with printf
Java Tutorial 8 - Formatting Output with printf
::2013/12/09::
Play Video
14
C Programming Tutorial: Input Output (printf, scanf, format specifiers) Lesson 2
C Programming Tutorial: Input Output (printf, scanf, format specifiers) Lesson 2
::2012/04/23::
Play Video
15
How-to: printf and scanf Basics
How-to: printf and scanf Basics
::2009/08/21::
Play Video
16
Strings and printf - Python: Tutorial 4
Strings and printf - Python: Tutorial 4
::2014/06/07::
Play Video
17
C Programming 17 - Using printf to format numbers
C Programming 17 - Using printf to format numbers
::2014/04/17::
Play Video
18
Daniel
Daniel's quick start guide to C programming #12 (Printf placeholders formatting)
::2013/06/08::
Play Video
19
[FuzzySecurity] Format String Exploitation - Part1
[FuzzySecurity] Format String Exploitation - Part1
::2013/04/07::
Play Video
20
Java: Rounding Numbers (Math.round(), DecimalFormat & printf)
Java: Rounding Numbers (Math.round(), DecimalFormat & printf)
::2013/04/13::
Play Video
21
Print Formatting: printf() Width Part 4 (Java)
Print Formatting: printf() Width Part 4 (Java)
::2013/10/30::
Play Video
22
Formatting Output with printf in Java
Formatting Output with printf in Java
::2014/08/19::
Play Video
23
memviewer part4 - printing a c-string with printf
memviewer part4 - printing a c-string with printf
::2012/09/08::
Play Video
24
C Programming Tutorial 13 scanf printf char* or String pointer data types
C Programming Tutorial 13 scanf printf char* or String pointer data types
::2011/05/18::
Play Video
25
Introduction to format string vulnerabilities
Introduction to format string vulnerabilities
::2013/04/30::
Play Video
26
Print Formatting: printf() Flag - Part 5 (JAVA)
Print Formatting: printf() Flag - Part 5 (JAVA)
::2013/10/31::
Play Video
27
Notation, Precision and Formatting with printf() in Java - Part 2
Notation, Precision and Formatting with printf() in Java - Part 2
::2010/07/18::
Play Video
28
Java Programming - Format String - Part 18
Java Programming - Format String - Part 18
::2012/08/27::
Play Video
29
Format String Vulnerabilities Primer (Part 1 The Basics)
Format String Vulnerabilities Primer (Part 1 The Basics)
::2012/03/09::
Play Video
30
Notation, Precision and Formatting with printf() in Java - Part 1
Notation, Precision and Formatting with printf() in Java - Part 1
::2010/07/18::
Play Video
31
OCAML Tutorial 25/33: Formatted Output with OCAML Printf
OCAML Tutorial 25/33: Formatted Output with OCAML Printf
::2014/06/05::
Play Video
32
IST297D Formatting Output in Java with the printf statement
IST297D Formatting Output in Java with the printf statement
::2013/03/18::
Play Video
33
78.Formatted Output Printf PT1
78.Formatted Output Printf PT1
::2014/02/10::
Play Video
34
79.Formatted Output Printf PT2
79.Formatted Output Printf PT2
::2014/02/10::
Play Video
35
Print Formatting:: printf() Flag space Part 7 (JAVA)
Print Formatting:: printf() Flag space Part 7 (JAVA)
::2013/10/31::
Play Video
36
Print Formatting: printf() Flag + Part 6 (JAVA)
Print Formatting: printf() Flag + Part 6 (JAVA)
::2013/10/31::
Play Video
37
[Fuzzy Security] - Format String Exploitation - Part 2
[Fuzzy Security] - Format String Exploitation - Part 2
::2013/04/07::
Play Video
38
Robert Stewart: Spirit.Qi in the Real World
Robert Stewart: Spirit.Qi in the Real World
::2013/10/29::
Play Video
39
Format String
Format String
::2013/02/26::
Play Video
40
Java - printf
Java - printf
::2011/04/14::
Play Video
41
Using return methods and String.format
Using return methods and String.format
::2011/09/25::
Play Video
42
Java Tutorial 3 - Strings and Formatting
Java Tutorial 3 - Strings and Formatting
::2012/05/26::
Play Video
43
Java Lesson 27 - String Class - Part 10 - format Method
Java Lesson 27 - String Class - Part 10 - format Method
::2011/10/18::
Play Video
44
Libraries, clients and printf
Libraries, clients and printf
::2013/03/09::
Play Video
45
Display formated number in decimal, octal and hexadecimal using String.format
Display formated number in decimal, octal and hexadecimal using String.format
::2014/07/28::
Play Video
46
Java Basics - printf
Java Basics - printf
::2013/01/03::
Play Video
47
50 C Language   Language   String Formats
50 C Language Language String Formats
::2014/03/18::
Play Video
48
Beginning Java 5 - Printf
Beginning Java 5 - Printf
::2013/01/02::
Play Video
49
08 04 Formatted Output Printf PT2
08 04 Formatted Output Printf PT2
::2011/07/17::
Play Video
50
08.03.Formatted Output-Printf PT1
08.03.Formatted Output-Printf PT1
::2009/10/23::
NEXT >>
RESULTS [51 .. 101]
From Wikipedia, the free encyclopedia
  (Redirected from Printf)
Jump to: navigation, search
An example of the printf function.

Printf format string (of which "printf" stands for "print formatted") refers to a control parameter used by a class of functions in the string-processing libraries of various programming languages. The format string is written in a simple template language, and specifies a method for rendering an arbitrary number of varied data type parameters into a string. This string is then by default printed on the standard output stream, but variants exist that perform other tasks with the result, such as returning it as the value of the function. Characters in the format string are usually copied literally into the function's output, as is usual for templates, with the other parameters being rendered into the resulting text in place of certain placeholders – points marked by format specifiers, which are typically introduced by a % character, though syntax varies. The format string itself is very often a string literal, which allows static analysis of the function call. However, it can also be the value of a variable, which allows for dynamic formatting but also a security vulnerability known as an uncontrolled format string exploit.

The term "printf" is due to the C language, which popularized this type of function, but these functions predate C, and other names are used, notably "format". Printf format strings, which provide formatted output (templating), are complementary to scanf format strings, which provide formatted input (parsing). In both cases these provide simple functionality and fixed format compared to more sophisticated and flexible template engines or parsers, but are sufficient for many purposes.

Timeline[edit]

Many programming languages implement a printf function to output a formatted string. It originated from the C programming language, where it has a prototype similar to the following:

int printf(const char *format, ...);

The string constant format provides a description of the output, with placeholders marked by "%" escape characters, to specify both the relative location and the type of output that the function should produce. The return value yields the number of printed characters.

FORTRAN 66[edit]

Fortran's variadic PRINT statement referenced a non-executable FORMAT statement.

      PRINT 601, 123456, 1000.0, 3.1415, 250
  601 FORMAT (8H RED NUM,I7,4H EXP,E8.1,5H REAL,F5.2,4H VAL,I4)

will print the following (on a new line, because of the leading blank character):[1]

RED NUM 123456 EXP 1.0E 03 REAL 3.14 VAL 250

COBOL[edit]

COBOL provided formatting via hierarchical data structure specification:

   01 out-rec.
   02 out-name   picture x(20).
   02 out-amount picture  $9,999.99.

...

    move me to out-name.
    move amount to out-amount.
    write out-rec.

1960s: BCPL, ALGOL 68, Multics PL/I[edit]

C's variadic printf has its origins in BCPL's writef function.

ALGOL 68 Draft and Final report had the functions inf and outf, subsequently these were revised out of the original language and replaced with the now more familiar readf/getf and printf/putf.

 printf(($"Color "g", number1 "6d,", number2 "4zd,", hex "16r2d,", float "-d.2d,", unsigned value"-3d"."l$,
             "red", 123456, 89, BIN 255, 3.14, 250));

Multics has a standard function called ioa_ with a wide variety of control codes. It was based on a machine-language facility from Multics's BOS (Bootstrap Operating System).

call ioa_ ("Hello, ^a", "World!");

1970s: C, Lisp[edit]

 printf("Color %s, number1 %d, number2 %05d, hex %#x, float %5.2f, unsigned value %u.\n",
        "red", 123456, 89, 255, 3.14159, 250);

will print the following line (including new-line character, \n):

Color red, number1 123456, number2 00089, hex 0xff, float  3.14, unsigned value 250.

The printf function returns the number of characters printed, or a negative value if an output error occurs.

Common Lisp has the extremely powerful format function.

 (format t "~{~a~^, ~}!" '(hello world))
 ;; ⇒ "HELLO, WORLD!"

prints "Hello, World!" on the standard output stream. If the first argument is nil, format returns the string to its caller. The first argument can also be any output stream. format was introduced into ZetaLisp at MIT in 1978, based on the Multics ioa_, and was later adopted into the Common Lisp standard. Scheme incorporated Common Lisp-style format in SRFi-28 and SRFi-54.[2]

1980s: Perl, Shell[edit]

Perl also has a printf function. Common Lisp has a format function which acts according to the same principles as printf, but uses different characters for output conversion. The GLib library contains g_print, an implementation of printf.

Some Unix systems have a printf program for use in shell scripts; many derive this from the GNU Core Utilities suite. This can be used instead of echo in situations where the latter is not portable. For example:

echo -n -e "$FOO\t$BAR"

may be rewritten portably as:

printf "%s\t%s" "$FOO" "$BAR"

1990s: PHP, Python[edit]

1995: PHP also has the printf function, with the same specifications and usage as that in C/C++. MATLAB does not have printf, but does have its two extensions sprintf and fprintf which use the same formatting strings. sprintf returns a formatted string instead of producing a visual output.

1991: Python's % operator hearkens to printf's syntax when interpolating the contents of a tuple.[3] This operator can, for example, be used with the print function:

print("%s\t%s" % (foo,bar))

Version 2.6 of Python added the more versatile str.format() method:

print("If you multiply five and six you get {0}.".format(5*6))

It's easy to create a C language-like printf() function[4] in either Python 2.x or 3.x:

import sys
def printf(format, *args):
    sys.stdout.write(format % args)
 
printf("%d x %d is %d\n", 6, 7, 6*7)

2000s: Java[edit]

2004: Java supported printf from version 1.5 onwards as a member of the PrintStream[5] class, giving it the functionality of both the printf and fprintf functions. At the same time sprintf-like functionality was added to the String class by adding the format(String, Object... args) method.[6]

// Write "Hello, World!" to standard output (like printf)
System.out.printf("%s, %s", "Hello", "World!");
// create a String object with the value "Hello, World!" (like sprintf)
String myString = String.format("%s, %s", "Hello", "World!");

Unlike most other implementations, Java's implementation of printf throws an exception on encountering a malformed format string.

Format placeholders[edit]

Formatting takes place via placeholders within the format string. For example, if a program wanted to print out a person's age, it could present the output by prefixing it with "Your age is ". To denote that we want the integer for the age to be shown immediately after that message, we may use the format string:

"Your age is %d."

The syntax for a format placeholder is

%[parameter][flags][width][.precision][length]type

Parameter can be omitted or can be:

Character Description
n$ n is the number of the parameter to display using this format specifier, allowing the parameters provided to be output multiple times, using varying format specifiers or in different orders. If any single placeholder specifies a parameter, all the rest of the placeholders MUST also specify a parameter. This is a POSIX extension and not in C99. Example: printf("%2$d %2$#x; %1$d %1$#x",16,17) produces

"17 0x11; 16 0x10"

Flags can be zero or more (in any order) of:

Character Description
+ always denote the sign '+' or '-' of a number (the default is to omit the sign for positive numbers). Only applicable to numeric types.
space prefixes non-negative signed numbers with a space
- left-align the output of this placeholder (the default is to right-align the output).
# Alternate form. For 'g' and 'G', trailing zeros are not removed. For 'f', 'F', 'e', 'E', 'g', 'G', the output always contains a decimal point. For 'o', 'x', and 'X', a 0, 0x, and 0X, respectively, is prepended to non-zero numbers.
0 use 0 instead of spaces to pad a field when the width option is specified. For example, printf("%2d", 3) results in " 3", while printf("%02d", 3) results in "03".

Width specifies a minimum number of characters to output, and is typically used to pad fixed-width fields in tabulated output, where the fields would otherwise be smaller, although it does not cause truncation of oversized fields. A leading zero in the width value is interpreted as the zero-padding flag mentioned above, and a negative value is treated as the positive value in conjunction with the left-alignment "-" flag also mentioned above.

Precision usually specifies a maximum limit on the output, depending on the particular formatting type. For floating point numeric types, it specifies the number of digits to the right of the decimal point that the output should be rounded. For the string type, it limits the number of characters that should be output, after which the string is truncated.

Length can be omitted or be any of:

Character Description
hh For integer types, causes printf to expect an int-sized integer argument which was promoted from a char.
h For integer types, causes printf to expect an int-sized integer argument which was promoted from a short.
l For integer types, causes printf to expect a long-sized integer argument.

For floating point types, causes printf to expect a double argument.

ll For integer types, causes printf to expect a long long-sized integer argument.
L For floating point types, causes printf to expect a long double argument.
z For integer types, causes printf to expect a size_t-sized integer argument.
j For integer types, causes printf to expect a intmax_t-sized integer argument.
t For integer types, causes printf to expect a ptrdiff_t-sized integer argument.

Additionally, several platform-specific length options came to exist prior to widespread use of the ISO C99 extensions:

Characters Description
I For signed integer types, causes printf to expect ptrdiff_t-sized integer argument; for unsigned integer types, causes printf to expect size_t-sized integer argument. Commonly found in Win32/Win64 platforms.
I32 For integer types, causes printf to expect a 32-bit (double word) integer argument. Commonly found in Win32/Win64 platforms.
I64 For integer types, causes printf to expect a 64-bit (quad word) integer argument. Commonly found in Win32/Win64 platforms.
q For integer types, causes printf to expect a 64-bit (quad word) integer argument. Commonly found in BSD platforms.

ISO C99 includes the inttypes.h header file that includes a number of macros for use in platform-independent printf coding. These need to be double-quoted, e.g. printf("%" PRId64 "\n", t);

Example macros include:

Macro Description
PRId32 Typically equivalent to I32d (Win32/Win64) or d
PRId64 Typically equivalent to I64d (Win32/Win64), lld (32-bit platforms) or ld (64-bit platforms)
PRIi32 Typically equivalent to I32i (Win32/Win64) or i
PRIi64 Typically equivalent to I64i (Win32/Win64), lli (32-bit platforms) or li (64-bit platforms)
PRIu32 Typically equivalent to I32u (Win32/Win64) or u
PRIu64 Typically equivalent to I64u (Win32/Win64), llu (32-bit platforms) or lu (64-bit platforms)
PRIx32 Typically equivalent to I32x (Win32/Win64) or x
PRIx64 Typically equivalent to I64x (Win32/Win64), llx (32-bit platforms) or lx (64-bit platforms)

Type[edit]

Type can be any of:

Character Description
d, i int as a signed decimal number. '%d' and '%i' are synonymous for output, but are different when used with scanf() for input (where using %i will interpret a number as hexadecimal if it's preceded by 0x, and octal if it's preceded by 0.)
u Print decimal unsigned int.
f, F double in normal (fixed-point) notation. 'f' and 'F' only differs in how the strings for an infinite number or NaN are printed ('inf', 'infinity' and 'nan' for 'f', 'INF', 'INFINITY' and 'NAN' for 'F').
e, E double value in standard form ([-]d.ddd e[+/-]ddd). An E conversion uses the letter E (rather than e) to introduce the exponent. The exponent always contains at least two digits; if the value is zero, the exponent is 00. In Windows, the exponent contains three digits by default, e.g. 1.5e002, but this can be altered by Microsoft-specific _set_output_format function.
g, G double in either normal or exponential notation, whichever is more appropriate for its magnitude. 'g' uses lower-case letters, 'G' uses upper-case letters. This type differs slightly from fixed-point notation in that insignificant zeroes to the right of the decimal point are not included. Also, the decimal point is not included on whole numbers.
x, X unsigned int as a hexadecimal number. 'x' uses lower-case letters and 'X' uses upper-case.
o unsigned int in octal.
s null-terminated string.
c char (character).
p void * (pointer to void) in an implementation-defined format.
a, A double in hexadecimal notation, starting with "0x" or "0X". 'a' uses lower-case letters, 'A' uses upper-case letters.[7][8] (C++11 iostreams have a hexfloat that works the same).
n Print nothing, but write number of characters successfully written so far into an integer pointer parameter.
% a literal '%' character (this type doesn't accept any flags, width, precision or length).

The width and precision formatting parameters may be omitted, or they can be a fixed number embedded in the format string, or passed as another function argument when indicated by an asterisk "*" in the format string. For example printf("%*d", 5, 10) will result in "   10" being printed, with a total width of 5 characters, and printf("%.*s", 3, "abcdef") will result in "abc" being printed.

If the syntax of a conversion specification is invalid, behavior is undefined, and can cause program termination. If there are too few function arguments provided to supply values for all the conversion specifications in the template string, or if the arguments are not of the correct types, the results are also undefined. Excess arguments are ignored. In a number of cases, the undefined behavior has led to "Format string attack" security vulnerabilities.

Some compilers, like the GNU Compiler Collection, will statically check the format strings of printf-like functions and warn about problems (when using the flags -Wall or -Wformat). GCC will also warn about user-defined printf-style functions if the non-standard "format" __attribute__ is applied to the function.

Risks of using field width versus explicit delimiters in tabular output[edit]

Using only field widths to provide for tabulation, as with a format like "%8d%8d%8d" for three integers in three 8-character columns, will not guarantee that field separation will be retained if large numbers occur in the data. Loss of field separation can easily lead to corrupt output. In systems which encourage the use of programs as building blocks in scripts, such corrupt data can often be forwarded into and corrupt further processing, regardless of whether the original programmer expected the output would only be read by human eyes. Such problems can be eliminated by including explicit delimiters, even spaces, in all tabular output formats. Simply changing the dangerous example from before to " %7d %7d %7d" addresses this, formatting identically until numbers become larger, but then explicitly preventing them from becoming merged on output due to the explicitly included spaces. Similar strategies apply to string data.

Custom format placeholders[edit]

There are a few implementations of printf-like functions that allow extensions to the escape-character-based mini-language, thus allowing the programmer to have a specific formatting function for non-builtin types. One of the most well-known is the (now deprecated) glibc's register_printf_function(). However, it is rarely used due to the fact that it conflicts with static format string checking. Another is Vstr custom formatters, which allows adding multi-character format names, and can work with static format checkers.

Some applications (like the Apache HTTP Server) include their own printf-like function, and embed extensions into it. However these all tend to have the same problems that register_printf_function() has.

The Linux kernel printk function supports a number of ways to display kernel structures using the generic %p specification, by appending additional format characters.[9] For example, %pI4 prints an IPV4 address in dotted-decimal form. This allows static format string checking (of the %p portion) at the expense of full compatibility with normal printf.

Most non-C languages that have a printf-like function work around the lack of this feature by just using the "%s" format and converting the object to a string representation. C++ offers a notable exception, in that it has a printf function inherited from its C history, but also has a completely different mechanism that is preferred.

Programming languages with printf[edit]

See also[edit]

Notes[edit]

  1. ^ "ASA Print Control Characters". Retrieved 12 February 2010. 
  2. ^ "Final SRFis". Srfi.schemers.org. Retrieved 2014-03-17. 
  3. ^ "Built-in Types — Python 3.3.4 documentation". Python Programming Language – Official Website. Python Software Foundation. Retrieved 12 February 2014. 
  4. ^ Martelli, Alex (March 2005). Python Cookbook, Second Edition. Sebastopol, CA: O'Reilly Media, Inc. p. 183. ISBN 0-596-00797-3. 
  5. ^ "PrintStream (Java 2 Platform SE 5.0)". Sun Microsystems Inc. 1994. Retrieved 18 November 2008. 
  6. ^ "String (Java 2 Platform SE 5.0)". Sun Microsystems Inc. 1994. Retrieved 18 November 2008. 
  7. ^ ""The GNU C Library Reference Manual", "12.12.3 Table of Output Conversions"". Gnu.org. Retrieved 2014-03-17. 
  8. ^ "printf" ("%a" added in C99)
  9. ^ "Linux kernel Documentation/printk-formats.txt". Git.kernel.org. Retrieved 2014-03-17. 

External links[edit]

Wikipedia content is licensed under the GFDL License
Powered by YouTube
LEGAL
  • Mashpedia © 2014