谁知道sprintf是怎么实现的

DDrddr 2003-08-21 05:35:55
在stdio.h中的声明是:
int _Cdecl sprintf (char *buffer, const char *format, ...);
后面的...是怎么处理的呢?
...全文
236 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Dragon132 2003-08-21
  • 打赏
  • 举报
回复
代码如下:

#ifndef _COUNT_

int __cdecl sprintf (
char *string,
const char *format,
...
)
#else /* _COUNT_ */

int __cdecl _snprintf (
char *string,
size_t count,
const char *format,
...
)
#endif /* _COUNT_ */

{
FILE str;
REG1 FILE *outfile = &str;
va_list arglist;
REG2 int retval;

va_start(arglist, format);

_ASSERTE(string != NULL);
_ASSERTE(format != NULL);

outfile->_flag = _IOWRT|_IOSTRG;
outfile->_ptr = outfile->_base = string;
#ifndef _COUNT_
outfile->_cnt = MAXSTR;
#else /* _COUNT_ */
outfile->_cnt = count;
#endif /* _COUNT_ */

retval = _output(outfile,format,arglist);

_putc_lk('\0',outfile); /* no-lock version */

return(retval);
}
Dragon132 2003-08-21
  • 打赏
  • 举报
回复
你运气真好,我刚好有一个例子

#include <stdio.h>
#include <stdlib.h>
#define MAX 20

int main(void)
{
char source[MAX];
char target[MAX];
char command[2*MAX+5];
puts("Enter the name of the file you wish to copy:");
gets(source);
puts("Enter the desired name for the copy:");
gets(target);
sprintf(command,"copy %s %s",source,target);

printf("Executing the following command: %s\n",command);
system(command);
return 0;
}
new1mm 2003-08-21
  • 打赏
  • 举报
回复
/* Copyright (C) 1991, 1995, 1997, 1998, 2002 Free Software Foundation, Inc.
This file is part of the GNU C Library.

The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA. */

#include <stdarg.h>
#include <stdio.h>

#ifdef USE_IN_LIBIO
# include <libio/iolibio.h>
# define vsprintf(s, f, a) INTUSE(_IO_vsprintf) (s, f, a)
#endif

/* Write formatted output into S, according to the format string FORMAT. */
/* VARARGS2 */
int
sprintf (char *s, const char *format, ...)
{
va_list arg;
int done;

va_start (arg, format);
done = vsprintf (s, format, arg);
va_end (arg);

return done;
}
libc_hidden_def (sprintf)

#ifdef USE_IN_LIBIO
strong_alias(sprintf, _IO_sprintf)
#endif

69,382

社区成员

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

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