Problems with printf()

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Matze2002

Problems with printf()

Post 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
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Problems with printf()

Post by Pype.Clicker »

without a return in vsprintf, there is little chance you get a correct result !
matze2002

Re:Problems with printf()

Post 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().
Post Reply