Page 1 of 1

Problems with printf()

Posted: Mon Oct 21, 2002 7:20 am
by Matze2002
Hi,
at this time i write my own printf() function, here is the source, when i call the function with:

int var1 = 10;
printf("%i", var1);

the functions returns the wrong number, why ?

Here is the function:

int vsprintf(char *buf, const char *fmt, va_list args)
{

   char * str;
   int zeichen;

   for (str=buf ; *fmt ; fmt++)
   {
      switch (*fmt)
      {
      case '%':
      {
      fmt++;
      switch (*fmt)
      {
      case 'i':
      {
      // show me the var   
      zeichen = va_arg(args, int);
      break;
      }
      }
      break;
      }
      default:
    {
// copy the char into the string
      }
      }
   }

}
/* ************************************ */
int sprintf(char *buffer, const char *fmt, ...)
{
int i;
va_list args;

va_start(args, fmt);
i = vsprintf(buffer, fmt, args);
va_end(args);
return i;
}

the function should write 5 into the var
thanks, bye

Re:Problems with printf()

Posted: Mon Oct 21, 2002 7:54 am
by Pype.Clicker
without a return in vsprintf, there is little chance you get a correct result !

Re:Problems with printf()

Posted: Mon Oct 21, 2002 8:04 am
by matze2002
Hi,
no sorry, i have a easy function to write INT on the screen, with this function i write the var "zeichen" after zeichen = va_arg(args, int) on the screen, and the value is wrong. i know, later i write the value into the string, so i can use it for sprintf() and printf().