About%x and other formatting characters in C

Source: Internet
Author: User
Tags clear screen

Original: http://blog.csdn.net/lincyang/article/details/6252443

Formatting:

%x means 16 input output;
int a = 16;
%02X: Output 10;
%03x: output: 010;
%04x: output: 0010;

Reproduced below: http://tech.e800.com.cn/articles/2009/83/1249265552433_1.html

1.1.1 formatting input and output functions
The Turbo C2.0 Standard Library provides two console format inputs, output functions printf (), and
scanf (), these two functions can read and write data in a variety of formats on a standard input.
The printf () function is used to write data to a standard output device (screen); The scanf () function is used from the standard input
Read data on the device (keyboard). The use of these two functions is described in detail below.
One, the printf () function
The printf () function is a formatted output function that is typically used to output to a standard output device in a specified format
Information. This function is often used when writing programs. The format for calling the printf () function is:
printf ("< formatted string >", < Parameter table >);
Where the formatted string consists of two parts: some are normal characters, and the characters are sorted by the original
Sample output; The other part is the formatting of the specified character, starting with "%", followed by one or more of the specified characters,
Used to determine the output content format.
The parameter table is a series of parameters that need to be output, and the number must be the output indicated by the formatted string
The number of parameters is as many as, the parameters are separated by "," and the order one by one corresponds, otherwise the intent will appear
Not to the error.
1. Formatting the specified character
The format specifier provided by the Turbo C2.0 is as follows:
━━━━━━━━━━━━━━━━━━━━━━━━━━
Symbolic effect
——————————————————————————
%d decimal signed integer
%u decimal unsigned integer
%f floating Point
%s string
%c single character
%p the value of the pointer
Floating-point numbers in the form of%e indices
%x,%x unsigned integer in hexadecimal notation
%0 unsigned integer represented in octal
%g automatic selection of appropriate representations
━━━━━━━━━━━━━━━━━━━━━━━━━━
Description
(1). You can insert a number between "%" and a letter to indicate the maximum field width.
For example:%3d represents the output of 3-bit integers, not enough 3-bit right-justified.
The%9.2f represents a floating-point number with an output field width of 9, where the decimal bit is 2 and the integer bit is 6.
One decimal point, not enough 9-bit right-aligned.
The%8s represents the output of a 8-character string, which is not aligns aligned to 8 characters.
If the length of a string, or the number of integers exceeds the width of the description, it is output at its actual length.
But for floating-point numbers, if the integer number of bits exceeds the width of the indicated integer digits, it will be output according to the actual integer digits;
If the fractional number of digits exceeds the indicated decimal width, the output is rounded by the width of the description.
In addition, if you want to add some 0 before the output value, you should add a 0 before the field width.
For example:%04D indicates that when a numeric value less than 4 bits is output, the total width of the previous 0 is made
is 4 bits.
If you use floating-point numbers to represent the output format of a character or integer, the number after the decimal point represents the maximum width,
The number before the decimal point represents the minimum width.
For example,%6.9s indicates that a string with a length of not less than 6 and not greater than 9 is displayed. If it is greater than 9,
The contents of the 9th character will be deleted.
(2). You can add a lowercase letter L between "%" and the letter, indicating that the output is a long form.
For example,%ld indicates an output long integer
%LF indicates the output double floating-point number
(3). You can control the output left-justified or right-aligned, that is, the "%" and the letter to add a "-" number can be
Indicates that the output is left-justified, otherwise right-justified.
Example:%-7d for output 7-bit integers left justified
%-10s indicates output 10 characters aligns aligned
2. Some special provisions of the character
━━━━━━━━━━━━━━━━━━━━━━━━━━
Character action
——————————————————————————
/N Line break
/F Clear screen and change page
/R Enter
/T Tab character
/XHH represents an ASCII code with 16-in notation,
where HH is 1 to 2 x 16 binary number
━━━━━━━━━━━━━━━━━━━━━━━━━━
The printf () function learned in this section, combined with the data type learned in the previous section, compiles the following process
To deepen the understanding of the Turbo C2.0 data type.
Example 1
#include
#include
int main ()
{
Char c, s[20], *p;
int a=1234, *i;
float f=3.141592653589;
Double x=0.12345678987654321;
P= "How does You Do";
strcpy (S, "Hello, comrade");
*i=12;
c= '/x41 ';
printf ("a=%d/n", a); /* Result output decimal integer a=1234*/
printf ("a=%6d/n", a); /* Results output 6-bit decimal number a= 1234*/
printf ("a=%06d/n", a); /* Results output 6-bit decimal number a=001234*/
printf ("a=%2d/n", a); /*a more than 2 bits, output by actual value a=1234*/
printf ("*i=%4d/n", *i); /* Output 4-bit decimal integer *i= 12*/
printf ("*i=%-4d/n", *i); /* Output left-justified 4-bit decimal integer *i=12*/
printf ("i=%p/n", I); /* Output Address i=06e4*/
printf ("f=%f/n", f); /* Output floating-point number f=3.141593*/
printf ("f=6.4f/n", f); /* Output 6-bit floating-point numbers with 4 digits after the decimal point
f=3.1416*/
printf ("x=%lf/n", X); /* Output long floating-point number x=0.123457*/
printf ("x=%18.16lf/n", x);/* Output 18-bit long floating point where 16 bits after the decimal point
Number x=0.1234567898765432*/
printf ("c=%c/n", c); /* Output character c=a*/
printf ("c=%x/n", c); /* ASCII code value for output character c=41*/
printf ("s[]=%s/n", s); /* Output array string S[]=hello, comrade*/
printf ("s[]=%6.9s/n", s);/* Outputs a string of up to 9 characters S[]=hello,
co*/
printf ("s=%p/n", s); /* Output array string first character address s=ffbe*/
printf ("*p=%s/n", p); /* Output pointer string p=how do do*/
printf ("p=%p/n", p); /* The value of the output pointer p=0194*/
Getch ();
RETUNR 0;
}
The address values in the above results may be different on different computers.
Example 1. The meaning of the first statement, # include, is to call another file stdio.h, which
is a header file that includes data type definitions and function descriptions for all standard input and output library functions.
The Turbo C2.0 defines and explains the variables and function types used by each library function, and puts them in the corresponding
In the header file "*.h", users must use #include<*.h> or # include "* when using these functions. H
Statement to call the corresponding header file for the connection. If this statement is not used, the connection will be error-
Miss.
Second, scanf () function
The scanf () function is a formatted input function that reads input information from a standard input device (keyboard).
Its invocation format is:
scanf ("< formatted string >", < Address table >);
The formatted string includes the following three different types of characters;
1. Format specifier: The format specifier is basically the same as the format specifier in the printf () function.
2. Whitespace character: Whitespace character causes the scanf () function to omit one or more of the inputs in the read operation
A blank character.
3. Non-whitespace characters: a non-whitespace character causes the scanf () function to be removed from the read-out
Characters with the same white space characters.
The Address table is the address of all the variables that need to be read in, not the variable itself. This is associated with the printf () function
Completely different, pay special attention to it. Separate the addresses of each variable with ",".
Example 2:
Main ()
{
int I, J;
printf ("I, j=?/n");
scanf ("%d,%d", &i, &j);
}

