/* this is a program in C-Language to implement a printf statement*/
#include
#include
int mprintf(const char *str,...);
int main()
{
     mprintf("Text is %d,%c,%d,%f",10,'a',15,2.3);
     return 0;
}
int mprintf(const char *str,...)
{
     int i=0,p;
     float f;
     char c,ch;
     va_list ap;
     va_start(ap,str);
     while(str[i]!='\0')
     {
         if(isalpha(str[i])
         {
             putchar(str[i]);
             i++;
         }
         else if(str[i]=='%')
         {
         i++;
         switch(str[i])
         {
         case 'd': p=va_arg(ap,int);printf("%d",p);break;
//instead of printf here we can use a user defined function that converts the integers into characters and prints
         case 'f': f=va_arg(ap,double);printf("%d",f);break;
//instead of printf here we can use a user defined function that converts the floating point values into characters and prints. to print characters putchar can be used.
         case 'c': c=va_arg(ap,int);putchar(c);break;
         default: putchar("%");putchar(str[i]);break;
     }
     i++;
     }
     else
     {
         puthcar(str[i]);i++;
     }
     }
     return 0;
// we can employ a count value to count the total no of values printed and can be returned here
}
No comments:
Post a Comment