>Yes it is possible
Correct.
>but the code will be large enough and not good from good programmers view
Um, how do you think printf was written? Granted, cutedipti probably doesn't compare to the quality of programmer that writes the standard library code, but suggesting that the code won't be good is kind of insulting.
By the way, the code for printf is long because it does a lot of work and interfaces with quite a bit of framework. If you check the source code for any commercial version, you'll find that while long and complex, the code is actually quite lean. Similar functions that take a variable number of arguments but do less work will be quite a bit shorter.
>Can we write a function similar to printf()?
Yes, here's an example (possibly buggy, and longer than it could be if I spent time on it):
#include <stdarg.h>
int spaste ( char *dst, size_t size, const char *fmt, ... )
{
va_list args;
const char *fill;
int rc = 0;
va_start ( args, fmt );
while ( --size > 0 ) {
switch ( *fmt ) {
case '\0':
rc = 1;
goto finish;
case '%':
switch ( *++fmt ) {
case 's':
fill = va_arg ( args, const char * );
while ( *fill != '\0' && --size < (size_t)-1 )
*dst++ = *fill++;
if ( *fill != '\0' )
goto finish;
++fmt;
break;
case '%':
*dst++ = '%';
break;
default:
goto finish;
}
break;
default:
*dst++ = *fmt++;
break;
}
}
finish:
*dst = '\0';
va_end ( args );
return rc;
}
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>I see you are using goto.
Every time I use goto, somebody jumps on it like ants on a cheese danish. Sometimes I wonder what their intentions are, but I'm afraid I already know the answer to that question. ;)
>And I know programmers don't like to use goto
Many don't even know why they don't like goto. They were told it's evil by their mentors and propagate that myth without fully understanding the feature or the reasoning behind avoiding it. You can verify this by asking why they don't like goto (they'll give you the classic canned answers) and when goto should be used (they'll invariably say "never").
>so if I may ask: why?
Laziness. I wasn't interested in spending extra time to polish the code. What you see is shooting from the hip draft code. However, that's not to say that in polishing I would necessarily remove the gotos, just that I didn't fully weigh the cost to benefit ratio and went for the quickest, cleanest solution.
>Did you just use it because it's fastest to write code like that?
I rarely worry about performance beyond a bird's eye view. This makes it easier to optimize when I need to and doesn't waste my time optimizing when I don't.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>But the respective comment is made by other person...
I'm aware of that. I was defending you, in case it wasn't obvious. Further, I'd like to amend my own statement:
Granted, cutedipti probably doesn't compare to the quality of programmer that writes the standard library code yet, but suggesting that the code won't be good is kind of insulting.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401