printf

from Wikipedia, the free encyclopedia

printf ( English print formatted ) is a very common output function that comes from the C programming language .

The function accepts a character string with text and formatting instructions as well as zero, one or more values. The character string is output and the values ​​are inserted in the appropriate formatting (notation).

As a rule, the output is on the standard output . There is also a variant fprintffor output on any output data stream (e.g. standard error output or a file).

Use in the C programming language

Hello world program in C, using printf .

#include <stdio.h>

int main(void)
{
    printf("Hallo Welt!\n");
    return 0;
}

The function consists of a formatting part and the specific arguments to be output.

An example of the printf function

The following placeholders are used in the format string for the various data types / character sequences:

Type / character sequence Letters
int % i or% d
long % ld
float / double % f
char % c
char * (string) % s
(void *) % p
hexadecimal % x
\ n Jump to the beginning of the following line of the screen (LF)
\ b Step back one sign
\ a Acoustic signal
\ r Jump to the beginning of the current screen line (CR)
\\ Output of the backslash "\" (backslash)
%% Output of the percent sign
\ " Output of a double quote
\ t Jump to the next tab position (TAB)

Java

In the Java programming language in 2004 similar functionality was introduced with Release 5. Since then, the two classes java.io.PrintStream and java.io.PrintWriter have corresponding methods called format . There is also a separate class, java.util.Formatter, and a static method format of the standard class String , which process appropriate formatting patterns and generate formatted strings.

Individual evidence

  1. New Features and Enhancements: J2SE 5.0. In: JDK 5.0 Documentation. Retrieved January 20, 2014 .

literature

  • Brian Kernighan, Dennis M. Ritchie: The C Programming Language . 1st edition. Hanser, Munich 1983, ISBN 3-446-13878-1 , p. 157-159 .

Web links

Wikibooks: C programming: Simple input and output  - learning and teaching materials