make printf using printf?

This is a discussion on make printf using printf? within the C Programming forums, part of the General Programming Boards category; Homework Going to implement my own printf-function. I could need some hints on how to get started, what exactly do ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    5

    Post make printf using printf?

    Homework Going to implement my own printf-function. I could need some hints on how to get started, what exactly do they want me to do? I know basic C programming, so that is not a problem.. I have heard va_* macros mentioned.. Anyone?

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,169
    va_start(), va_arg(), and va_end() are definitely good things to look at. Check out the man pages for them for explanations and examples.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,169
    Here's a really sad one that only handles %d and %s without any precision modifiers and doesn't return any value:
    Code:
    itsme@itsme:~/C$ cat myprintf.c
    #include <stdio.h>
    #include <stdarg.h>
    
    void myprintf(char *fmt, ...)
    {
      char *p;
      va_list ap;
    
      va_start(ap, fmt);
    
      for(p = fmt;*p;p++)
      {
        if(*p != '%')
          putchar(*p);
        else
        {
          p++;
          switch(*p)
          {
            case 'd':
            {
              int num = va_arg(ap, int);
              int mult, init = 0;
    
              if(num < 0)
              {
                putchar('-');
                num *= -1;
              }
              for(mult = 1000000000;mult > 0;mult /= 10)
                if(init || num/mult%10)
                {
                  putchar(num/mult%10+'0');
                  init = 1;
                }
              break;
            }
            case 's':
            {
              char *s = va_arg(ap, char *);
    
              while(*s)
                putchar(*s++);
              break;
            }
            default:
              putchar(*p);
          }
        }
      }
    
      va_end(ap);
    }
    
    int main(void)
    {
      myprintf("%d %s %s %d %s %s...\n",
        4, "score", "and", 7, "years", "ago");
      return 0;
    }
    itsme@itsme:~/C$ ./myprintf
    4 score and 7 years ago...
    itsme@itsme:~/C$
    Last edited by itsme86; 11-08-2004 at 01:37 PM.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Nov 2004
    Posts
    5
    Thanks alot itsyou86

    I'll have a look at both the man-example and the code you posted. Thanks!

  5. #5
    Registered User
    Join Date
    Nov 2004
    Posts
    5
    okei, a few questions...
    whats up with the for-loop where mult = 100000000000?
    'd' is int, what is double? float? 'x'?
    I see strings are just many chars, but still I have trouble making a working case 'c'. not much help from the stdarg-manual..
    and float-cases.. hmm...

  6. #6
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Depending on the specifics of your homework, you may be getting too complicated. Look into the vfprintf() function.

  7. #7
    Registered User
    Join Date
    Nov 2004
    Posts
    5
    Ah, sorry guys, but I have some trouble finishing this. It is the %c and %x parameters that is giving me trouble. I it a very easy task, because I dont need to implement anything else than the %d, %s, %x and %c parameters. No flags, etc. Could anyone help me out with x and c?

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,169
    Well, I can see why the %d and %s ones wouldn't be giving you any trouble...I did them for you. If I helped with those other two then I'm afraid you wouldn't bother learning anything. What (specifically) are you having problems with in those two?
    If you understand what you're doing, you're not learning anything.

  9. #9
    Registered User
    Join Date
    Nov 2004
    Posts
    5
    Damn, I got the %c right now. Thx for not helping, one realy learn better when not given the solution Anyway, what is the %x supposed to do? Is it the hex or ascii we give as input? can you give an printf-example of using %x? thanks

  10. #10
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,169
    %x expects an integer value. Something like printf("%x\n", 0x8c);

    I'm not against helping, but your question was too vague to give you an answer without doing the whole thing for you. Your last question was about something specific and I was able to answer it.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. IF CONDITION plese help
    By birumut in forum C Programming
    Replies: 12
    Last Post: 03-06-2009, 08:48 PM
  2. Simple C question: user input to repeat a loop
    By evernaut in forum C Programming
    Replies: 2
    Last Post: 11-18-2006, 08:23 AM
  3. Drawing tables in C
    By stanoman in forum C Programming
    Replies: 5
    Last Post: 10-09-2003, 10:14 AM
  4. Replies: 4
    Last Post: 03-31-2003, 11:49 PM
  5. Please help debug
    By Wexy in forum C Programming
    Replies: 2
    Last Post: 11-10-2002, 11:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21