sprintf用法

匚匚 2010-03-04 08:12:06

如何将整型数组的数值以百分之D的格式输入到字符数组中,最后用atoi将其一个个正确的输出?
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int buf[] = {5,1,2,3,8},i;
char str[16];
for(i=0; i<5; i++)
sprintf(str,"%d",buf[i]);
printf("%s\n",str);
i=atoi(str);
printf("%d\n",i);
return 0;
}
...全文
279 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
shiweifu 2010-03-05
  • 打赏
  • 举报
回复
LZ请上眼~
把代码跑一遍就懂了
shiweifu 2010-03-05
  • 打赏
  • 举报
回复
sprintf function
int sprintf ( char * str, const char * format, ... ); <cstdio>

Write formatted data to string

Writes into the array pointed by str a C string consisting on a sequence of data formatted as the format argument specifies. After the format parameter, the function expects at least as many additional arguments as specified in format.
This function behaves exactly as printf does, but writing its results to a string instead of stdout. The size of the array passed as str should be enough to contain the entire formatted string.


Parameters
str
Pointer to an array of char elements where the resulting C string is stored.
format
C string that contains the text to be written to the buffer.
It can optionally contain embedded format tags that are substituted by the values specified in subsequent argument(s) and formatted as requested.
The number of arguments following the format parameters should at least be as much as the number of format tags.
The format tags follow this prototype:

%[flags][width][.precision][length]specifier
Where specifier is the most significant one and defines the type and the interpretation of the value of the coresponding argument:
specifier Output Example
c Character a
d or i Signed decimal integer 392
e Scientific notation (mantise/exponent) using e character 3.9265e+2
E Scientific notation (mantise/exponent) using E character 3.9265E+2
f Decimal floating point 392.65
g Use the shorter of %e or %f 392.65
G Use the shorter of %E or %f 392.65
o Signed octal 610
s String of characters sample
u Unsigned decimal integer 7235
x Unsigned hexadecimal integer 7fa
X Unsigned hexadecimal integer (capital letters) 7FA
p Pointer address B800:0000
n Nothing printed. The argument must be a pointer to a signed int, where the number of characters written so far is stored.
% A % followed by another % character will write % to the string.

The tag can also contain flags, width, .precision and modifiers sub-specifiers, which are optional and follow these specifications:

flags description
- Left-justify within the given field width; Right justification is the default (see width sub-specifier).
+ Forces to preceed the result with a plus or minus sign (+ or -) even for positive numbers. By default, only negative numbers are preceded with a - sign.
(space) If no sign is going to be written, a blank space is inserted before the value.
# Used with o, x or X specifiers the value is preceeded with 0, 0x or 0X respectively for values different than zero.
Used with e, E and f, it forces the written output to contain a decimal point even if no digits would follow. By default, if no digits follow, no decimal point is written.
Used with g or G the result is the same as with e or E but trailing zeros are not removed.
0 Left-pads the number with zeroes (0) instead of spaces, where padding is specified (see width sub-specifier).


width description
(number) Minimum number of characters to be printed. If the value to be printed is shorter than this number, the result is padded with blank spaces. The value is not truncated even if the result is larger.
* The width is not specified in the format string, but as an additional integer value argument preceding the argument thas has to be formatted.

.precision description
.number For integer specifiers (d, i, o, u, x, X): precision specifies the minimum number of digits to be written. If the value to be written is shorter than this number, the result is padded with leading zeros. The value is not truncated even if the result is longer. A precision of 0 means that no character is written for the value 0.
For e, E and f specifiers: this is the number of digits to be printed after de decimal point.
For g and G specifiers: This is the maximum number of significant digits to be printed.
For s: this is the maximum number of characters to be printed. By default all characters are printed until the ending null character is encountered.
For c type: it has no effect.
When no precision is specified, the default is 1. If the period is specified without an explicit value for precision, 0 is assumed.

.* The precision is not specified in the format string, but as an additional integer value argument preceding the argument thas has to be formatted.


length description
h The argument is interpreted as a short int or unsigned short int (only applies to integer specifiers: i, d, o, u, x and X).
l The argument is interpreted as a long int or unsigned long int for integer specifiers (i, d, o, u, x and X), and as a wide character or wide character string for specifiers c and s.
L The argument is interpreted as a long double (only applies to floating point specifiers: e, E, f, g and G).



additional arguments
Depending on the format string, the function may expect a sequence of additional arguments, each containing one value to be inserted instead of each %-tag specified in the format parameter, if any. There should be the same number of these arguments as the number of %-tags that expect a value.

Return Value
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.

Example
/* sprintf example */
#include <stdio.h>

int main ()
{
char buffer [50];
int n, a=5, b=3;
n=sprintf (buffer, "%d plus %d is %d", a, b, a+b);
printf ("[%s] is a %d char long string\n",buffer,n);
return 0;
}


