Creating your own printf() in C

  • Thread starter Thread starter Tony11235
  • Start date Start date
Click For Summary
The discussion revolves around completing a custom implementation of a printf-like function in C, specifically focusing on the printint(), printstring(), and printhex() functions. The printhex() function is particularly highlighted, with a suggested implementation that involves bitwise operations to extract and print hexadecimal digits. The provided code snippet includes a basic structure for myprintf, which handles different format specifiers. Concerns are raised about the necessity of creating a custom printf function, with suggestions to refer to existing implementations for guidance. The context of the project involves programming on an MSP430 processor, which has memory limitations that necessitate writing custom library routines for educational purposes.
Tony11235
Messages
254
Reaction score
0
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)
{
}
 
Computer science news on Phys.org
Last edited:
dduardo said:
Why are you creating your own printf function? Don't try to reinvent the wheel.

For the hex you'll basically want to do this:

for (int a=2*sizeof(int) - 1; a>=0; a--) {
putchar("0123456789ABCDEF"[((i >> a*4) & 0xF)]);
}

[edit] Look at a printf source if you like:

http://cvs.opensolaris.org/source/xref/usr/src/uts/common/os/printf.c

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.
 
Thread 'ChatGPT Examples, Good and Bad'
I've been experimenting with ChatGPT. Some results are good, some very very bad. I think examples can help expose the properties of this AI. Maybe you can post some of your favorite examples and tell us what they reveal about the properties of this AI. (I had problems with copy/paste of text and formatting, so I'm posting my examples as screen shots. That is a promising start. :smile: But then I provided values V=1, R1=1, R2=2, R3=3 and asked for the value of I. At first, it said...

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 36 ·
2
Replies
36
Views
3K
Replies
89
Views
6K
  • · Replies 23 ·
Replies
23
Views
2K
  • · Replies 14 ·
Replies
14
Views
2K
  • · Replies 13 ·
Replies
13
Views
7K
  • · Replies 5 ·
Replies
5
Views
3K