Printf
Syntax:
# Include <stdio. h> int printf (const char * format ,...); |
The printf () function is based onFormatThe format is printed and output to stdout (standard output) and other parameters.
StringFormat(Format) consists of two types of items-the characters displayed on the screen and other parameters defining the display of printf (). Basically, you can specifyFormat(Format) string, which can also be a "special" character mapped to another parameter of printf (). For example, thisCode
Char name [20] = "Bob"; int age = 21; printf ("Hello % s, you are % d years old/N", name, age );
The following output is displayed:
Hello Bob, you are 21 years old
% S indicates, "insert the first parameter here, a string." % d indicates that the second parameter (an integer) should be placed there. Different"%-Codes"Indicates different variable types, or you can limit the length of the variable.
Code |
Format |
% C |
character |
% d |
signed integer |
% I |
signed integer |
% E |
scientific notation, in lower case "E" |
% E |
scientific notation, using uppercase "E" |
% F |
floating point number |
% G |
use a shorter value in % E or % F |
% G |
use a shorter value in % E or % F |
% O |
octal |
% S |
a string of characters |
% u |
unsigned integer |
% x |
unsigned hexadecimal number, with lowercase letters |
% x |
unsigned hexadecimal number, with uppercase letters |
% P |
A pointer |
% n |
the parameter should be a pointer to an integer pointing to the position where the number of characters is placed |
% |
one '%' symbol |
An integer located between one % and the formatting command acts as a minimum field width specifier, and adds enough space or 0 to make the output long enough. if you want to fill in 0, place 0 before the minimum field width specifier. you can use an accuracy modifier that has different meanings based on the format code used.
All printf () outputs are right aligned unless you place a negative sign after the % sign. For example,
%-12.4f
It will display 12 characters, four decimal point numbers and left alignment. You can modify it with lettersLAndHType specifiers such as % d, % I, % O, % u, and % x specify the long and short data types (for example, % HD indicates a short integer ). % E, % F, and % G type specifiers, which can be placed before themLIt indicates that a double is followed. % G, % F, and % E can be placed before the character '#', even if there is no decimal place. the '#' character with the % x type specifier indicates that the prefix '0x 'should be included when the hexadecimal number is displayed. the '#' character with the % O type specifier indicates that a '0' prefix should be included when the number of octal characters is displayed.
You can includeContinuous escape sequence.
The return value of printf () is the number of printed characters. If an error occurs, a negative value is returned.