I need to use a vsprintf function with the ellipsis but realized that mikroC doesn't seem to have a vsprintf function. Is there a way to do what vsprintf does in mikroC? Or is this a lost cause?
Daniel
p.s. va_end doesn't seem to exist either.
Is there a vsprintf?
Re: Is there a vsprintf?
Hi,
Well, thats the sprintf function and it exists as library routine in mikroC.Daniel Wee wrote:I need to use a vsprintf function with the ellipsis...
vsprintf
Drago Srdjan,
with respect it is not the same to use sprintf instead of vsprintf.
Whilst it is true sprintf provides a work around - it makes more complicated to implement overloaded forms of a printf function specific to a given application - for example I use the following function as part of a packet based messaging system - it is fully ANSI C compliant and works with many other embedded compilers.
The variable args support in C is a powerful way of creating handlers for variable data in a useful way - please add it to your list of updates for this otherwise excellent compiler.
Regards,
Jeff
Below example code using vsprintf - impossible to write using sprintf
/*=============================================*/
/* */
/* Message - Send message to external link */
/* */
/*=============================================*/
/*-------------------- Private variables -------------------------*/
SHORT_RESULT SysResultMessage;
char szMessageBuff[SHORT_BLOCK_SIZE];
void Message(BYTE byResultCode, const char *format, ... )
{
va_list ArgumentList;
WORD wCopyCount;
/*-------------------- Initialise pointers -----------------------*/
va_start (ArgumentList, format); // Variable argument begin
/*-------------------- Generate string ---------------------------*/
wCopyCount = vsprintf(szMessageBuff, format, ArgumentList);
va_end (ArgumentList); // Variable argument end
/*-------------------- Generate message --------------------------*/
SysResultMessage.byCommand = byResultCode;
SysResultMessage.wSize = wCopyCount;
if (SysResultMessage.wSize > SHORT_BLOCK_SIZE)
SysResultMessage.wSize = SHORT_BLOCK_SIZE;
SysResultMessage.Response = rcInfo;
for (wCopyCount = 0; wCopyCount < SysResultMessage.wSize; wCopyCount++)
SysResultMessage.byData[wCopyCount] = szMessageBuff[wCopyCount];
SendResult((RESULT*)&SysResultMessage);
/*----------------------------------------------------------------*/
with respect it is not the same to use sprintf instead of vsprintf.
Whilst it is true sprintf provides a work around - it makes more complicated to implement overloaded forms of a printf function specific to a given application - for example I use the following function as part of a packet based messaging system - it is fully ANSI C compliant and works with many other embedded compilers.
The variable args support in C is a powerful way of creating handlers for variable data in a useful way - please add it to your list of updates for this otherwise excellent compiler.
Regards,
Jeff
Below example code using vsprintf - impossible to write using sprintf
/*=============================================*/
/* */
/* Message - Send message to external link */
/* */
/*=============================================*/
/*-------------------- Private variables -------------------------*/
SHORT_RESULT SysResultMessage;
char szMessageBuff[SHORT_BLOCK_SIZE];
void Message(BYTE byResultCode, const char *format, ... )
{
va_list ArgumentList;
WORD wCopyCount;
/*-------------------- Initialise pointers -----------------------*/
va_start (ArgumentList, format); // Variable argument begin
/*-------------------- Generate string ---------------------------*/
wCopyCount = vsprintf(szMessageBuff, format, ArgumentList);
va_end (ArgumentList); // Variable argument end
/*-------------------- Generate message --------------------------*/
SysResultMessage.byCommand = byResultCode;
SysResultMessage.wSize = wCopyCount;
if (SysResultMessage.wSize > SHORT_BLOCK_SIZE)
SysResultMessage.wSize = SHORT_BLOCK_SIZE;
SysResultMessage.Response = rcInfo;
for (wCopyCount = 0; wCopyCount < SysResultMessage.wSize; wCopyCount++)
SysResultMessage.byData[wCopyCount] = szMessageBuff[wCopyCount];
SendResult((RESULT*)&SysResultMessage);
/*----------------------------------------------------------------*/
Re: vsprintf
- I never said it's the same. If you take a look at the quote in my previous post, you'll see that I have stated that "vsprintf function with the ellipsis" is sprintf. Vsprintf does not take ellipsis as its input parameter. However, it may be that I have misunderstood Daniel's request.Dr Jeff wrote:Drago Srdjan,
with respect it is not the same to use sprintf instead of vsprintf.
- Thank you for your kind words and suggestions. We'll see to implement this routine with some of the next releases. I can not give you any deadlines right now.Dr Jeff wrote: Whilst it is true sprintf provides a work around - it makes more complicated to implement overloaded forms of a printf function specific to a given application - for example I use the following function as part of a packet based messaging system - it is fully ANSI C compliant and works with many other embedded compilers.
The variable args support in C is a powerful way of creating handlers for variable data in a useful way - please add it to your list of updates for this otherwise excellent compiler.
Hi Srdjan,
I'm sorry if I've offended - and son't want to worry about who said or typed what - the point is vsprintf is used (with the associated varargs features) to allow effective overloading of printf with our own functions - sprintf can help - but does not provide the same features.
Please add the varargs and vsprintf to the wish list - I am guessing that the varargs features require stack frame manipulation and recovery - with the little I've so far experienced with writing assembler bodies to functions I'm hoping it shouldn't be too hard.
Regards,
Jeff
I'm sorry if I've offended - and son't want to worry about who said or typed what - the point is vsprintf is used (with the associated varargs features) to allow effective overloading of printf with our own functions - sprintf can help - but does not provide the same features.
Please add the varargs and vsprintf to the wish list - I am guessing that the varargs features require stack frame manipulation and recovery - with the little I've so far experienced with writing assembler bodies to functions I'm hoping it shouldn't be too hard.
Regards,
Jeff
- I was not offended at all. I just wanted to clear things out. And yes, you are right, sprintf does not provide the same features as vsprintf.Dr Jeff wrote:Hi Srdjan,
I'm sorry if I've offended - and son't want to worry about who said or typed what - the point is vsprintf is used (with the associated varargs features) to allow effective overloading of printf with our own functions - sprintf can help - but does not provide the same features.
- Note is taken. We'll see to analyze the complicity of implementation and implement implement these features as soon as possible.Dr Jeff wrote: Please add the varargs and vsprintf to the wish list - I am guessing that the varargs features require stack frame manipulation and recovery - with the little I've so far experienced with writing assembler bodies to functions I'm hoping it shouldn't be too hard.