[to] output formats for cout in printf and C + + in C + +

Source: Internet
Author: User

Original address

One, Printf output 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 is used to represent the type of output data, its format character and meaning are shown in the following table:
Represents the format character format character meaning of the output type
A floating-point number, hexadecimal digit, and P-count method (C99)
A floating-point number, hexadecimal digit, and P-count method (C99)
C Output Single character
D output signed integers in decimal form (positive numbers do not output symbols)
E output single, double-precision real numbers in exponential form
E output single, double-precision real numbers in exponential form
F output single, double-precision real numbers in decimal form
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
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 form (not output prefix ox)
X output unsigned integers in 16-binary form (not output prefix ox)
U output unsigned integers in decimal form

#include "stdio.h"
#include "conio.h"
Main ()
{
printf ("The Program Test print style!\n");

printf ("%d\n", 223);
printf ("%d\n",-232);
printf ("\ n");

printf ("%o\n", 223);
printf ("%o\n",-232);
printf ("\ n");

printf ("%x\n", 223);
printf ("%x\n",-232);
printf ("\ n");

printf ("%u\n", 223);
printf ("%u\n",-232);
printf ("\ n");

printf ("%f\n", 223.11);
printf ("%f\n", 232.11111111);
printf ("%f\n",-223.11);
printf ("%f\n",-232.11111111);
printf ("\ n");

printf ("%e\n", 223.11);
printf ("%e\n", 232.11111111);
printf ("%e\n",-223.11);
printf ("%e\n",-232.11111111);
printf ("\ n");

printf ("%g\n", 223.11);
printf ("%g\n", 232.111111111111);
printf ("%g\n",-223.11);
printf ("%g\n",-232.111111111111);
printf ("\ n");

printf ("%c\n", ' a ');
printf ("%c\n", 97);
printf ("\ n");

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,
The output is prefixed with 0x or 0X, and the G,g class prevents trailing 0 from being deleted;
For all floating-point forms, #保证了即使不跟任何数字, also prints a decimal character
0 for all number formats, fill the field width with leading 0, if present-flag or specify precision (for integers), ignore
3. 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.

#include "stdio.h"
#include "conio.h"
Main ()
{

printf ("*%-10d*\n", 223);
printf ("*%+10d*\n",-232);
printf ("*-*\n", 223);
printf ("*% #d *\n",-232);
printf ("\ n");
Getch ();

printf ("*%-10o*\n", 223);
printf ("*%+10o*\n",-232);
printf ("*%o*\n", 223);
printf ("*% #o *\n",-232);
printf ("\ n");
Getch ();

printf ("$%-10x$\n", 223);
printf ("$0x$\n",-232);
printf ("$% x$\n", 223);
printf ("$% #x $\n",-232);
printf ("\ n");

printf ("%-10u\n", 223);
printf ("%+10u\n",-232);
printf ("% u\n", 223);
printf ("% #u \ n",-232);
printf ("\ n");
Getch ();

printf ("%-10f\n", 223.11);
printf ("%+10f\n", 232.11111111);
printf ("% f\n",-223.11);
printf ("% #f \ n",-232.11111111);
printf ("\ n");
Getch ();

printf ("%-10e\n", 223.11);
printf ("%+10e\n", 232.11111111);
printf ("% e\n",-223.11);
printf ("% #e \ n",-232.11111111);
printf ("\ n");
Getch ();

printf ("%-10g\n", 223.11);
printf ("%+10g\n", 232.111111111111);
printf ("% g\n",-223.11);
printf ("% #g \ n",-232.111111111111);
printf ("\ n");
Getch ();

printf ("%-10c\n", ' a ');
printf ("%+10c\n", 97);
printf ("% c\n", ' a ');
printf ("% #c \ n", 97);
printf ("\ n");
Getch ();

printf ("%-20s\n", "This is a test!");
printf ("%+20s\n", "2342o34uo23u");
printf ("% 20s\n", "This is a test!");
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.

#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.4hd
The HH and integer conversion specifiers are used together to represent a short int or unsigned short type of numeric value, example:
%hhu,%hhx,%6.4hhd
The 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,%8jx
The L and integer conversion specifiers are used together to represent a long int or a numeric value of type unsigned long int, example:
%ld,%8lu
The 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,%8llu
L and the floating-point conversion specifier, which represents a long double value, example:%lf,.4le
The 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,ti
The z and integer conversion specifiers are used together to represent a size_t value (the type that sizeof returns) (C99), example:%ZD,ZX
Main () {
int a=15;
float b=138.3576278;
Double c=35648256.3645687;
Char d= ' P ';
printf ("a=%d,],%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,?\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,],%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,?\n", d,d);
Getch ();
}
One more thing to note when using the printf function 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

The format for M.N 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.

Second, C + + cout output format

The following header files are often seen in C + + programs
   #include <iomanip>
IO represents the input and output, MANIP is the abbreviation of the manipulator (manipulator)
The role of Iomanip:
Mainly for cin,cout, such as some manipulation operators, such as setfill,setw,setbase,setprecision and so on. It is the I/O flow control header file, just like the formatted output in C. Here are some common control functions:
   Dec Base is 10 equivalent to "%d"
Hex base is 16 equivalent to "%x"
The OCT has a base of 8 equivalent to "%o"
Setfill (' C ') set the fill character to C
Setprecision (n) Set display decimal precision is n bit
SETW (n) Set a field width of n characters

This control means that the output width is guaranteed to be N. Such as:
cout << SETW (3) << 1 << setw (3) << << SETW (3) << + << Endl; The output result is
1 10100 (the default is right-aligned) when the output length is greater than 3 o'clock (<<1000), SETW (3) does not work.
▲SETW (N) Usage: Popularly speaking is preset width
such as COUT&LT;&LT;SETW (5) <<255<<endl;
The result is:
Spaces (space) 255
▲setfill (char c) Usage: that is, if there is already a unused width size in the preset width, fill with the character C that is set
such as Cout<<setfill (' @ ') &LT;&LT;SETW (5) <<255<<endl;
The result is:
@@255
▲setbase (int n): Converts a number to n binary.
such as Cout<<setbase (8) &LT;&LT;SETW (5) <<255<<endl;
Cout<<setbase (&LT;&LT;SETW) (5) <<255<<endl;
Cout<<setbase (<<255<<endl;)
The result is:
(space) (space) 377
Spaces (space) 255
Spaces (space) F F
▲setprecision usage
Use Setprecision (n) to control the number of floating-point numbers displayed by the output stream. The default stream output value for C + + is 6.
If Setprecision (n) is combined with setiosflags (ios::fixed), you can control the number of digits to the right of the decimal point. Setiosflags (ios::fixed) is a fixed-point representation of real numbers.
If combined with setiosnags (ios::scientific), you can control the number of decimal digits of exponential notation. Setiosflags (ios::scientific) is an exponential representation of real numbers.
Setiosflags (ios::fixed) fixed floating-point display
Setiosflags (ios::scientific) index indicates
Setiosflags (ios::left) Align Left
Setiosflags (ios::right) Align Right
Setiosflags (IOS::SKIPWS) ignores leading whitespace
Setiosflags (ios::uppercase) 16 Decimal Capital Output
Setiosflags (ios::lowercase) 16 binary lowercase output
Setiosflags (ios::showpoint) force display decimal point
Setiosflags (ios::showpos) force display symbols
Example:
#include <iostream.h>
#include <iomanip.h>
using namespace Std;
int main ()
{
cout<<12345.0<<endl;//Output "12345"
Cout<<setiosflags (ios::fixed) <<setprecision (3) <<1.2345<<endl; output "1.235"
Cout<<setiosflags (ios::scientific) <<12345.0<<endl;//output "1.234500e+004"
Cout<<setprecision (3) <<12345.0<<endl;//output "1.235e+004" (1.235e+004 should be changed to 1.23e+004)
return 0;
}

[to] output formats for cout in printf and C + + 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.