The scanf () function in the example above reads an integer and then strips out the comma that is then entered, the most
Read into the other integer number. If "," this particular character is not found, the scanf () function terminates. If
The delimiter between the parameters is a space, you must enter one or more spaces between the parameters.
Description
(1). For string arrays or string pointer variables, the array name and pointer variable name itself
are addresses, so when using the scanf () function, you do not need to precede them with the "&" operator.
Example 3
Mian ()
{
Char *p, str[20];
scanf ("%s", p); /* Enter a string from the health disk */
scanf ("%s", str);
printf ("%s/n", p); /* Output string to screen */
printf ("%s/n", str);
}

(2). You can add an integer between the "%" formatting specifiers in the formatted string to indicate
The maximum number of digits in any read operation.
In Example 3 if you can only enter 10 characters to the string pointer p, then the first scanf () function statement
Into
scanf ("%10s", p);
When the program runs, if the number of characters is greater than ten, p will not continue to read, and a later read
The entry function, scanf ("%s", str), starts reading in the 11th character.
There is an issue with the actual use of the scanf () function, which is illustrated below:
When you use multiple scanf () functions to enter multiple character variables consecutively, for example:
Main ()
{
Char C1, C2;
scanf ("%c", &C1);
scanf ("%c", &C2);
printf ("C1 is%c, C2 is%c", C2/1, C2);
}

Run the program, enter a character a after carriage return (to complete the input must enter), in the execution scanf
("%c", &C1), assign "A" to the variable c1, but the carriage return remains in the buffer, executing the input statement
scanf ("%c", &C2), the variable C2 output is a blank line, if the input ab after the return, then the output junction
The fruit is: C1 is A, C2 is B.
To solve these problems, you can add a clear function fflush () before the input function (the function
The method will be described at the end of this section). Modify the above procedure into:
#include
Main ()
{
Char C1, C2;
scanf ("%c", &C1);
Fflush (stdin);
scanf ("%c", &C2);
printf ("C1 is%c, C2 is%c", C1, C2);
}

1.1.2 Unformatted Input-output functions
Unformatted input-output functions can be replaced by the standard format input and output functions described above, but
These functions are compiled with less code, are relatively small in memory, and thus improve speed, while also comparing
Convenient. The following are described separately.
One, puts () and gets () function
1. puts () function
The puts () function is used to write a string to a standard output device (screen) and wrap it in a format called:
Puts (s);
where S is a string variable (string array name or string pointer).
The puts () function works the same as the language printf ("%s/n", s).
Example 4:
Main ()
{
Char s[20], *f; /* Define string array and pointer variable */
strcpy (S, "hello! Turbo C2.0 "); /* String array variable assignment */
f= "Thank You"; /* String pointer variable assignment */
Puts (s);
Puts (f);
}

Description
(1). The. puts () function can only output strings, cannot output numeric values, or format transformations.
(2). You can write a string directly into the puts () function. Such as:
Puts ("Hello, Turbo C2.0");

2. The gets () function
The Get () function is used to read a string from a standard input device (keyboard) until the carriage return ends, but the carriage return
Does not belong to this string. Its invocation format is:
Gets (s);
where S is a string variable (string array name or string pointer).
The gets (s) function is similar to scanf ("%s", &s), but not identical, using scanf ("%s", &s)
There is a problem with the function input string, that is, if you enter a space, the input string ends,
The character after the space is processed as the next entry, but the Get () function receives the entire character of the input
String until the carriage return.
Example 5
Main ()
{


Char s[20], *f;
printf ("What ' s Your name?/n");
Gets (s); /* Wait for input string until end of carriage return */
Puts (s); /* Outputs the input string */
Puts ("How old is You?");
Gets (f);


Puts (f);
}

About%x and other formatting characters in C

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.