printf, sprintf, output data type

Source: Internet
Author: User
Tags sprintf

Turn from: http://blog.csdn.net/willy3000/article/details/5786888


The parameter type of the character; output form
d,i int type; Displays a signed decimal number (I and D are different when used in the scanf function).
o int type; unsigned octal number (without leading 0)
x,x int type; Displays an unsigned hexadecimal number, X can be used to display digital 0~9 and letters A~F,X can be used to display digital 0~9 and letter a~f.
u int type; unsigned decimal number
c int type; single character
s char * type; the character in the sequential print string until it encounters '/0 ' or the number of characters specified by the precision has been printed.
f double; decimal decimal [-]M.DDDDDD, where the number of D is specified by precision (default is 6)
E,e double; [-]m.dddddde+/-xx or [-]m.dddddde+/-xx, where the number of D is specified by precision (default is 6)
G,g double type, if the exponent is less than-4 or greater than or equal to precision, output in%e or%e format, otherwise output in%f format. The trailing 0 and decimal points do not print.
p void * type; pointer (depending on implementation)
% does not convert parameters; Print a percent percent The letter H or L, the letter H indicates that the integer is printed as the short type, and the letter L indicates that the integer is printed as a long type.


The width used to hold the output data is called the domain width. If the field is wider than the width of the printed data, the data is usually right-aligned in the field. If the output value is wider than the field width, the field width is automatically increased. The field width is usually inserted between the percent semicolon and the conversion descriptor.
printf ("%4d/n", 123)
printf ("%4d/n", 1234)
printf ("%4d/n", 12345)
The output result is:
123
1234
12345

You can also specify the precision of the output data using the printf function. For different data types, precision has different meanings.
For the integer conversion specifier, the precision represents the number of digits to output (if the number of digits being output is less than the specified precision, add 0 to the front of the output value).
printf ("%.4d/n", 873)
printf ("%.9d/n", 873)
The output result is:
0873
000000873

sprintf (S, "%08x", 4567); and sprintf (S, "%.8x", 4567); The two ways of writing are the same.
printf ("%.8x", 4567); and printf ("%08x", 4567); The two ways of writing are the same. %08x and%.8x both indicate that after converting the corresponding variable to 16 unsigned number, if the target number is less than 8 digits, then the left side of 0 to fill 8 bits and then output.

For floating-point conversion specifiers E, E, F, the precision is the number of digits that appear after the decimal point.

printf ("%.3f/n", 123.45678)
printf ("%.3e/n", 123.45678)
The output result is:
123.456
1.235e+02

For floating-point conversion specifiers G, g, the precision is the maximum number of valid digits that are printed out.

printf ("%.3g/n", 123.45678)
The output result is:
123

For the string descriptor s, the precision is the maximum number of characters that are output.

printf ("%.11s/n", "Happy Birthday")
The output result is:
Happy Birth
For example:
Print a string "Hello, World" (12 characters, not included/0), depending on the different conversion instructions produced by different results. We add a colon to the left and right of each field to clearly indicate the width of the field.
:%s:: Hello, World:
:%10s:: Hello, World:
:%.10s:: Hello, wor:
:%-10s:: Hello, World:
:%.15s:: Hello, World:
:%-15s:: Hello, World:
:%15.10s:: Hello, wor:
:%-15.10s:: Hello, wor:
-(minus sign): Causes the output to be left-aligned in the field width.

Example 1. Output integer
#include <stdio.h>
Main ()
    {
     printf ("%d/n ", 455);
     printf ("%i/n", 455);
    /* in printf, I and D are equivalent */
     printf ("%d/n", +455);
      printf ("%hd/n", 32000);
     printf ("%ld/n", 2000000000);
     printf ("%o/n", 455);
     printf ("%u/n", 455);
     printf ("%u/n",-455);
     printf ("%x/n", 455);
     printf ("%x/n", 455);
     return 0;
   }
    above the program output the following results
455
455
455
32000
2000000000
707
455
4294966841
1C7
1c7
printf ("%u/n",-455); output is 4294966841

-1 Original Code 1000000000000001
-1 complement 1111111111111111
-1 becomes unsigned number is 65535
-455 becomes unsigned number output 65081, output by Long integer 4294966841.
Long INT-455 of the source code is
10000000 00000000 00000100 01010101
Complement is
11111111 11111111 11111011 10101011
Become unsigned number is 2~31+2~30+2~29 .....

Similarly:

