char* to int, has to be an easier way

Programming, for all ages and all languages.
User avatar
sortie
Member
Member
Posts: 931
Joined: Wed Mar 21, 2012 3:01 pm
Libera.chat IRC: sortie

Re: char* to int, has to be an easier way

Post by sortie »

BASICFreak wrote:C is not my native language, learning so much within 1500 lines. I'm starting to miss the OO languages though - or at the very least standard libraries.
That's the best thing about osdev: You get to do everything from scratch. You'll end up deeply understanding a full complex system in full details.
BASICFreak wrote:And I'll continue to make my code C89 standard so I don't have to worry about anyone (mostly me) missing a -C99 gcc tag.
Just learn to use your compiler. Passing -std=c99 (or -std=gnu99 if you want extensions such as asm rather than __asm__) is easy and just takes a few moments to remember. Besides, you put that in build scripts anyways - other people building your code would be using these scripts too. I'd argue it is definitely worth the trade-off learning how to invoke your compiler compared with the advantages of not dealing with artificial ancient C restrictions. Note that it is called an option, not a tag.
User avatar
BASICFreak
Member
Member
Posts: 284
Joined: Fri Jan 16, 2009 8:34 pm
Location: Louisiana, USA

Re: char* to int, has to be an easier way

Post by BASICFreak »

sortie wrote:
BASICFreak wrote:C is not my native language, learning so much within 1500 lines. I'm starting to miss the OO languages though - or at the very least standard libraries.
That's the best thing about osdev: You get to do everything from scratch. You'll end up deeply understanding a full complex system in full details.
That's why I love it so much ;) Learning far more than I thought I would.
BOS Source Thanks to GitHub
BOS Expanded Commentary
Both under active development!
Sortie wrote:
  • Don't play the role of an operating systems developer, be one.
  • Be truly afraid of undefined [behavior].
  • Your operating system should be itself, not fight what it is.
User avatar
BASICFreak
Member
Member
Posts: 284
Joined: Fri Jan 16, 2009 8:34 pm
Location: Louisiana, USA

Re: char* to int, has to be an easier way

Post by BASICFreak »

sortie wrote:
BASICFreak wrote:And I'll continue to make my code C89 standard so I don't have to worry about anyone (mostly me) missing a -C99 gcc tag.
Just learn to use your compiler. Passing -std=c99 (or -std=gnu99 if you want extensions such as asm rather than __asm__) is easy and just takes a few moments to remember. Besides, you put that in build scripts anyways - other people building your code would be using these scripts too. I'd argue it is definitely worth the trade-off learning how to invoke your compiler compared with the advantages of not dealing with artificial ancient C restrictions. Note that it is called an option, not a tag.
And you know what sold me on C99, I had to get this all on one line to make it match everything else:

Code: Select all

if (streql(explode_cmd[0], "echo")) for (int i = 1; i <= args; i++) printf ("%s ", explode_cmd[i]);
I did have to go through my code and replace all (3) asm calls with __asm__ __volatile__


OFF TOPIC
-----
And still my favorite gcc warning:

Code: Select all

CMD.c: In function 'doCommand':
CMD.c:50: warning: division by zero
CMD.c:50: warning: unused variable 'divz'
type div0 at prompt and what do you know 1/0 does not compute, still useful for faulting the system and testing my error source
BOS Source Thanks to GitHub
BOS Expanded Commentary
Both under active development!
Sortie wrote:
  • Don't play the role of an operating systems developer, be one.
  • Be truly afraid of undefined [behavior].
  • Your operating system should be itself, not fight what it is.
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: char* to int, has to be an easier way

Post by Brendan »

Hi,
bwat wrote:
BASICFreak wrote:Tested and works fine, and returns -1 on error

I could not think of how to process in order thanks for that, makes more sense than what I was doing.
You've written a function with return type unsigned int that returns -1 on error. Interesting....... :)
Correction: a function with return type unsigned int that returns -1 on one error while ignoring other errors (e.g. overflows). ;)

Code: Select all

int stringToInt(unsigned int *out, char* in) {
    unsigned int result = 0;

    while (*in != 0) {
        if ( (*in < '0') || (*in > '9') ) {
            return BAD_CHAR;
        }
        if(result > UINT_MAX/10 ) {
            return OVERFLOW;
        }
        result *= 10;
        if(result > UINT_MAX - (*in - '0') ) {
            return OVERFLOW;
        }
        ret += *in - '0';
        *in++;
    }
    *out = result;
    return OK;
}

Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: char* to int, has to be an easier way

Post by Combuster »

sortie wrote:
BASICFreak wrote:And I'll continue to make my code C89 standard so I don't have to worry about anyone (mostly me) missing a -C99 gcc tag.
Just learn to use your compiler. Passing -std=c99 (or -std=gnu99 if you want extensions such as asm rather than __asm__) is easy and just takes a few moments to remember. Besides, you put that in build scripts anyways - other people building your code would be using these scripts too. I'd argue it is definitely worth the trade-off learning how to invoke your compiler compared with the advantages of not dealing with artificial ancient C restrictions. Note that it is called an option, not a tag.
Which makes me think you're not passing -Wall -Wextra (-Werror -pedantic) either even though that really teaches you to write proper C - more so than -std=c99 :wink:
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
sortie
Member
Member
Posts: 931
Joined: Wed Mar 21, 2012 3:01 pm
Libera.chat IRC: sortie

Re: char* to int, has to be an easier way

Post by sortie »

BASICFreak wrote: And you know what sold me on C99, I had to get this all on one line to make it match everything else:

Code: Select all

if (streql(explode_cmd[0], "echo")) for (int i = 1; i <= args; i++) printf ("%s ", explode_cmd[i]);
Please realize you are using a horrible indention style! Do not write such long convoluted lines for the sake of it, I don't know what gave you the idea that is a good style. Please adapt this indention style instead:

Code: Select all

if (streql(explode_cmd[0], "echo"))
    for (int i = 1; i <= args; i++)
        printf ("%s ", explode_cmd[i]);
(Or any other sane indention style)
BASICFreak wrote: I did have to go through my code and replace all (3) asm calls with __asm__ __volatile__
Did you not read my above post? The default option is -std=gnu89, which gives you C89 and GNU extensions such as asm volatile. You changed that to -std=c99 and got C99 but no GNU extensions. You'll want to use -std=gnu99 and get C99 and GNU extensions. Hell, use -std=gnu11!
BASICFreak wrote: And still my favorite gcc warning:

Code: Select all

CMD.c: In function 'doCommand':
CMD.c:50: warning: division by zero
CMD.c:50: warning: unused variable 'divz'
type div0 at prompt and what do you know 1/0 does not compute, still useful for faulting the system and testing my error source
Do you know what undefined behaviour is in C? If you are not entirely sure or have to guess, please do yourself an important favour and research the topic carefully. Zero division is undefined behaviour.
Post Reply