Hi all,
I have to write a function named print, which simulates the library function printf, which has the following header:
void print(const char *fmt, ...);
It should be able to deal with "%d", "%f" and "%s" format specifiers pointed to by fmt as the printf function does. The function print will print other characters directly. The template (in blue) is given and is unchangable.

I tried to use sscanf to read the characters in fmt, but realized everytime I called it, it reads from the first character and hence, gives me an infinite loop. I tried to copy fmt to another string of characters, but got segmentation fault.

I am also not sure of how to handle the %s format specifier. Specifically, I don't know which data type I should use for the second argument in va_arg, as the string is a pointer(?).

My code is below:

Code:
#include <stdarg.h>
#include <stdio.h>
#include <string.h>

void print(const char *fmt, ...);

int main(void)
{
   float x=1.0;
   int i=1;
   char* s="Happy New Year.";
   print("x=%f, i=%d, and %s\n", x, i, s);

   return 0;
}
void print(const char *fmt, ...)
{
   int c, flag = 0, next_int;
   double next_float;
   char next_string;
   va_list arg_addr;
   va_start(arg_addr, fmt);
   while (sscanf(fmt, "%c", &c) != EOF)
   {
   	printf("1\n");
	if (c == '%')
   	       flag += 1;
	if (flag == 1)
   	{
                       if (c == 'd')
                       {
   		next_int = va_arg(arg_addr, int);
      		printf("%d", next_int);
                                flag = 0;
	       }
	       else
   		if (c == 'f')
         	               {
         		      next_float = va_arg(arg_addr, double);
	                      printf("%lf", next_float);
                                      flag = 0;
   	               }
      	               else
         		      if (c == 's')
            	                      {
            		              next_string = va_arg(arg_addr, int);
               	                              printf("%s", next_string);
                                             flag = 0;
	                      }
              }
              else
   	   printf("%c", c);
       }  
}
Please help.

Thank you.

Regards,
Rayne