unsigned short si =-1;
This sentence has an implicit type conversion when it is assigned a value.
If there is no truncation or elevation, the binary content in SI memory is the same as -1, but is interpreted according to the unsigned short type. This and unsigned short si = ' A ' is a truth.
So as long as you write down-1 then it is a signed, and the fact that you assign a value to unsigned or double does not change-1 is signed.
However, the compiler may issue a warning. You can use this method to forcibly assign a value:
unsigned short si = (unsigned short)-1;
Results for si = 0xFFFF (65535)

printf ("%d/n", +455); the output is 455.
%d is a signed integer, but the output does not output a positive sign, but a negative number is output minus. If you want to output a positive sign, then add a plus in front of the%e, and then you can make a conditional judgment on the front.
Example 2. Output floating-point numbers
#include <stdio.h>

Main ()
    {
     printf ("%e/n", 1234567.89);
      printf ("%e/n", +1234567.89);
     printf ("%e/n",-1234567.89);
     printf ("%e/n", 1234567.89);
     printf ("%f/n", 1234567.89);
     printf ("%g/n", 1234567.89);
     printf ("%g/n", 1234567.89);
    
     return 0;
   }
    The above program outputs the following result
1.234568e+06
1.234568e+06
-1.234568e+06
1.234568E+06
1234567.890000
1.234567e+
1.234567E+06

By default, the values printed with the conversion specifier E, E, and F take 6 decimal places.
Example 3. Output domain width
#include <stdio.h>
Main ()
{
printf ("%4d/n", 1);
printf ("%4d/n", 12);
printf ("%4d/n", 123);
printf ("%4d/n", 1234);
printf ("%4d/n/n", 12345);
printf ("%4d/n",-1);
printf ("%4d/n",-12);
printf ("%4d/n",-123);
printf ("%4d/n",-1234);
printf ("%4d/n",-12345);

return 0;
}
The above program outputs the following results
1
12
123
1234
12345

-1
-12
-123
-1234
-12345

You can also use the following method to indicate:
(example) printf ("%*.*s/n", m,n,ch);
The preceding * Defines the total width, and the number of outputs is defined in the rear. corresponding to the outer parameters m and n respectively. The advantage of this approach is that you can assign values to parameters m and n outside of the statement, thereby controlling the output format.
For example
printf ("%*d", 5,5) equals printf ("%5d", 5)
printf ("%*d", 9, 3) equals printf ("%9d", 3)

The function sprintf performs the same transformation as the function printf, but it saves the output to a string.
int sprintf (char *string, Char *format, arg1, arg2, ...);
The sprintf function, like the printf function, formats the parameter sequence arg1, arg2 、... in the format format, but it stores the output in string rather than output to standard output. Of course, the string must be large enough for the output to be stored.

1234/100 If the variable is defined with an int, that is 12, because it is an integral type, leaving only the whole number of parts
If you use the float definition, you can get the decimal part.

printf is the output function of the standard output stream, which is used to output to the standard output device;
fprintf outputs the output to a file on your hard disk.

fprintf (FP, "%s", name); FP is a file pointer.

fprintf (stdout, "%s", name); and printf ("%s", name); is exactly the same (assuming that your standard output is pointing to an explicit

indicator, but the general standard output is the monitor.

sprintf can print integers to strings, and in most cases sprintf can replace Itoa.
For example:
sprintf (S, "%d", 123); Print the integer 123 into a string and save it in S.
sprintf (S, "%8d%8d", 123, 4567); Produces: "123 4567" can specify width, insufficient left fill space.
sprintf (S, "%-8d%8d", 123, 4567); Generated: "123 4567" of which 123 left-aligned.
For example:
It can also be printed as follows in 16.
sprintf (S, "%8x", 4567); Lowercase 16, with a width of 8 positions and right alignment.
sprintf (S, "%-8x", 4568); Uppercase 16, width of 8 positions, left-aligned.
This way, an integer's 16 string is easy to get.

If you want to get the equal-width format for the left complement 0 when printing the 16 feed, add a 0 to the number that represents the width.
For example:
sprintf (S, "%08x", 4567); Produce: "000011d7"
Decimal printing with "%d" can also be used to get the equal-width format using this left 0-complement method.

sprintf (Buff, "%s", name); Buff is an array of characters. Enter name into the string named buff.

sprintf can convert an int type to char by character.
For example:
int a = 12345;
Char s[10] = "";
sprintf (S, "%d", a);
may be s[0]=1; s[1]=2; s[2]=3; s[3]=4; s[4]=5; and converted to a char type.

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.