Creating your own printf() in C

  • Thread starter Tony11235
  • Start date
In summary, the student was not able to finish the printhex() function and is not sure if it is even right. The printchar() function prints out the character corresponding to the argument passed to it. The student is new to C and is looking for help with how to create the printhex() function.
  • #1
Tony11235
255
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
  • #2
Last edited:
  • #3
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.
 

1. How do I create my own printf() function in C?

To create your own printf() function in C, you will need to declare a variable argument list using the va_list data type and the va_start, va_arg, and va_end macros. You will also need to use the vsnprintf() function to format the output and print it to the screen using the puts() or printf() function.

2. What are the benefits of creating my own printf() function?

Creating your own printf() function allows you to customize the output and formatting of your program's output. It can also help improve the efficiency and performance of your code by reducing the number of function calls needed for printing. Additionally, it can make your code more readable and maintainable.

3. Can I use my own data types with my custom printf() function?

Yes, you can use your own data types with your custom printf() function. You will need to define the appropriate conversion specifiers and use them in your formatting string. You can also use the va_arg macro to retrieve the argument from the variable argument list based on its data type.

4. Is it difficult to create my own printf() function?

Creating your own printf() function can be challenging, but it is not necessarily difficult. It requires a solid understanding of C programming concepts such as pointers, variable argument lists, and macros. With practice and careful planning, you can successfully create your own printf() function.

5. Are there any potential drawbacks to creating my own printf() function?

One potential drawback of creating your own printf() function is that it may not be as robust or versatile as the standard printf() function provided by the C standard library. Additionally, if your custom printf() function is not well-designed or properly tested, it may introduce bugs or errors into your code. It is important to thoroughly test and debug your custom printf() function before using it in your program.

Similar threads

  • Programming and Computer Science
Replies
4
Views
735
  • Engineering and Comp Sci Homework Help
Replies
3
Views
884
  • Programming and Computer Science
2
Replies
36
Views
2K
  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
3
Replies
89
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Programming and Computer Science
Replies
1
Views
646
Back
Top