954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

printf

Hi,

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

cutedipti
Light Poster
45 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

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

grumpier
Posting Whiz in Training
211 posts since Aug 2008
Reputation Points: 193
Solved Threads: 32
 

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

trinitybrown
Newbie Poster
16 posts since Oct 2008
Reputation Points: 10
Solved Threads: 2
 

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

dhingra
Newbie Poster
6 posts since Sep 2008
Reputation Points: 10
Solved Threads: 1
 

Here is an example of a printf using va_args.

stilllearning
Posting Whiz
309 posts since Oct 2007
Reputation Points: 161
Solved Threads: 43
 

>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
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

@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?

Sci@phy
Posting Whiz in Training
279 posts since Sep 2008
Reputation Points: 110
Solved Threads: 43
 

>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
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Thank you for your answers :)

Sci@phy
Posting Whiz in Training
279 posts since Sep 2008
Reputation Points: 110
Solved Threads: 43
 

>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

cutedipti
Light Poster
45 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

>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
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You