C-formatted input-output functions

Source: Internet
Author: User

One: Format output function printf ()

1, the call form is generally: printf ("Format control string", output table column);

2. The format control string is used to specify the output format, which has three forms:

    • 1. Format specifier: Specifies the output format of the corresponding output table column, with a% beginning, such as%d,%o, etc.
    • 2. Escape character: Used to output the control code or special characters that the escape character represents, such as the usual ' \ n ', ' \ t '
    • 3. Ordinary characters: characters that need to be output as-is.

3, the output table is listed as several data items that need to be output, and it corresponds to the format specifier one by one on the quantity and type;

4, the format character m specifies the width of the output data, n for the real number represents the output n decimal places, the string represents the amount of characters intercepted, + the right alignment, usually omitted.

-Indicates left alignment, L is used for long integer data, can be added to D, O, X, U, and more format specifiers and their combined form are as follows:

Format characters

Data Objects

Output form Data Output method

% (+)-MD

Int

unsigned int

Short

unsigned short

Char

Decimal integer

1, no m output according to the actual number of digits

2, have m output m bit, more than M bit, according to the actual number of output, insufficient space to fill

3. have + (default = +) Right align (left fill space)

4, have-left alignment (right fill space)

% (+)-mo Eight-binary integers
% (+)-mx hexadecimal integer
% (+)-mu unsigned integer
% (+)-MLD

Long

unsigned long

Decimal integer

% (+)-mlo Eight-binary integers
% (+)-MLX hexadecimal integer
% (+)-mlu unsigned integer
% (+)-m.nf

Float

Double

Decimal decimal
% (+)-m.ne Decimal index
% (+)-G

Automatically selects short output widths in%f and%e to output single, double-precision floating-point numbers

% (+)-MC

Char

Int

Short

Single character

1. No m output single character

2, with m output m bit, fill space

3. have + (default = +) Right align (left fill space)

4, have-left alignment (right fill space)

% (+)-m.ns String A string of characters

1. No m, n output all characters by actual string

2, have m, n only output the first n characters, fill the space

3. have + (default = +) Right align (left fill space)

4, have-left alignment (right fill space)

Two: Format input function scanf ()

1, the call format is generally: scanf ("Format control string", Address table column);

2, the format control string and the printf () function similar meaning, the difference is that it is the input format control;

3, the Address table column is a number of data waiting for input corresponding to the memory unit address composition, separated by commas, the general form of &a,a as a variable;

4, the Address table column in the number and type and format control string in the format specifier one by one corresponds;

5, the format character H denotes the input short data, can be used in the D, O, x front, m specifies the width of the input data,

* indicates that the corresponding data item is not assigned to the corresponding variable after it is read, more format specifiers and their combinations are as follows

Format characters Data Objects Input form Data entry Methods
%md

Int

Short

unsigned int

unsigned short

Decimal integer

1, no m input according to the actual number of digits

2, have m input m bit, less than m followed by enter

%mo Eight-binary integers
%mx hexadecimal integer
%mld

Long

unsigned long

Decimal integer

%mlo Eight-binary integers
%mlx hexadecimal integer
%mlf

Float

Double

Decimal integer
%mle
%mc Char Single character

1. No m takes a single character

2, have m input m bit, only take the first character

%ms String A string of characters

1, no m to retrieve the car or a number of characters before the space

2, have M only take the first m character

C-formatted input-output functions

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.