Printf () function

Source: Internet
Author: User

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.

    • Use % E, % E, and % F to specify the number of decimal places you want. For example,

      % 12.6f

      A floating point number with at least 12 digits and six decimal digits will be displayed.

    • Use % G and % G. The precision modifier determines the maximum number of digits of the displayed valid number.
    • Use % s to represent a precision modifier.MaxTo supplement the minimum field length before the period.

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.