Re: '\0' ?

Discussion in 'C Programming' started by Skybuck Flying, Nov 1, 2004.

  1. "Skybuck Flying" <> wrote in message news:...
    > > >
    > > >
    > > > #include <string.h>
    > > > #include <memcopy.h>
    > > >
    > > > #undef strcmp
    > > >
    > > > /* Compare S1 and S2, returning less than, equal to or
    > > > greater than zero if S1 is lexicographically less than,
    > > > equal to or greater than S2. */
    > > > int
    > > > strcmp (p1, p2)
    > > > const char *p1;
    > > > const char *p2;
    > > > {
    > > > register const unsigned char *s1 = (const unsigned char *) p1;
    > > > register const unsigned char *s2 = (const unsigned char *) p2;
    > > > unsigned reg_char c1, c2;
    > > >
    > > > do
    > > > {
    > > > c1 = (unsigned char) *s1++;
    > > > c2 = (unsigned char) *s2++;
    > > > if (c1 == '\0')
    > > > return c1 - c2;

    > >
    > > if stored in s1 is '\0' then c1 would equal 0. c2 may be some
    > > unsigned value, for example, 97.
    > > So you have, for example, (unsigned)0 - (unsigned)97. Is is
    > > possible or not possilbe for this to return a negative value
    > > when converted to type int and returned from the function?

    >
    > Good question.
    >
    > In delphi it returns a negative value which is ok.
    >
    > In visual C/C++ 6.0 it returns a positive value which is probably wrong

    <-
    > ouch.


    God I suck at C lol.. my example was wrong... It does return -97 ;)

    int bla()
    {
    unsigned char a;
    unsigned char b;

    a = 0;
    b = 97;

    return (a-b);

    }

    int main()
    {

    printf("bla: %d \n", bla() );

    if (bla() == -97)

    printf("ok\n");
    }

    Bye,
    Skybuck.
     
    Skybuck Flying, Nov 1, 2004
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page