Output:
[5 plus 3 is 8] is a 13 char long string

shiweifu 2010-03-05
  • 打赏
  • 举报
回复
atoi function
int atoi ( const char * str ); <cstdlib>

Convert string to integer

Parses the C string str interpreting its content as an integral number, which is returned as an int value.

The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.

The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.

If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.


Parameters
str
C string beginning with the representation of an integral number.

Return Value
On success, the function returns the converted integral number as an int value.
If no valid conversion could be performed, a zero value is returned.
If the correct value is out of the range of representable values, INT_MAX or INT_MIN is returned.

Example
/* atoi example */
#include <stdio.h>
#include <stdlib.h>

int main ()
{
int i;
char szInput [256];
printf ("Enter a number: ");
fgets ( szInput, 256, stdin );
i = atoi (szInput);
printf ("The value entered is %d. The double is %d.\n",i,i*2);
return 0;
}


Output:

Enter a number: 73The value entered is 73. The double is 146.

xboy 2010-03-04
  • 打赏
  • 举报
回复
sorry呀,实在是愚钝了。
洗了睡去,
xboy 2010-03-04
  • 打赏
  • 举报
回复
不好意思误导你了,
atoi 接受的必须是字符串
换个办法吧

char ctmp=str[i+1];
str[i+1] = '\0';

atoi(str+i);

str[i+1] =ctmp;
====================

以上代码,你再结合上下文改动一下吧
匚匚 2010-03-04
  • 打赏
  • 举报
回复
你试过了吗?真的行吗?
引用 13 楼 xboy 的回复:
atoi(str[i]);
匚匚 2010-03-04
  • 打赏
  • 举报
回复
对这个论坛,我不在乎什么分不分的,有要的话,本人奉送!
xboy 2010-03-04
  • 打赏
  • 举报
回复
atoi(str[i]);
匚匚 2010-03-04
  • 打赏
  • 举报
回复

是吗?
如果我要单独使用5,1,2,3,8中的一个呢,而且要是atoi输出的?
引用 11 楼 xboy 的回复:
printf("%s\n",str); //这里不是输出了么

结贴哦
xboy 2010-03-04
  • 打赏
  • 举报
回复
printf("%s\n",str); //这里不是输出了么

结贴哦
insulted 2010-03-04
  • 打赏
  • 举报
回复


//sprintf(str,"%d",buf[i]);
sprintf(str+i, "%d", buf[i]); // 注意在同一个地址上不要覆盖了

C云 2010-03-04
  • 打赏
  • 举报
回复
讲得好啊。借鉴了。不过可以说得更详细些,高手在哪呢,一起来讨论
匚匚 2010-03-04
  • 打赏
  • 举报
回复
可以是版主对我的帖子或以往的帖子不满意,哈哈,哈哈
匚匚 2010-03-04
  • 打赏
  • 举报
回复
我先发了一个,但没看见,所以又发了一个。
我现在仍然只看见一个帖子。

这个论坛有点怪,经常登录了,却说是游客
引用 4 楼 xboy 的回复:
晕,被引用了哈哈,楼主发了两个帖子。

你跟版主留个言,退一个帖子的分吧。造业啊,浪费了分
musiclee 2010-03-04
  • 打赏
  • 举报
回复
引用 4 楼 xboy 的回复:
晕,被引用了哈哈,楼主发了两个帖子。

你跟版主留个言,退一个帖子的分吧。造业啊,浪费了分


- -!
匚匚 2010-03-04
  • 打赏
  • 举报
回复
如何从字符数组中输出五个数值呢?
xboy 2010-03-04
  • 打赏
  • 举报
回复
晕,被引用了哈哈,楼主发了两个帖子。

你跟版主留个言,退一个帖子的分吧。造业啊,浪费了分
yyg990441 2010-03-04
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>

int main() {
int buf[] = {5,1,2,3,8},i;
char str[16];

for(i=0; i <5; i++)
sprintf(str+i,"%d",buf[i]); //这样也行的

printf("%s\n",str);
i=atoi(str);
printf("%d\n",i);

system("pause");
return 0;
}
yyg990441 2010-03-04
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>

int main() {
int buf[] = {5,1,2,3,8},i;
char str[16];

for(i=0; i <5; i++)
sprintf(str+i,"%d",buf[i]); //这样也行的

printf("%s\n",str);
i=atoi(str);
printf("%d\n",i);

system("pause");
return 0;
}
xboy 2010-03-04
  • 打赏
  • 举报
回复
int main(void) 
{
int buf[] = {5,1,2,3,8},i;
char str[16];

for(i=0; i <5; i++)
sprintf(str+i,"%d",buf[i]); // is ok
printf("%s\n",str);
i=atoi(str);
printf("%d\n",i);

system("pause");
return 0;
}

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