printf()函数怎么实现的?

kaitty 2003-04-25 04:06:03
int printf(const char *format,[,argument]...)
是怎么定义和实现的?
怎么样定义和实现类型和参数不确定的函数?
...全文
31 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
kaitty 2003-04-25
  • 打赏
  • 举报
回复
3x
aZhuang 2003-04-25
  • 打赏
  • 举报
回复
在函数定义的参数列表中把最后一个参数写成...就可以了。
lang_ren 2003-04-25
  • 打赏
  • 举报
回复
void main( void )
{
/* Call with 3 integers (-1 is used as terminator). */
printf( "Average is: %d\n", average( 2, 3, 4, -1 ) );

/* Call with 4 integers. */
printf( "Average is: %d\n", average( 5, 7, 9, 11, -1 ) );

/* Call with just -1 terminator. */
printf( "Average is: %d\n", average( -1 ) );
}

/* Returns the average of a variable list of integers. */
#ifdef ANSI /* ANSI compatible version */
int average( int first, ... )
{
int count = 0, sum = 0, i = first;
va_list marker;

va_start( marker, first ); /* Initialize variable arguments. */
while( i != -1 )
{
sum += i;
count++;
i = va_arg( marker, int);
}
va_end( marker ); /* Reset variable arguments. */
return( sum ? (sum / count) : 0 );
}
#else /* UNIX compatible version must use old-style definition. */
int average( va_alist )
va_dcl
{
int i, count, sum;
va_list marker;

va_start( marker ); /* Initialize variable arguments. */
for( sum = count = 0; (i = va_arg( marker, int)) != -1; count++ )
sum += i;
va_end( marker ); /* Reset variable arguments. */
return( sum ? (sum / count) : 0 );
}
#endif

CCBeyond 2003-04-25
  • 打赏
  • 举报
回复
VC中的实现可以通过以上方式
CCBeyond 2003-04-25
  • 打赏
  • 举报
回复
// 写入跟踪文件
void PrintMessage( LPSTR lpszFormat, ... )
{
FILE* fp;
char aBuffer[MAX_PATH];
va_list va;

va_start( va, lpszFormat );
vsprintf( aBuffer, lpszFormat, va );
va_end( va );

fp = fopen( "c:\\BSDebug.txt", "a" );
if( fp )
{
fprintf( fp, "%s", aBuffer );
fclose( fp );
}
}
kaitty 2003-04-25
  • 打赏
  • 举报
回复
什么啊,文不对题
ArrowHead 2003-04-25
  • 打赏
  • 举报
回复
#include <stdio.h>

void main( void )
{
char ch = 'h', *string = "computer";
int count = -9234;
double fp = 251.7366;
wchar_t wch = L'w', *wstring = L"Unicode";

/* Display integers. */
printf( "Integer formats:\n"
"\tDecimal: %d Justified: %.6d Unsigned: %u\n",
count, count, count, count );

printf( "Decimal %d as:\n\tHex: %Xh C hex: 0x%x Octal: %o\n",
count, count, count, count );

/* Display in different radixes. */
printf( "Digits 10 equal:\n\tHex: %i Octal: %i Decimal: %i\n",
0x10, 010, 10 );

/* Display characters. */

printf("Characters in field (1):\n%10c%5hc%5C%5lc\n", ch, ch, wch, wch);
wprintf(L"Characters in field (2):\n%10C%5hc%5c%5lc\n", ch, ch, wch, wch);

/* Display strings. */

printf("Strings in field (1):\n%25s\n%25.4hs\n\t%S%25.3ls\n",
string, string, wstring, wstring);
wprintf(L"Strings in field (2):\n%25S\n%25.4hs\n\t%s%25.3ls\n",
string, string, wstring, wstring);

/* Display real numbers. */
printf( "Real numbers:\n\t%f %.2f %e %E\n", fp, fp, fp, fp );

/* Display pointer. */
printf( "\nAddress as:\t%p\n", &count);

/* Count characters printed. */
printf( "\nDisplay to here:\n" );
printf( "1234567890123456%n78901234567890\n", &count );
printf( "\tNumber displayed: %d\n\n", count );
}

==============================================================
Output

Integer formats:
Decimal: -9234 Justified: -009234 Unsigned: 4294958062
Decimal -9234 as:
Hex: FFFFDBEEh C hex: 0xffffdbee Octal: 37777755756
Digits 10 equal:
Hex: 16 Octal: 8 Decimal: 10
Characters in field (1):
h h w w
Characters in field (2):
h h w w
Strings in field (1):
computer
comp
Unicode Uni
Strings in field (2):
computer
comp
Unicode Uni
Real numbers:
251.736600 251.74 2.517366e+002 2.517366E+002

Address as: 0012FFAC

Display to here:
123456789012345678901234567890
Number displayed: 16

18,356

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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