Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

outputing numbers

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
For school I'm supposed to write my own version of printf.
I have everything down on variable length, but I was wondering how I would be able to print out int/float/doubles
without using printf. The only way I can think of is to put it in a string and print out the string, but I don't even know if thats possible. Thanks for any help in advance.
 
Hi!! Westin
Probably I am not very clear about the question u asked, but the minimum well understood thing is u need a function to convert int to String, try using itoa(), ltoa() and ultoa()...

Hope that helps,
SwapSawe.s-)
 
or you can use sprintf which has the same format and behavior as printf, except the output is in a string... Nosferatu
We are what we eat...
There's no such thing as free meal...
 
You can find an implementation
of this function in K&R C programming language book.

Instead of using printf("%d",...) you
can convert the int to a string and call putchar()
on each of the characters.

p is of type char*;

myprintf(char*fmt, ...)
{
va_list ap;
char *p;
float fval;
int ival;
char s[SZ_MAX];

va_start(ap, fmt);
for ( p =fmt; *p; p++ )
{
if (*p != '%')
{
putchar(*p);
continue;
}
switch(*++p)
{
case 'd':
ival = va_arg(ap, int);
s = itoa(ival);
for (int i=0;i<strlen(s); i++)
putchar(s);

case 'f':
fval = va_arg(ap, float);
s = ftoa(fval);
for (i=0; i<strlen(s); i++)
putchar(s);

...
/* like that for each type */

}



I am not sure if there is a function called ftoa().
sprintf is a good alternative for itoa() or ftoa() etc.

Regards

Anand :cool: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Mail me at abpillai@excite.com if you want additional help. I dont promise that I can solve your problem, but I will try my best.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top