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?
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?
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.
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 02:37 PM.
If you understand what you're doing, you're not learning anything.
Thanks alot itsyou86
I'll have a look at both the man-example and the code you posted. Thanks!
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...
Depending on the specifics of your homework, you may be getting too complicated. Look into the vfprintf() function.
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?
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.
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
%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.