On C language escape character and format controller _c language

Source: Internet
Author: User
Tags integer numbers lowercase

Escape character Reference:

\a: Beep, Bell

\b: Rewind: back to a grid

\f: Page Change

\ n: newline, the cursor to the beginning of the line

\ r: Enter, the cursor to the beginning of the line

\ t: Horizontal tabulation

\v: Vertical Tabulation

\: Back Slash

\ ': Single quotes

\ ": Double quotes

\?: Question mark

\DDD: three-bit octal

\XHH: two-bit hexadecimal

: null character (NULL), do nothing

Note:

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

2,\n in fact should be called carriage return line change. Line change is just a line, do not change the horizontal axis of the cursor; carriage return just goes back to the beginning, not changing the ordinate of the cursor.

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

4,\ ' is used in character character (that is, single quotes). In the string (that is, double quotes) do not need, as long as the ' can.

5,\? Actually, it's not necessary. Just use it? is available (validated in Windows VC6 and TC2).

Format Control character reference:

Format output character: printf

Use: Output several types of data to a terminal.

Form: printf (format control character, output list)

Description

Format control character:% Format Description Guide.

-Specifies left-aligned output.

0 Specify vacancy fill zero.

M.N Specifies the output field width and precision.

Correction of l.h output length.

The format character specifies the data type of the output.

Description

Format characters: Specifies the data type and output format of the output item.

D signed decimal integer.

o unsigned octal number.

x unsigned hexadecimal number. (The lowercase x format uses lowercase letters a,b,c,d,e,f to denote numbers between 10 and 15, and an uppercase X abcdef the number between 10 and 15).

u unsigned decimal integer.

Basic int: Two bytes, 16 bit (bit)

11 11 11 11 11 11 11 is 11 equals 65535.

11 11 11 11 11 11 11 is equal to 10.

The first digit (sign bit) of a negative number is 1. How to get the complement of a negative number: the original code to take the reverse plus one. The binary number of 2 is: 00 00 00 00 00 00 00 10, the reverse is equal to 11 11 11 11 11 11 11 01, then add 1, binary number every two into one, so we get the binary code of 2.

The number of signed integers can be output in%d.

Unsigned integer numbers can be output in%u.

The number stored in the computer is binary number, the highest bit is the symbol bit or data bit, can be controlled by the output format character.

Binary number conversion octal number: Three 1 corresponds to one 7.

Binary number chisel hexadecimal number: four-bit binary corresponds to a hexadecimal, four 1 corresponds to an F.

Description 

Format characters:
C Output one character.

s outputs a string.

e outputs the real number in exponential form.

f outputs the real number in decimal form.

G automatically determines that the output format is the shorter of E and F, and does not print an invalid 0.

% output%.

Attention:

For single-precision numbers, the first 7 digits are valid digits, with a decimal 6 digits, when using the%f format character output.

For double-precision numbers, the first 16 digits are valid digits, with a decimal number of 6 digits, when using the%LF format character output.

Length modifier

L: Specify long integer for integral type

Example:%ld,%LX,%lo,%lu

Specify double for the solid type

Example:%LF

H: Format character for integral type Fu Xiu is short

Example:%hd,%hx,%ho,%hu

For the input and output of 64-bit integers, in the C + + environment on POJ (ie VC + +), 64-bit integers are:

__int64

The input output format is "%i64d".

In a g++ environment, a 64-bit integer is

Long Long

The input output format is "%lld".

Description of field width and precision

M: field width, i.e. the number of characters on the output device for the corresponding output item

If: The actual data width >m, according to the actual data width output.

Actual data width <m, left blank.

N: Precision, indicating the number of decimal digits of the output's real number.

A few notes:

The compiler simply checks the call form of the printf function, does not parse the format control string, and does not type conversion 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 a constant or variable.

The%g,%f and%e three formats are used to print floating-point values.

The%g format is especially useful for printing floating-point numbers that do not need to be aligned by columns. It has two functions:

One, remove the extra 0 (not up to six digits) in the end of the number

Second, keep six digits (extra six digits)

The%e format is used in exponential form when printing floating-point numbers: For example: Output pi: 3.141593e+00

The difference between the two:

The number printed in the%G format is a total of 6-digit valid digits

%e format prints out 6 digits after the decimal point

%f prohibit the use of exponential form to represent floating-point numbers. So the PI output is: 3.141593

(but note that its precision requirements: is also a decimal point after the 6-digit valid digits)

(8)%% format is used to print a% character.

(9)%e and%g only use uppercase letters (e) in place of lowercase letters in the output (e)

The technique of outputting signs, for example:

printf ("%+d%+d%+d\n", -5,0,5);

Just add a "+" number in the middle. function is the output sign bit (that is, the number of positive and negative numbers)

If you do not want the ' + ' sign to precede a positive number, use the following method

Just add a "" number in the middle (that is, a space) on the line. For example:

Action: If a number is non-negative, insert a space in front of it.

int i;

for (i=-3;i<=3;i++)

printf ("% d\n", i); Note that there is a space between% and D

SSCANF () Tips for using:

Decomposition of strings, many features require regular expression knowledge, so the simplest of several use sscanf to decompose the use of strings.

1.

Char str[100],str1[100],str2[100];

Gets (str);

SSCANF (str, "%s%s", STR1,STR2);

Divides an entire line of read-in characters into two strings by space, tab, or carriage return.

2.

The string that takes the specified length. In the following example, take a string with a maximum length of 4 bytes.

SSCANF ("123456", "%4s", str);

The above article on the C language escape characters and format control is a small series to share all the content, I hope to give you a reference, but also hope that we support the cloud habitat community.

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.