C-Language printf format

Source: Internet
Author: User
Tags truncated

When printing with printf, it turns out that the result is confusing if the data type is inconsistent with the type used for printing.

This is because printf itself does not make the type conversion of the data, he just put the input in the given data format output, if they do not match, then because of different data types of binary storage, the data read is wrong.

So, for the sake of the safe, in the use of printf,fprintf,scanf,fscanf in the type of inconsistency or manual conversion it!

printf ("%f\n", 1.3);

printf ("%d%f\n", 1.3,1.3);

Printing results:

1.300000

-858993459-92559653408872784000000000000000000000000000000000000000000000.000000

The previous line prints correctly, but the last line of 1.3 and%d does not match the print error, and it affects not only the latter but also the print error.

Read into the fscanf with%f read into the float type, to read into the assignment of double type, with%LF

C-Language printf format

the general form of a format string in C is:%[flag [output minimum width] [. precision] [length] type, where the entry in square brackets [] is optional. The meanings of the items are described as follows:1. Type type character used to represent the type of output data, its format character and meaning are shown in the following table:
represents the format characters of the output type     format character meaning A floating-point number, hexadecimal digit, and P-count method (C99) a Floating-point numbers, hexadecimal digits, and P-count (C99) C output a single character
D output signed integer (positive not output symbol) in decimal form e output single, double-precision real e in exponential form single, double-precision real F Output single, double-precision real g in decimal form output single, double-precision real numbers with shorter output widths in%f%e,%e format when exponent is less than 4 or greater than or equal to precision
G output single, double-precision real numbers with shorter output widths in%f%e,%e format when exponent is less than 4 or greater than or equal to precision
I signed decimal integer (same as%d)
o Output unsigned integers in eight binary form (do not output prefix O) P pointer s Output string
x output unsigned integers in 16 binary (not output prefix ox) x output unsigned integers in 16 binary (not output prefix ox)
U output unsigned integer as decimal */Test Common Output type */#include "stdio.h"
#include "conio.h" main ()
{
printf ("The Program Test print style!\n");
/* Output signed integer (positive not output symbol) in decimal form */
printf ("%d\n", 223);
printf ("%d\n",-232);
printf ("\ n"); /* Output unsigned integers in eight binary form (do not output prefix o) */
printf ("%o\n", 223);
printf ("%o\n",-232);
printf ("\ n"); /* Output unsigned integers in 16 binary format (not output prefix ox) */
printf ("%x\n", 223);
printf ("%x\n",-232);
printf ("\ n"); /* Output unsigned integers in decimal form */
printf ("%u\n", 223);
printf ("%u\n",-232);
printf ("\ n"); /* Output single, 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, 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 single, double-precision real numbers with shorter output widths 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. Logo
The flag character is-, +, #, space, and 05, and its meaning is shown in the following table:
Flag format character flag meaning
-Results left-aligned, right-hand blanks
+ Output symbol (plus or minus) The space output value is a positive-time space, minus the negative.
# No effect on C,s,d,u class, O class, prefix 0 on output, X class,
Prefix the output with 0x or 0X; prevent trailing 0 from being deleted on the G,g class; for all floating-point forms, #保证了即使不跟任何数字, also prints a decimal character 0 For all number formats, the field width is populated with leading 0, if the-flag is present or the precision is specified (for integers), ignoring3. Output minimum width
a decimal integer that represents the minimum number of digits for the output. If the actual number of digits is greater than the defined width, the actual number of digits is output, or if the actual number of digits is less than the defined width, a space or 0 is provided. /* Test flag characters are-, +, #, spaces four kinds */#include "stdio.h"
#include "conio.h" main ()
{/* output signed integers in decimal form (positive not output symbols) */
printf ("*%-10d*\n", 223);
printf ("*%+10d*\n",-232);
printf ("*%2d*\n", 223);
printf ("*% #d *\n",-232);
printf ("\ n");
Getch ();/* Output unsigned integers in eight binary form (do not output prefix o) */
printf ("*%-10o*\n", 223);
printf ("*%+10o*\n",-232);
printf ("*%o*\n", 223);
printf ("*% #o *\n",-232);
printf ("\ n");
Getch ();/* Output unsigned integers in 16 binary format (not output prefix ox) */
printf ("$%-10x$\n", 223);
printf ("$%010x$\n",-232);
printf ("$% x$\n", 223);
printf ("$% #x $\n",-232);
printf ("\ n"); /* Output unsigned integers in decimal form */
printf ("%-10u\n", 223);
printf ("%+10u\n",-232);
printf ("% u\n", 223);
printf ("% #u \ n",-232);
printf ("\ n");
Getch (); /* Output single, 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, 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 single, double-precision real numbers with shorter output widths in%f%e */
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!"); * Insufficient Fill space * *
printf ("% #s \ n", "2342o34uo23u");
printf ("\ n");
Getch ();}4. Accuracy
Precision format characters with "." Begins with a decimal integer followed by. The meaning of this item is: If the output number, then the number of decimal places, if the output is a character, the number of output characters, if the actual number of digits is greater than the defined number of precision, the section 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 character is h,l Two, h means output by a short integer, l means output by a long integer quantity. The h and integer conversion specifiers are used together to represent a short int or a numeric value of type unsigned short int, example: %HU,%HX,%6.4HDThe hh and integer conversion specifiers are used together to represent a short int or unsigned short type of numeric value, example:%HHU,%HHX,%6.4HHDThe J and integer conversion specifiers are used together to represent a numeric value of a intmax_t or uintmax_t type, as an example:%JD,%8JXThe L and integer conversion specifiers are used together to represent a long int or a numeric value of type unsigned long int, example: %ld,%8luThe ll and integer conversion specifiers are used together to represent a long int or a numeric value (C99) of type unsigned long int, example: %lld,%8lluL and the floating-point conversion specifier, which represents a long double value, example:%lf,%10.4leThe T and integer conversion specifiers are used together to represent a ptrdiff_t value (a type that corresponds to the difference between two pointers) (C99), example:%td,%12tiThe z and integer conversion specifiers are used together to represent a size_t value (the type that sizeof returns) (C99), example:%zd,%12zxMain () {
int a=15;
float b=138.3576278;
Double c=35648256.3645687;
Char d= ' P ';
printf ("a=%d,%5d,%o,%x\n", a,a,a,a);
printf ("b=%f,%lf,%5.4lf,%e\n", b,b,b,b);
printf ("c=%lf,%f,%8.4lf\n", c,c,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", a,a,a,a);

/* The output of the "%f" and "%LF" formats is the same, indicating that the "L" character has no effect on the "F" type
* "%5.4LF" specifies the output width of 5, the accuracy is 4, because the actual length of more than 5 should be based on the actual number of digits output, the number of decimal place more than 4 * section is truncated
*/
printf ("b=%f,%lf,%5.4lf,%e\n", b,b,b,b);

/* Output double-precision real number, "%8.4LF" because the specified precision is 4 bits and then cut off more than 4 bits of the part */
printf ("c=%lf,%f,%8.4lf\n", c,c,c);

/* Output character D, where "%8c" specifies an output width of 8 so add 7 spaces before the output character p */
printf ("d=%c,%8c\n", d,d);
Getch ();
There is one more thing to note when using the printf function, which is the order of evaluation in the output table column. Different compilation systems are not necessarily the same, either from left to right or from right to left. Turbo c is on right to left
Main () {
int i=8;
printf ("%d\n%d\n%d\n%d\n%d\n", ++i,--i,i--, i++,-i--);
6. Special usage for M.N format can also be represented as follows (example)
Char ch[20];
printf ("%*.*s\n", m,n,ch);
The front of the * defines the total width, and the back defines the number of outputs. corresponding to the outer parameters m and n respectively. I think the benefit of this approach is that you can assign values to parameters m and n outside of the statement to control the output format.
Today (06.6.9) see an output format%n you can assign the length value of the output string to a variable, see the following example:

int Slen;

printf ("Hello world%n", &slen);

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

Check again, see an article (see) said this format output has been identified as a security risk, and has been disabled. Search again. This usage has been used to make what overflow, loopholes and so on, casually found a: formatted string attack notes

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

C-Language printf format

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.