Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Creating your own printf() in C

  1. Sep 17, 2005 #1
    Given this code, I was to finish it by creating the printint(), printstring(), and printhex() functions. I wasn't able to finish the printhex(). And the printint() might not even be right. I know java, but I'm fairly new to C. Could anyone show me what the printhex() should be?

    #include <stdarg.h>

    extern int printchar(int);

    void myprintf(const char *fmt, ...);
    {
    const char *p;
    va_list argp;
    int i;
    char *s;
    char fmtbuf[256];

    va_start(argp, fmt);

    for(p + fmt; *p != '\0'; p++)
    {
    if(*p != '%')
    {
    putchar(*p);
    continue;
    }

    switch(*++p)
    {
    case 'c':
    i = va_arg(argp, int);
    putchar(i);
    break;

    case 'd':
    i = va_arg(argp, int);
    printint(i);
    break;

    case 's':
    s = va_arg(argp, char *);
    printstring(s);
    break;

    case 'x':
    i = va_arg(argp, int);
    printhex(i);
    break;

    case '%':
    putchar('%');
    break;
    }
    }

    va_end(argp);
    }

    void printint(int i)
    {
    int digit;
    digit = i % 10;
    digit = digit + '0';
    i = i / 10;
    putchar(i);
    }

    void printstring(char s)
    {
    putchar(s);
    }

    void printhex(int i)
    {
    }
     
  2. jcsd
  3. Sep 17, 2005 #2

    dduardo

    User Avatar
    Staff Emeritus

    Last edited: Sep 17, 2005
  4. Sep 17, 2005 #3
    The standard c libraries require memory that msp430, the processor for this little toyish robot were going to be programming in lab, does not have. So I guess our professor is just having us practice writing library routines.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Creating your own printf() in C
  1. C++ printf help (Replies: 2)

Loading...