asadullah.ansari's Avatar, Join Date: Jan 2008
TechCake
Really i am unable to understand what's your requirement!!!
imrantechi's Avatar, Join Date: Feb 2008
Ambitious contributor
Quote:
Originally Posted by asadullah.ansari
You are still using library function getchar() and putchar().

He is asking if i am correct.


Code:
#include "stddef.h"
#include "stdarg.h"
#include "stdlib.h"

extern FILE *Stderr;


int  MyScanf(char *s, const char *fmt, ...)
{
	char *s0;
	va_list ap;
	long L, *Lp;
	int i, *ip, rc = 0;
	va_start(ap, fmt);
	for( ; ; ) {
		for(;;) {
			switch(i = *(unsigned char *)fmt++) {
				case 0:
					goto done;
				case '%':
					break;
				default:
					if (i <= ' ') {
						while(*s <= ' ')
							if (!*s++)
								return rc;
						}
					else if (*s++ != i)
						return rc;
					continue;
				}
			break;
			}
		switch(*fmt++) {
			case 'l':
				if (*fmt != 'd')
					bad(fmt);
				fmt++;
				Lp = va_arg(ap, long*);
				L = strtol(s0 = s, &s, 10);
				if (s > s0) {
					rc++;
					*Lp = L;
					continue;
					}
				return rc;
			case 'd':
				ip = va_arg(ap, int*);
				L = strtol(s0 = s, &s, 10);
				if (s > s0) {
					rc++;
					*ip = (int)L;
					continue;
					}
				return rc;
			default:
				bad(fmt);
			}
		}
 done:
	return rc;
	}
	}

i feel this code is really self explanatary ,

you can explain well
gyanu's Avatar, Join Date: Jan 2008
Light Poster
//please try to sort out the errors
//I ve tried to make the function but has too much errors........
//pls do it ....
Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
void scanchar(char**);
int scans(char**,char*,int,int);
int scani(char**,int,int,int,int,int,int);
int myscanf(char *);
int smyscanf(char*,char*);
clrscr();
printf("enter");
myscanf("%d",&a);
printf("%d",a);
getch();
}
static void scanchar(char **str)
{
 extern int getchar();
 if (str) {
  **str = getchar();
  ++(*str);
 }
 else (void)getchar();
}
#define PAD_RIGHT 1
#define PAD_ZERO 2
static int prints(char **in, const char *string, int width, int pad)
{
 register int sc = 0, padchar = ' ';
 if (width > 0) {
  register int len = 0;
  register const char *ptr;
  for (ptr = string; *ptr; ++ptr) ++len;
  if (len >= width) width = 0;
  else width -= len;
  if (pad & PAD_ZERO) padchar = '0';
 }
 if (!(pad & PAD_RIGHT)) {
  for ( ; width > 0; --width) {
   scanchar (in, padchar);
   ++sc;
  }
 }
 for ( ; *string ; ++string) {
  scanchar (in, *string);
  ++sc;
 }
 for ( ; width > 0; --width) {
  scanchar (in, padchar);
  ++sc;
 }
 return sc;
}
/* the following should be enough for 32 bit int */
#define SCAN_BUF_LEN 12
static int scani(char **in, int i, int b, int sg, int width, int pad, int letbase)
{
 char scan_buf[SCAN_BUF_LEN];
 register char *s;
 register int t, neg = 0, sc = 0;
 register unsigned int u = i;
 if (i == 0) {
  scan_buf[0] = '0';
  scan_buf[1] = '\0';
  return scans (out, print_buf, width, pad);
 }
 if (sg && b == 10 && i < 0) {
  neg = 1;
  u = -i;
 }
 s = scan_buf + SCAN_BUF_LEN-1;
 *s = '\0';
 while (u) {
  t = u % b;
  if( t >= 10 )
   t += letbase - '0' - 10;
  *--s = t + '0';
  u /= b;
 }
 if (neg) {
  if( width && (pad & PAD_ZERO) ) {
   scanchar (in,'-');
   ++sc;
   --width;
  }
  else {
   *--s = '-';
  }
 }
 return sc + scans (in, s, width, pad);
}
static int scan(char **in, int *varg)
{
 register int width, pad;
 register int sc = 0;
 register char *format = (char *)(*varg++);
 char scr[2];
 for (; *format != 0; ++format) {
  if (*format == '%') {
   ++format;
   width = pad = 0;
   if (*format == '\0') break;
   if (*format == '%') goto in;
   if (*format == '-') {
    ++format;
    pad = PAD_RIGHT;
   }
   while (*format == '0') {
    ++format;
    pad |= PAD_ZERO;
   }
   for ( ; *format >= '0' && *format <= '9'; ++format) {
    width *= 10;
    width += *format - '0';
   }
   if( *format == 's' ) {
    register char *s = *((char **)varg++);
    sc += scans (in, s?s:"(null)", width, pad);
    continue;
   }
   if( *format == 'd' ) {
    sc += scani (in, *varg++, 10, 1, width, pad, 'a');
    continue;
   }
   if( *format == 'x' ) {
    sc += scani (in, *varg++, 16, 0, width, pad, 'a');
    continue;
   }
   if( *format == 'X' ) {
    sc += scani (in, *varg++, 16, 0, width, pad, 'A');
    continue;
   }
   if( *format == 'u' ) {
    sc += scani (in, *varg++, 10, 0, width, pad, 'a');
    continue;
   }
   if( *format == 'c' ) {
    /* char are converted to int then pushed on the stack */
    scr[0] = *varg++;
    scr[1] = '\0';
    sc += scans (in, scr, width, pad);
    continue;
   }
  }
  else {
  in:
   scanchar (in, *format);
   ++sc;
  }
 }
 if (in) **in = '\0';
 return sc;
}
/* assuming sizeof(void *) == sizeof(int) */
int myscanf(const char *format, ...)
{
 register int *varg = (int *)(&format);
 return scan(0, varg);
}
int smyscanf(char *out, const char *format, ...)
{
 register int *varg = (int *)(&format);
 return scan(&out, varg);
}

Last edited by shabbir; 13Feb2008 at 21:54.. Reason: Code block - http://www.go4expert.com/forums/misc.php?do=bbcode#code