变参函数加打印

xie6723632 2017-05-27 02:58:32
假设有一个在库里的无法看到源码的变参函数int func(int a,...);如何实现一个函数或者宏实现类似以下功能。

int printf_func(int a,...)
{
printf("the first parameter is %d\n",a);
return func(a,...);
}
可以用宏实现,但是要确保
printf_func(a,...);和if(printf_func(a,...)<0)这种都能正常展开。
多谢了。
...全文
139 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
LubinLew 2017-05-27
  • 打赏
  • 举报
回复

#define printf_func(a, ...)  (printf("the first parameter is %d\n",a), func(a, ##__VA_ARGS__))
幻夢之葉 2017-05-27
  • 打赏
  • 举报
回复
试试

int printf_func(int a, ...)
{
    va_list args;
    va_start(args, a);
    func(a, args);
    va_end(args);
}
赵4老师 2017-05-27
  • 打赏
  • 举报
回复
Calls with a Variable Number of Arguments A partial parameter list can be terminated by the ellipsis notation, a comma followed by three periods (, ...), to indicate that there may be more arguments passed to the function, but no more information is given about them. Type checking is not performed on such arguments. At least one parameter must precede the ellipsis notation and the ellipsis notation must be the last token in the parameter list. Without the ellipsis notation, the behavior of a function is undefined if it receives parameters in addition to those declared in the parameter list. To call a function with a variable number of arguments, simply specify any number of arguments in the function call. An example is the printf function from the C run-time library. The function call must include one argument for each type name declared in the parameter list or the list of argument types. All the arguments specified in the function call are placed on the stack unless the __fastcall calling convention is specified. The number of parameters declared for the function determines how many of the arguments are taken from the stack and assigned to the parameters. You are responsible for retrieving any additional arguments from the stack and for determining how many arguments are present. The STDARGS.H file contains ANSI-style macros for accessing arguments of functions which take a variable number of arguments. Also, the XENIX®- style macros in VARARGS.H are still supported. This sample declaration is for a function that calls a variable number of arguments: int average( int first, ...); Microsoft Specific —> To maintain compatibility with previous versions of Microsoft C, a Microsoft extension to the ANSI C standard allows a comma without trailing periods (,) at the end of the list of parameters to indicate a variable number of arguments. However, it is recommended that code be changed to incorporate the ellipsis notation. END Microsoft Specific
STC单片机的串口UART1,2,3,4的配置。几乎适用于所有STC单片机,4个串口可同时配置使用,函数由结构体封装打包好,非常方便。 --------------------------STC_UART函数目录-------------------------- //注意:若主循环正在打印突然跳到中断中恰好又掉用printf1,2,3,4,恢复后会使主循环中的打印错误 //printf与printf1,2,3,4, 相互独立,可同时用且互不影响 //自定义printf1,2,3,4打印函数打印字符最长限制 #define CMD_BUFFER_LEN 50 //#define UART_Printfx //注释则不使用重定向打印函数 //-------------------------------------------------------------------------------- //UART1,2,3,4初始化 void UART1_Init(u32 BaudRate); void UART2_Init(u32 BaudRate); void UART3_Init(u32 BaudRate); void UART4_Init(u32 BaudRate); //UART1,2,3,4串口打印函数 void printf1 (char *fmt, ...); //变参函数 void printf2 (char *fmt, ...); void printf3 (char *fmt, ...); void printf4 (char *fmt, ...); //UART1,2,3,4发送单个字符 void UART1_SendByte(char dat); void UART2_SendByte(char dat); void UART3_SendByte(char dat); void UART4_SendByte(char dat); //UART1,2,3,4发送字符串 void UART1_SendStr(char *TI_Dat); void UART2_SendStr(char *TI_Dat); void UART3_SendStr(char *TI_Dat); void UART4_SendStr(char *TI_Dat); //UART1,2,3,4接收Leng个字符 void UART1_ReceiveStr(u8 Leng,u8 *dat); void UART2_ReceiveStr(u8 Leng,u8 *dat); void UART3_ReceiveStr(u8 Leng,u8 *dat); void UART4_ReceiveStr(u8 Leng,u8 *dat); //获取期待值 NULL 表示无效的 bit UART_extract(u8 *Puf_0,u8 *Puf_1,u32 time); //从串口中获取所期待的数据 NULL 表示无效的 //计算指针所指数组元素个数 //警告; count(0);返回的是2原因不明 unsigned char count(u8 *p); void UART_Send_Str(char *s); // 发送字符串 void UART_Send_Num(unsigned long dat); // 发送数值 void UART_Send_StrNum(char *inf,unsigned long dat); // 发送字符串+数值 void UART_Send_Hex(unsigned int hex); // 发送16进制(整数范围) void UART_Send_binary(unsigned char dat); // 发送2进制 void UART_Send_Enter(); void UART_Send_Byte(unsigned char dat); //UART初始化 void UART_Init(u8 UARTx, UART_InitTypeDef *UART_InitStructure);

69,369

社区成员

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

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