-9

I have a string having 12 characters in it, and want to convert that in int or we can say digit , but not able to do it. i have written a code which converts string up to 11 digits but after that it starts giving garbage value. my code is like

#include<stdio.h>
double myAtoi(char *str);
void main()
{
    double n;
    double m;
    char* a="12345678912";
    char* b="34";
    n=myAtoi(a);
    //m=atoi(b);
    printf("\nconversion is : %d",(n));
}

double myAtoi(char *str)
{   
    printf("\nstr : %s",str);
    long res = 0; // Initialize result
    long i ;
    // Iterate through all characters of input string and update result
    for (i = 0; str[i] != '\0'; ++i)
    {
        res = res*10 + str[i] - '0';
        printf("\n res : %d",res);
        //x=res;
    }
    return res;
}

3 Answers3

3

Bugs:

  • What is x? Where is it declared? Why have you commented out the code copying res into x? Why did you copy it inside the loop instead of at the end of the function? You don't even need x, merely return (double)res; if you insist on using double, for reasons unknown.
  • 12345678912 is 12.3 billion. Usually a long can only fit 2^32 / 2 = 2.14 billion as largest number. Consider using long long.
  • You are printing long and double with %d conversion specifier which is wrong in both cases.
  • You probably don't even want to use double in this program, it doesn't make any sense.

Other things:

  • Since this is a hosted system, main must be declared as int main (void). Reference.
  • Always declare pointers to string literals as const char*. Because you will never write to a string literal, only read from it.
  • The parameter to your function should also be declared as const char*, for const correctness.
Community
  • 1
  • 1
Lundin
  • 195,001
  • 40
  • 254
  • 396
1

Issue is that long cannot hold number as big as "12345678912" instead use unsigned long long .

Its specifier is "%llu".

And also in your code x is not delared instead use return res;

As you use unsigned long long you need to change definition of function double myatoi() to unsigned long long myatoi() -

Your program can be re-written as below -

#include<stdio.h>
unsigned long long myAtoi(char *str);
int main()
{

   unsigned long long  n; // Note here n also changed from double to unsigned long long
   double m;
   char* a="12345678912";
   char* b="34";
   n=myAtoi(a);
   //m=atoi(b);
   printf("\nconversion is : %llu",(n));
}

   unsigned long long myAtoi(char *str)
{   

   printf("\nstr : %s",str);
   unsigned long long res = 0; // Initialize result
   long i ;
   // Iterate through all characters of input string and update result
   for (i = 0; str[i] != '\0'; i++) 
  {
       res = res*10 + str[i] - '0';
       printf("\n res : %llu",res);
       //x=res; 
  }
     return res;
}
ameyCU
  • 16,489
  • 2
  • 26
  • 41
0

I think the issue is you cannot hold "12345678912" to a long variable. please refer http://www.tutorialspoint.com/cprogramming/c_data_types.htm

the maximum value that can hold for a long variable is -2,147,483,648 to 2,147,483,647

and you are returning wrong value from your method.return res instead of x.no need of x in that code.

Joseph
  • 1,054
  • 1
  • 11
  • 25
  • C specifies the _minimum_, range of `long` is -2,147,483,647 to 2,147,483,647. C does not specified its maximum range. `-2,147,483,648` to `2,147,483,647` is common range. – chux - Reinstate Monica Jul 11 '15 at 05:09