Input and output format control in C language

Source: Internet
Author: User

1, C , non-0 value is true, true with 1, 0 value is false, False with 0 is indicated.

2. Escape character Reference:

\a

Beep, Bell

\b

Fallback: Back one pane

\f

Page change

\ n

Line break

\ r

Enter, cursor to the beginning of the bank

\ t

Horizontal tabulation

\v

Vertical tabulation

\\

Back slash

\‘

Single quotation marks

\"

Double quotes

\?

Question mark

\ddd

Three-bit octal

/

Null character (null), do nothing

\xhh

Two-bit hexadecimal

Description

1) \v Vertical tabs and \f page breaks have no effect on the screen, but affect the printer's response actions.

2), \ n actually should be called carriage return to line. Line break is just a line, do not change the horizontal axis of the cursor, enter just back to the beginning, do not change the vertical axis of the cursor.

Note:

Line terminator for text files , traditionally (MS)pc with CRLF, Apple Mac with CR,Unix with LF.

CR- carriage return , c ' \ r '

LF-- line break , c ' \ n '

The uniform rule of C language on different computers is that the line terminator of a text file becomes a symbol LF, that is, the newline character ' \ n '. "Carriage return and newline character converted to a newline character", for PC (MS OS), the text file line terminator CRLF read in , discard CR, leave LF. For example, fgets () reads a line with only LF at the end of the line and no CR.

3) the \ t cursor moves forward four or eight cells, which can be set in the compiler

4) \ ' is used in the word character (i.e. single quotes). In the string (that is, double quotes) is not required, just use ' can.

3. Format input and output

1) format input

printf (" format string " [, expression 1, expression 2,]);

There are two types of characters in the format string:

① a format character that starts with a% start, and is used to control the output format of an expression (how many expressions there are, and how many format characters );

② Non-format character: displayed on the screen as is.

(1) output of string constants

No expression, no format characters in the format string, such as:

printf ("We are students.\n"); 

(2) syntax for format characters

Syntax:%[flag][width][.precision][h|l| L]type

a) [h|l| L]type

The Type field uses an English letter to express the data type and format :

D

Output as a decimal signed integer

I

Output as a decimal signed integer (in the same format as D )

U

Output as an unsigned decimal number

O

Output as an octal unsigned number

X

Output as a hexadecimal unsigned number using lowercase letters (A, B, C, D, E, f)

X

Output in hexadecimal unsigned numbers, with uppercase letters (A, B, C, D, E, F) on output

F

Output floating-point number as decimal decimal, output format : [-]ddd.dddddd(Default output 6 decimal)

E

Output floating-point numbers in decimal exponent format:[-]d.dddde[+/-]ddd, (e followed by exponent )

E

The floating-point number is output in decimal exponent format:[-]D.DDDDE[+/-]DDD, ( the exponent is followed by E ).

When exporting floating-point numbers in E and E formats, the output is in scientific notation, where the integer portion preceding the decimal point is fixed to a 1 -bit integer

G

Output floating-point numbers in decimal form, automatically choose f or e format, output length of a small format output;g format does not output useless 0.

G

Output floating-point numbers in decimal form, automatically choose F or E format, output length of a small format output;G format does not output useless 0.

C

Output a single character

S

Output string

P

Output void pointer, output format is:WIN32 Programming mode, only XXXXXXXX one format

%

Output character %(% for boot format control, must be in format percent when output% number in format string )

Prefix modifier:

L: output long double type expression must be added

L: output Long integer expression must be added

H: output short integer expression must be added

b)[width]

The Width field is used to specify the number of character columns that the output data item occupies, also known as the output field width. By default, the output width is output by the actual number of digits of the data, and if the specified output width is less than the actual number of digits of the data, the limit of the width of the field is exceeded, and the actual number of digits is output; The output data is automatically aligned to the right in the output field.

Width has the following three conditions:

N

A nonnegative integer constant that specifies that the output occupies the width of the N column.

0n

n is an integer constant, the output occupies n columns, if the actual number of bits is less than n columns, the data is preceded by 0, filling n columns

*

The output field is wide from an integer expression that precedes the output expression.

printf ("%*d", 8+1,-3);

c)[. precesion]

There are 4 scenarios:

Default

For F |e| E format, which represents a 6 decimal place after the decimal point, for g | G format, representing up to 6 digits of valid digits

. m

M is a nonnegative integer constant, for floating-point format, the output m decimal number after the decimal point, and the S format, which represents only the first M characters of the output string

.0

For F |e| the E format, which indicates that no decimal and decimals are output, as integers

.*

Represents the precision from an integer expression that precedes the output expression

Example

View Code?
#include "iostream"#include "stdio.h"usingnamespace std; intmain(){    doublea=12.578,b=0.2572;    intn=9,m=4;    printf("%.2f,%10.2E,%f,%e,%10g",a,a,a,a,a);    printf("\n");    printf("a=%.0f,a=%*.*f",a,n,m,a);    printf("\n");    printf("%.2f%%",b*100);    printf("\n");    printf("%s%6s%6.3s","ABCD""ABCD""ABCD");    return1;}

Note that any one of the characters in the output counts as one bit.

D) [flag]

Default

The output positive number does not output a plus sign ; When the field is wider than the actual number of digits, the left side of the field is blank, and the data is aligned to the right;

+

A positive number is output when it is output .

-

When the field is wider than the actual number of digits, the right side of the field is blank, and the data is left-aligned.

#

For x | X format: Output 0x(format x) or 0X(format x).

1. Format Input function scanf

Usage : scanf (" format string ", address 1, address 2,...) );

1) format character

Syntax:%[*][width][h|l| L]type

(1) [h|l| L]type

D

Convert input data as a decimal signed integer

I

Convert input data as a decimal signed integer ( same as D format )

U

Convert input data as a decimal unsigned integer

O

Convert input data as an octal signed integer

x | X

Convert input data as a hexadecimal signed integer (x and x equivalent )

e| E|f|g| G

Converts input data as decimal floating-point numbers, entering data with integer constants, fractional-form solid constants, or exponential-form solid constants.

C

Enter a character ( you can enter a control character )

S

Enter a string (the first space,TAB, or line break end conversion is encountered)

Prefix modifier:

L: input long double type variable must be added

L: input Long integer variable or double type variable must be added

H: input short integer variable must be added

(2) [Width]

The Width field is used to specify the conversion width of the input data, which must be a decimal non-negative integer constant. width Indicates how many characters are read in to end the conversion of this data item. If width is not specified , a space,TAB key, carriage return /Line break, and illegal input are encountered for the conversion of the end data item (except for%c format).

float A;int b;double C;

scanf ("%f%3x%lf", &a,&b,&c);

If the input is:-1.5a8059¿

Then a=-1.5, B=0XA80, c=59.0

(3) [*] No.

Indicates that the data entry is to be converted in the specified format, but the variable is not saved, that is, the % does not have a corresponding variable.

Generally use %*c to absorb characters.

2, several notes:

The compiler simply checks the invocation form of the printf function, does not parse the format control string, and does not convert the type if the format character does not match the type of the output item. (The number of outputs is random)

Format characters are in lowercase letters.

The format control string can contain escape characters.

Normal characters in the format control string are output as-is.

The parameters of an output item can be an expression, a function call, in addition to constants and variables.

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.