帮我看看这段代码为什么总是不能得到正确结果?

wabc 2003-04-07 03:42:54
#define ANSI

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


void print(char *fmt,...)
{
va_list a;
va_start(a, fmt);
printf( fmt,a );
va_end(a);
}

main()
{

print ("hello:%d\n",123);

}
...全文
26 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
wabc 2003-04-08
  • 打赏
  • 举报
回复
void print(char *fmt,...)后面的参数是不确定的,我怎么根据应用编程序啊。
netcropper 2003-04-08
  • 打赏
  • 举报
回复
具体的函数当然要根据你的应用目的来编写,MSDN上对此概念已经介绍的很清楚了,你看一下例子就会明白。
va_arg retrieves a value of type from the location given by arg_ptr and increments arg_ptr to point to the next argument in the list, using the size of type to determine where the next argument starts. va_arg can be used any number of times within the function to retrieve the arguments from the list.
In355Hz 2003-04-08
  • 打赏
  • 举报
回复
printf 是通过解析 fmt 字符串确定参数数目,所以传入的 format 字符串 % 树木必须和参数数目相同,如果一定要调用 printf 建议调用他的 valist 版本 vprintf

int vprintf( const char *format, va_list argptr );

void print(char *fmt,...)
{
va_list a;
va_start(a, fmt);

vprintf( fmt, a );

va_end(a);
}
wabc 2003-04-08
  • 打赏
  • 举报
回复
up
wabc 2003-04-07
  • 打赏
  • 举报
回复
但如果print ("hello:%d - %d\n",123,321);
楼上的方法就不灵了
netcropper 2003-04-07
  • 打赏
  • 举报
回复
这样可以:
void print(char *fmt,...)
{
va_list a;
int arg;
va_start(a, fmt);
arg=va_arg(a,int);
printf( fmt,arg );
va_end(a);
}
wabc 2003-04-07
  • 打赏
  • 举报
回复
To netcropper:你的例子是MSDN上的,和我使用的情况不一样啊?

我的代码的问题是并没有把“123”打印出来。
laolaoliu2002 2003-04-07
  • 打赏
  • 举报
回复
warning C4508: 'main' : function should return a value; 'void' return type assumed
netcropper 2003-04-07
  • 打赏
  • 举报
回复
printf( fmt,a );???
print("..")
一个例子:
/* VA.C: The program below illustrates passing a variable
* number of arguments using the following macros:
* va_start va_arg va_end
* va_list va_dcl (UNIX only)
*/

#include <stdio.h>
#define ANSI /* Comment out for UNIX version */
#ifdef ANSI /* ANSI compatible version */
#include <stdarg.h>
int average( int first, ... );
#else /* UNIX compatible version */
#include <varargs.h>
int average( va_list );
#endif

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
chenweihello 2003-04-07
  • 打赏
  • 举报
回复
帮你up一下

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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