Hello i need some help for a function that I'm creating, it should perform some of the fuctions of the operand "printf". The problem I have with the funcion, is that I am unable to read the "Float" and "Double" type parameters, also i want to create a function that sorts the number of digits that is shown (%3 , %.5) and so on. If you could help I would be greatfull.
Thanks for your time. Regards, Daniel
Here is the code im using:
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
char * convert(unsigned int, int);
void echo(char *,...);
int *ftoa(float Fnum, char numStr[]);
int main()
{
int i=65;
float k=5.75;
char str[]="This is my string";
echo("\nMessage = %f",k);
getchar();
}
void echo(char * frmt,...)
{
float f;
double d;
char *p;
int i;
char *s;
char fstr[40];
va_list argp;
va_start(argp, frmt);
p=frmt;
for(p=frmt; *p!='\0';p++)
{
if(*p!='%')
{
putchar(*p);continue;
}
p++;
switch(*p)
{
case 'c':
i=va_arg(argp,int);
putchar(i);
break;
case 'd':
i=va_arg(argp,int);
if(i<0)
{
i=-i;putchar('-');
}
puts(convert(i,10));
break;
case 's':
s=va_arg(argp,char *);
puts(s);
break;
case 'f':
f=va_arg(argp,float);
ftoa(f,fstr);
break;
case 'lf':
d=va_arg(argp,double);
puts(ftoa(f,fstr));
case '%':
putchar('%');
break;
}
}
va_end(argp);
}
char *convert(unsigned int num, int base)
{
static char buff[33];
char *ptr;
ptr=&buff[sizeof(buff)];
*ptr='\0';
do
{
*--ptr="0123456789"[num%base];
num/=base;
}
while(num!=0);
return(ptr);
}
int *ftoa(float Fnum, char numStr[])
{
int wholeNum=Fnum;
int decimalNum=((Fnum - wholeNum) * 10000000);
char wholeNumStr[13];
char decNumStr[13];
printf("WholeNum=%d",wholeNum);
itoa(wholeNum, wholeNumStr,10);
itoa(decimalNum, decNumStr,10);
strcpy(numStr, wholeNumStr);
strcat(numStr, ".");
strcat(numStr, decNumStr);
return puts(numStr);
}