关于printf函数的参数问题,简单?!请看看^_^

fi9 2002-09-13 01:52:41

#include <stdlib.h>
#include <stdio.h>

int MyPrintf(int a, ...)
{
fPrintf(stdout, "%d", a);
/*Here, What can I Use ...?!(我怎么用...里面的参数?!)*/

return 0;
}

int main(int n, char *p[])
{
MyPrintf(1, 2, 3, 4, 5);

return 0;
}

/*
或者哪位有更加好的方法,请谈谈
最好大家的方法是ANSI标准的
我想在Windows, Unix, Linux都能通过
像VC BCB GCC等都能通过
*/
...全文
61 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
alexxing 2002-09-20
  • 打赏
  • 举报
回复
嘿嘿,我想你是看了帮助文档里的

int execl(char *path, char *arg0 *arg1, ..., *argn, NULL);

之类的函数原型了吧

这些原型是给人看的,便于理解,编译器是不认的,不信你去看看 process.h 和 exec*.c 的源程序,根本就不是这样

int _RTLENTRY _EXPFUNC execl(const char * __path, const char * __arg0, ...);


... 是可变的,它后面的参数,怎么定位?根本不可能嘛


================================================================
人生最大的幸福莫过于顿顿有玉米吃
(CSDN 论坛助手,挺好使!俺们拨号上网的有福了)
fi9 2002-09-17
  • 打赏
  • 举报
回复
up
liujf 2002-09-16
  • 打赏
  • 举报
回复
up!
fi9 2002-09-16
  • 打赏
  • 举报
回复
没有想出来啊?!那我来自己UP一下!
fi9 2002-09-16
  • 打赏
  • 举报
回复
对啊,我也知道可以放到前面去,可是别人是怎么实现的呢?!有没有想过?!
潘李亮 2002-09-16
  • 打赏
  • 举报
回复
#include "include/base_io.h"
#include "include/stdarg.h"
#define printn(n) printc(n+48)
void conio_clear(void)
{
asm pusha;
asm mov ah,0;
asm mov al,2;
asm int 10h;
asm popa;
}
void conio_putc(char n)
{
asm pusha;
asm mov ah,14;
asm mov cx,1 ;
asm mov bh,0;
asm mov al, n;
asm int 10h ;
asm popa;
}
void conio_set_x(char x)
{
asm pusha;
asm mov ah,3;
asm mov bh,0;
asm int 10h;
asm mov ah, 2;
asm mov dl,x;
asm mov bh,0;
asm int 10h;
asm popa;
}
int conio_get_x()
{
char pos;
asm pusha;
asm mov ah,3;
asm mov bh,0;
asm int 10h;
asm mov pos,dl;
asm popa;
return (int)pos;
}
void printc(char n)
{
int pos;
conio_putc(n);
if(n=='\n')
printc(13);
}
int prints(char* buf)
{
int i=0;
while(buf[i])
{
printc(buf[i]);
i++;
}
return i;
}
void printi(long n,int base)
{
long a;
int c=n%base;
if(a=n/base)
printi(a,base);
else
{
switch(base)
{
case 8:
printc('0');
break;
case 16:
printc('0');
printc('x');
break;
}
}
if(c>9)
printc(c+55);
else
printn(n%base);
}
void printf(char *buf,...)
{
int n;
char* pc;
va_list ap;
va_start(ap,buf);
while(*buf!=0)
{
if(*buf!='%')
printc(*buf);
else
{
switch(*(++buf))
{
case 'd':
n=va_arg(ap,int);
printi(n,10);
break;
case 'b':
n=va_arg(ap,int);
printi(n,2);
break;
case 'o':
n=va_arg(ap,int);
printi(n,8);
break;
case 'x':
n=va_arg(ap,int);
printi(n,16);
break;
case 's':
pc=va_arg(ap,char*);
prints(pc);
break;
default:
printc(*buf);
}
}
buf++;
}
va_end(ap);
}
jotting 2002-09-16
  • 打赏
  • 举报
回复
同意 呵呵 不过比较搞笑
pi1ot 2002-09-16
  • 打赏
  • 举报
回复
放到前面去吧。
fi9 2002-09-16
  • 打赏
  • 举报
回复
谢谢各位!
原来的问题我已经解决了!
//-----------------------
可是又有了新的问题:
char MyPrint(const char *cpFormat, ..., int n)
就是在...后面再加参数,但是,编译不过!报错!
开始我以为是语法错误,可是昨晚当看到
exec函数是才知道也可以这样!
//-----------------------
请大家都来想想!
谢谢啦!
liubear 2002-09-13
  • 打赏
  • 举报
回复
/* 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


Output

Average is: 3
Average is: 8
Average is: 0


dsangvei 2002-09-13
  • 打赏
  • 举报
回复
好了,你自己测试下。
#include <stdio.h>
#include <stdarg.h>

void test_fn( const char *msg,
const char *types,
... );

void main()
{
printf( "VA...TEST\n" );
test_fn( "PARAMETERS: 1, \"abc\", 546",
"isi", 1, "abc", 546 );
test_fn( "PARAMETERS: \"def\", 789",
"si", "def", 789 );
}

static void test_fn(

const char *msg, /* message to be printed */
const char *types, /* parameter types (i,s) */
... ) /* variable arguments */
{
va_list argument;
int arg_int;
char *arg_string;
const char *types_ptr;

types_ptr = types;
printf( "\n%s -- %s\n", msg, types );
va_start( argument, types );
while( *types_ptr != '\0' ) {

if (*types_ptr == 'i') {
arg_int = va_arg( argument, int );
printf( "integer: %d\n", arg_int );
} else if (*types_ptr == 's') {
arg_string = va_arg( argument, char * );
printf( "string: %s\n", arg_string );
}
++types_ptr;
}
va_end( argument );
}

xzhuang 2002-09-13
  • 打赏
  • 举报
回复
char *printf(...)
fi9 2002-09-13
  • 打赏
  • 举报
回复
谢谢jinfeng_wang(G-G-S,D-D-U)!现在好像有点点意思了!
这个问题我是看到Unix的源码后提出的。
但是为什么他的源码是那么简单?!
不知道大家看过没有?!就是那本《???Unix源码》不是很记得全名了。
只记得源码是这样定义的:
char *printf(char *cpFormat, x1, x2, x3, x4, x5, x6, x7, x8, x9, xa xb, xc, xd, xe, xf);
大概是这样吧!可是x1..xf是什么呢?!找不到它们的定义呢?!
aileen_long 2002-09-13
  • 打赏
  • 举报
回复
C++编译器通常对函数参数的入栈的处理顺序是从左向右,且每个参数占用空间与你当前系统中int所占字节相同。因此,只要计算出第一个参数的地址:
BYTE *pbParas = (BYTE*)&a;
第二个参数地址:pbParas += sizeof(int);
有了参数地址,参数的值只要经过简单的相应转换,就可以得出。

69,368

社区成员

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

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