Instance C language printf format

Source: Internet
Author: User
Tags truncated

When printing with printf, it is found that if the data type is different from the type used for printing, the result is chaotic.

This is because printf does not convert the data type itself. It only outputs the input according to the given data format. If the two do not match, the binary storage methods of different data types are different, the read data is incorrect.

Therefore, for the sake of security, you can manually convert printf, fprintf, scanf, and fscanf when the types are inconsistent!

Printf ("% F \ n", 1.3 );

Printf ("% d % F \ n", 1.3, 1.3 );

Print result:

1.300000

-858993459-92559653408872784000000000000000000000000000000000000000000000.000000

The first line is correctly printed, but the last line 1.3 does not match % d. This not only affects the last line but also prints the error.

 

Read fscanf and read it to the float type with % F. Read it to the double type with % lf

 

C language printf format

 
The common format of the format string in C is: % [flag] [minimum output width] [. Precision] [length] type. The items in square brackets [] are optional. The meanings of each item are described as follows:

1. Type type characters are used to indicate the type of output data. The format characters and meanings are shown in the following table:
It indicates the meaning of the characters in the format of the output type. A floating point number, hexadecimal number, and p-notation (c99) a floating point number, hexadecimal number, and p-notation (c99) c Outputs a single character
D. Output signed integers in decimal form (positive numbers do not output symbols) e. Output a single order in exponential form, double-precision real number E. Output a single order in exponential form, double-precision real number F. Output a single order in decimal form, double-precision real number G. Output a single order in a shorter output width in % F % E., double-precision real number, % eformat used when the exponent is smaller than-4 or greater than or equal to the precision
G uses the shorter output width of % F % E to output a single and double-precision real number. The % E formula is used when the exponent is smaller than-4 or greater than or equal to the precision.
I signed decimal INTEGER (same as % d)
O outputs an unsigned integer in octal form (no prefix o is output) P pointer s output string
X outputs an unsigned integer in hexadecimal format (no prefix ox is output) x outputs an unsigned integer in hexadecimal format (no prefix ox is output)
U outputs unsigned integers in decimal format/* test common output types */# include "stdio. H"
# Include "conio. H" Main ()
{
Printf ("the program test print style! \ N ");/* output a signed integer in decimal format (positive number does not output a symbol )*/
Printf ("% d \ n", 223 );
Printf ("% d \ n",-232 );
Printf ("\ n");/* Outputs unsigned integers in octal form (no prefix o is output )*/
Printf ("% O \ n", 223 );
Printf ("% O \ n",-232 );
Printf ("\ n");/* Outputs unsigned integers in hexadecimal format (no output prefix Ox )*/
Printf ("% x \ n", 223 );
Printf ("% x \ n",-232 );
Printf ("\ n");/* output an unsigned integer in decimal format */
Printf ("% u \ n", 223 );
Printf ("% u \ n",-232 );
Printf ("\ n");/* Outputs Single and Double Precision Real Numbers in decimal form */
Printf ("% F \ n", 223.11 );
Printf ("% F \ n", 232.11111111 );
Printf ("% F \ n",-223.11 );
Printf ("% F \ n",-232.11111111 );
Printf ("\ n");/* output single and double precision real numbers in exponential form */
Printf ("% E \ n", 223.11 );
Printf ("% E \ n", 232.11111111 );
Printf ("% E \ n",-223.11 );
Printf ("% E \ n",-232.11111111 );
Printf ("\ n");/* output a single, double-precision real number with a shorter output width in % F % E */
Printf ("% G \ n", 223.11 );
Printf ("% G \ n", 232.111111111111 );
Printf ("% G \ n",-223.11 );
Printf ("% G \ n",-232.111111111111 );
Printf ("\ n");/* output a single character */
Printf ("% C \ n", 'A ');
Printf ("% C \ n", 97 );
Printf ("\ n");/* output a single character */
Printf ("% s \ n", "this is a test! ");
Printf ("% s \ n", "2342o34uo23u ");
Printf ("\ n"); getch ();
} 2. Flag
The following table lists the characters-, +, #, space, and 0:
Logo format character logo meaning
-The result is left aligned with a space on the right.
+ Space of the output symbol (positive or negative). The output value is a positive value with a space and a negative value with a negative value.
# It has no effect on Class C, S, D, and U. For Class O, add a prefix of 0 to the output. For Class X,
Add the prefix 0x or 0x to the output, and delete the G and G classes to prevent trailing 0. For all floating-point formats, # ensures that even if it is not followed by any number, print a decimal point character 0. Fill in the field width with leading 0 for All numeric formats. If a-sign is displayed or the precision (for integers) is specified, ignore 3. minimum output width
The minimum number of digits of the output is expressed by a decimal integer. If the actual number of digits is greater than the defined width, the actual number of digits is output. If the actual number of digits is less than the defined width, spaces or 0 are supplemented. /* Four test flag characters:-, +, #, and space */# include "stdio. H"
# Include "conio. H" Main ()
{/* Output signed integers in decimal format (positive numbers do not output symbols )*/
Printf ("* %-10D * \ n", 223 );
Printf ("* % + 10d * \ n",-232 );
Printf ("* % 2D * \ n", 223 );
Printf ("* % # D * \ n",-232 );
Printf ("\ n ");
Getch ();/* Outputs unsigned integers in octal form (no prefix o is output )*/
Printf ("* %-10o * \ n", 223 );
Printf ("* % + 10o * \ n",-232 );
Printf ("* % O * \ n", 223 );
Printf ("* % # O * \ n",-232 );
Printf ("\ n ");
Getch ();/* Outputs unsigned integers in hexadecimal format (no prefix ox is output )*/
Printf ("$ %-10x $ \ n", 223 );
Printf ("$ % 010x $ \ n",-232 );
Printf ("$ % x $ \ n", 223 );
Printf ("$ % # x $ \ n",-232 );
Printf ("\ n");/* output an unsigned integer in decimal format */
Printf ("%-10u \ n", 223 );
Printf ("% + 10u \ n",-232 );
Printf ("% u \ n", 223 );
Printf ("% # U \ n",-232 );
Printf ("\ n ");
Getch ();/* output single and double precision real numbers in decimal form */
Printf ("%-10f \ n", 223.11 );
Printf ("% + 10f \ n", 232.11111111 );
Printf ("% F \ n",-223.11 );
Printf ("% # f \ n",-232.11111111 );
Printf ("\ n ");
Getch ();/* output single and double precision real numbers in exponential form */
Printf ("%-10E \ n", 223.11 );
Printf ("% + 10E \ n", 232.11111111 );
Printf ("% E \ n",-223.11 );
Printf ("% # E \ n",-232.11111111 );
Printf ("\ n ");
Getch ();/* output a single output with a shorter output width in % F % E and a double-precision real number */
Printf ("%-10g \ n", 223.11 );
Printf ("% + 10g \ n", 232.111111111111 );
Printf ("% G \ n",-223.11 );
Printf ("% # G \ n",-232.111111111111 );
Printf ("\ n ");
Getch ();/* output a single character */
Printf ("%-10C \ n", 'A ');
Printf ("% + 10C \ n", 97 );
Printf ("% C \ n", 'A ');
Printf ("% # C \ n", 97 );
Printf ("\ n ");
Getch ();/* output a single character */
Printf ("%-20s \ n", "this is a test! ");
Printf ("% + 20s \ n", "2342o34uo23u ");
Printf ("% 20s \ n", "this is a test! ");/* Do not fill in spaces */
Printf ("% # s \ n", "2342o34uo23u ");
Printf ("\ n ");
Getch ();} 4. Precision
The precision format character starts with "." and is followed by a decimal integer. The meaning of this item is: if a number is output, it indicates the number of digits in decimal places. If the output is a character, it indicates the number of output characters. If the actual number of digits is greater than the defined precision, the part that exceeds the limit is truncated. /* Test accuracy */# include "stdio. H"
# Include "conio. H" Main ()
{
Printf ("%. 3D \ n", 5555 );
Getch (); printf ("%. 3f \ n", 0.88888 );
Getch (); printf ("%. 3f \ n", 0.9999 );
Getch (); printf ("%. 4S \ n", "this is a test! ");
Getch ();
} 5. Length
The length format is H and l. h indicates output by short integer, and l indicates output by long integer. H and Integer Conversion specifiers are used together to represent a value of the Short Int or unsigned short int type. Example: % Hu, % HX, % 6.4hd HH and Integer Conversion specifiers are used together, A value of the Short Int or unsigned short type. For example, % hhu, % hhx, % 6.4hhd J and Integer Conversion specifier are used together to indicate a value of the intmax_t or uintmax_t type, example: % JD, % 8jx L and Integer Conversion specifier are used together to represent a long Int or unsigned long int type value. Example: % lD, % 8lu ll and Integer Conversion specifiers are used together to represent a long Int or unsigned long int type value (c99). Example: % LLD, % 8llu L and floating point conversion specifiers are used together to represent a long double value. Examples: % lf, % 10.4le T and Integer Conversion specifiers are used together, represents a ptrdiff_t value (the type of the difference between the two pointers) (c99), for example: % TD, % 12ti Z and Integer Conversion specifier, indicates a size_t value (type returned by sizeof) (c99), for example: % ZD, % 12 zxmain (){
Int A = 15;
Float B = 138.3576278;
Double C = 35648256.3645687;
Char d = 'P ';
Printf ("A = % d, % 5d, % O, % x \ n", );
Printf ("B = % F, % lf, % 5.4lf, % E \ n", B );
Printf ("c = % lf, % F, % 8.4lf \ n", C );
Printf ("d = % C, % 8C \ n", D, D );
}
A <-- 15
B <-- 138.3576278.
C <-- 35648256.3645687.
D <-- 'P'

