while sscanf() and printf() are big libraries, string to integer using
atoi() is likely smaller than you can code up.
--- In ARMexpress@yahoogroups.com, "gordone.nelson"
<gordone.nelson@...> wrote:
>
> Hi, I'm currently trying to code up my own function for converting
> strings into integers. I want to do this in pure C so I can avoid
> adding unnecessary libraries to my project.
>
> Currently the code is working correctly in the Dev-C++ compiler, but
> when I compile it with MakeItC and load it onto the ARMmite, it
> compiles with no errors, but then gets stuck on any line that does a
> divide or mod operation on the "num" input variable.
>
> Any help is appreciated.
>
> Code:
> /*This function will take in an integer of any length
> and turn it into a string.
> int num - the integer to be converted.
> char *s - the pointer to the destination string.
>
> return - the pointer to the destination string.
> */
> char *itoa(int num, char *s)
> {
> char *p;
> char *q;
> char c;
> char digits[]="0123456789";
>
> num=12;
>
> q=s;
> p=s;
>
> if (num >= 0)
> {
> while((num/10)>0)
> {
> *q = digits[num % 10];
> q++;
> num/=10;
> }
> *q = digits[num % 10];
> }
> else
> {
> num*=-1;
> while((num/10)>0)
> {
> *q = digits[num % 10];
> q++;
> num/=10;
> }
> *q = digits[num % 10];
> q++;
> *q='-';
> }
> *(q+1)='\0'; //add null to end of string
> //flip digits around
> while (p < q)
> {
> c = *p;
> *p = *q;
> *q = c;
> q--;
> p++;
> }
> printf("\nconversion complete s=%s", s);
> return s;
> }
>
scanf
-
- Posts: 1462
- Joined: Fri Oct 19, 2012 5:11 am
Re: scanf
I should have said
atoi() and itoa()
atoi() and itoa()