Page 2 of 2

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

Posted: Sat Mar 01, 2014 3:34 pm
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.

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

Posted: Sat Mar 01, 2014 10:32 pm
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.

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

Posted: Sun Mar 02, 2014 12:05 am
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

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

Posted: Sun Mar 02, 2014 12:41 am
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

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

Posted: Sun Mar 02, 2014 4:49 am
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:

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

Posted: Sun Mar 02, 2014 9:04 am
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.