Main ()
{
Int A = 29;
Float B = 1243.2341;
Double C = 24212345.24232;
Char d = 'H ';
Printf ("A = % d, % 5d, % O, % x \ n", );

/* The output format of "% F" and "% lf" is the same, indicating that the "L" character has no effect on the "f" type.
* "% 5.4lf" indicates that the output width is 5 and the precision is 4. Because the actual length exceeds 5, the output should be based on the actual number of digits, and the number of decimal places exceeds 4 digits * is truncated.
*/
Printf ("B = % F, % lf, % 5.4lf, % E \ n", B );

/* Output dual-precision real number. "% 8.4lf" truncated more than four digits due to the specified precision of four digits */
Printf ("c = % lf, % F, % 8.4lf \ n", C );

/* The number of output characters D, where "% 8C" specifies that the output width is 8. Therefore, add 7 spaces before the output character p */
Printf ("d = % C, % 8C \ n", D, D );
Getch ();
} When using the printf function, pay attention to the order of values in the output table column. Different compilation systems may not be the same, either from left to right or from right to left. Turbo C runs from right to left.
Main (){
Int I = 8;
Printf ("% d \ n", ++ I, -- I, I --, I ++, -I --);
} 6. For special usage, the format of M. N can also be expressed using the following method (for example)
Char ch [20];
Printf ("% *. * s \ n", M, N, CH );
* Defines the total width and the number of outputs. The Parameter M and N correspond to the outer parameters respectively. I think the advantage of this method is that the M and N parameters can be assigned outside the statement to control the output format.
Today (06.6.9), we see another output format: % N. You can assign the length value of the output string to another variable, as shown in the following example:

Int slen;

Printf ("Hello world % N", & slen );

After execution, the variable is assigned a value of 11.

I checked again and saw an article (View) saying that the output in this format has been confirmed as a security risk and has been disabled. Search again to find out what overflow, vulnerabilities, and so on are all used for this usage. You can find one: Format String Attack notes

Pay special attention to the usage of % *. * s.

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.