Description: printf, sprintf functions
Reprinted from Http://www.cnblogs.com/adslg/archive/2008/08/22/1274164.html
The printf () function is a formatted output function, typically used to output information to a standard output device in a prescribed format: printf ("< formatted string >" < parameter table >), where a formatted string consists of two parts:
1. Normal characters. These characters will be output as-is;
2. Format the specified character. Starts with "%", followed by one or more specified characters, used to determine the format of the output content.
The parameter table is a series of parameters that need to be output, the number must be as many as the number of output parameters as stated in the formatted string, separated by "," between the parameters, and the order one by one corresponds, otherwise an unexpected error will occur.
The format specifier is as follows:
%d decimal signed integer
%u decimal unsigned integer
%f floating Point
%s string
%c character
%p the value of the pointer
Floating-point numbers in the form of%e indices
%x,%x hexadecimal representation of unsigned integers
%0 An octal representation of unsigned integers
%g automatic selection of appropriate representations
1. You can insert a number between the "%" and the letter to indicate the maximum field width, for example "%3d" means the output 3-bit integer number, not enough 3-bit right-aligned; "%9.2f" represents a floating-point number with an output field width of 9, where the decimal place is 2 bits, the integer digit is 6 bits, one digit is Represents the output of a 8-character string, not enough 8 characters aligns aligned.
for integers or strings, if the length exceeds the description of the field width, the actual length of the output;
For floating-point numbers, if the length of the integer part exceeds the field width of the description, the output is sorted by the actual integer digits, and if the fractional length exceeds the indicated decimal width, the output is rounded by the indicated decimal width.
If you use floating-point numbers for the output format of a character or integer, the number after the decimal point represents the Maximum field width, and the number before the decimal point represents the Minimum field width, such as "6.9s" for a string that is not less than 6 and not greater than 9, and if the string length is greater than 9, the content after the 9th character is deleted
2. If you want to add some 0 before the output value, you should add a 0 before the field width, for example, "%04d" means that when you output an integer less than 4, the total width of the previous 0 is 4 bits.
3. You can add a lowercase letter L between "%" and a letter to indicate that the output is a long type, such as "%ld" for the output long Integer, and "%lf" for the output double type of floating-point number.
4. You can add "-" between "%" and the letter to indicate that the output is left-justified, for example "%-7d" means that output 7 is left-justified by integers
Some special provisions of the character
\ nthe line break
\ r Enter
\ t Tab character
\f Clear screen and change page
\XHH denotes an ASCII code in hexadecimal, where HH is 1 to 2 16 decimal
#include <stdio.h>#include<string.h>intMain () {CharC, s[ -], *p; intA =1234, b= A, *i; floatf=3.141592653589f; Doublex =0.12345678987654321; P=" How does do"; strcpy (s),"Hello, comrade."); I= &C; C='\x41'; printf ("a=%d\n", a);/*result output decimal integer a=1234*/printf ("a=%6d\n", a);/*result output 6-bit decimal number a= 1234*/printf ("a=%06d\n", a);/*result output 6-bit decimal number a=001234*/printf ("a=%2d\n", a);/*a over 2 bits, output a=1234 by actual value*/printf ("*i=%4d\n", *i);/*output 4-bit decimal integer *i=*/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 number with 4 digits after the decimal point f=3.1416*/printf ("x=%lf\n", x);/*output long floating point x=0.123457*/printf ("x=%18.16lf\n", x);/*outputs a 18-bit long floating-point number with 16 digits after the decimal point x=0.1234567898765432*/printf ("c=%c\n", c);/*Output character C=a*/printf ("c=%x\n", c);/*ASCII code value of the 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*/printf ("p=%p\n", p);/*the value of the output pointer p=0194*/GetChar (); return 0; }
/*int sprintf (char * str, const char * format, ...); Write formatted data to string[writes the format to string]composes a string with the same text that would be printed if format is U Sed on printf, but instead of being printed, the content was stored as a C string in the buffer pointed by str.[differs from printf () Output formatted data to a standard output device, sprintf () saves formatted data in a C string buffer]the size of the buffer should be large enough to contain the entire Resulti Ng string (see snprintf for a safer version). [The size of the string buffer should be large enough to contain all result strings] A terminating null character is automatically appended after the content. The [sprintf () function automatically adds a null as a string terminator] After the format parameter, the function expects at least as many additional arguments as needed for format. [sprintf () The number of parameter tables needs to correspond to the number of formatting specifiers one by one, and the number is equal] Return ValueOn Success, the total number of characters written is returned. This count does not include the additional null-character automatically appended at the end of the string. [If successful, returns the total number of characters written (without automatically adding a null terminator)] On failure, a negative number is returned. [Returns a negative number if it fails]*/#include<stdio.h>intMain () {Charbuffer[ -]; intN, a=5, b=3; N= sprintf (buffer,"%d plus%d is%d", A, B, a +b); printf ("[%s] is a string%d chars long.\n", buffer, n); GetChar (); return 0;}
Some of the functions in C