Tuesday, 9 December 2014

C program for the Implementation of atoi function

Implementation of atoi function
#include<stdio.h>
int myatoi(const char *string);
int main(int argc, char* argv[])
{
int i;
for(i=1;i<argc;i++)
  printf("\n%d\n", myatoi(argv[i]));
//  getch();
 // return(0);
}
int myatoi(const char *string)
{
    int i;
    i=0;
    while(*string)
    {
         i=(i<<3) + (i<<1) + (*string - '0');
         string++;
         // Dont increment i!
    }
    return(i);
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ ./a.out 1234

1234