Hi,

I have one query that: Can we write a function similar to printf()? If it is possible then how could it be written?

Recommended Answers

All 10 Replies

Yes. Look up the C standard header stdarg.h or (in C++) the standard header <cstdarg>. Those headers contain macros and types that support writing functions with variable argument lists.

Yes it is possible , but the code will be large enough and not good from good programmers view

the printf is defined in stdio.h and if you want to create similar fun. than you can create a function which can use a char constant format and return a int value so it is very unusual

Here is an example of a printf using va_args.

>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
I see you are using goto .

And I know programmers don't like to use goto, so if I may ask: why? :)
Did you just use it because it's fastest to write code like that?
Or would you still use it because it has more positive than negative sideeffects in this code?

>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.

Thank you for your answers :)

>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.


...ya it's really insulting. But the respective comment is made by other person...

trinitybrown--> Yes it is possible , but the code will be large enough and not good from good programmers view

>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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.