本篇文章主要介绍了"sprintf 与 strchr"，主要涉及到sprintf 与 strchr方面的内容，对于sprintf 与 strchr感兴趣的同学可以参考一下。
int sprintf ( char * str, const char * format, ... );
Write formatted data to string
Composes a string with the same text that would be printed if format was used on printf, but instead of being printed, the content is stored as a C string in the buffer pointed by str.
The size of the buffer should be large enough to contain the entire resulting string (see snprintf for a safer version).
A terminating null character is automatically appended after the content.
After the format parameter, the function expects at least as many additional arguments as needed for format.
Pointer to a buffer where the resulting C-string is stored.
The buffer should be large enough to contain the resulting string.
C string that contains a format string that follows the same specifications as format in printf (see printf for details).
... (additional arguments)
Depending on the format string, the function may expect a sequence of additional arguments, each containing a value to be used to replace a format specifier in the format string (or a pointer to a storage location, for n).
There should be at least as many of these arguments as the number of values specified in the format specifiers. Additional arguments are ignored by the function.
On 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.
On failure, a negative number is returned.
/* sprintf example */
int main ()
char buffer ;
int n, 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);
[5 plus 3 is 8] is a string 13 chars long
sprintf 最常见的应用之一莫过于把整数打印到字符串中，所以，sprintf 在大多数场合可以替代
//把整数123 打印成一个字符串保存在s 中。
sprintf(s, "%d", 123); //产生"123"
sprintf(s, "%8d%8d", 123, 4567); //产生：" 123 4567"
sprintf(s, "%-8d%8d", 123, 4567); //产生："123 4567"
sprintf(s, "%8x", 4567); //小写16 进制，宽度占8 个位置，右对齐
sprintf(s, "%-8X", 4568); //大写16 进制，宽度占8 个位置，左对齐
const char * strchr ( const char * str, int character );
char * strchr ( char * str, int character );
Locate first occurrence of character in string
Returns a pointer to the first occurrence of character in the C string str.
The terminating null-character is considered part of the C string. Therefore, it can also be located in order to retrieve a pointer to the end of a string.
Character to be located. It is passed as its int promotion, but it is internally converted back to char for the comparison.
A pointer to the first occurrence of character in str.
If the character is not found, the function returns a null pointer.
In C, this function is only declared as:
char * strchr ( const char *, int );
instead of the two overloaded versions provided in C++.
/* strchr example */
int main ()
char str = "This is a sample string";
char * pch;
printf ("Looking for the 's' character in \"%s\"...\n",str);
printf ("found at %d\n",pch-str+1);
Looking for the 's' character in "This is a sample string"...
found at 4
found at 7
found at 11
found at 18