atol(), atof(), atoi() function behaviours, is there a stable way to convert from/to string/integer
- by Berkay
In these days i'm playing with the C functions of atol(), atof() and atoi(), from a blog post i find a tutorial and applied:
here are my results:
void main()
char a[10],b[10];
puts("Enter the value of a");
gets(a);
puts("Enter the value of b");
gets(b);
printf("%s+%s=%ld and %s-%s=%ld",a,b,(atol(a)+atol(b)),a,b,(atol(a)-atol(b)));
getch();
}
there is atof() which returns the float value of the string and atoi() which returns integer value.
now to see the difference between the 3 i checked this code:
main()
{
char a[]={"2545.965"};
printf("atol=%ld\t atof=%f\t atoi=%d\t\n",atol(a),atof(a),atoi(a));
}
the output will be
atol=2545 atof=2545.965000 atoi=2545
char a[]={“heyyou”};
now when you run the program the following will be the output (why?, is there any solution to convert pure strings to integer?)
atol=0 atof=0 atoi=0
the string should contain numeric value now modify this program as
char a[]={“007hey”};
the output in this case(tested in Red hat) will be
atol=7 atof=7.000000 atoi=7
so the functions has taken 007 only not the remaining part (why?)
Now consider this
char a[]={“hey007?};
the output of the program will be
atol=0 atof=0.000000 atoi=0
So i just want to convert my strings to number and then again to same text, i played with these functions and as you see i'm getting really interesting results?
why is that?
any other functions to convert from/to string/integer and vice versa?