A question on argument passing

Discussion in 'C Programming' started by M. Azam, Dec 24, 2008.

  1. M. Azam

    M. Azam Guest

    I thought this should work but it doesn't. When bin is passed argument
    251 it get converted in to -5. If I changed bin to take an int, it works
    fine. I was expecting it to work even with char, since 251 is less than
    the max a char can handle. Why it is not working? Thanks.


    #include<stdio.h>
    void bin(char n) {
    printf("n = %d\n", n);
    if (n!=0) {
    printf("%d",n%2);
    bin(n/2);
    }
    printf("\n");
    }
    main() {
    unsigned char val = 4;
    printf("%d: ", val);
    bin(val);

    val = ~ val;
    printf("%d: ", val);
    bin(val);
    }
     
    M. Azam, Dec 24, 2008
    #1
    1. Advertising

  2. M. Azam

    Eric Sosman Guest

    M. Azam wrote:
    > I thought this should work but it doesn't. When bin is passed argument
    > 251 it get converted in to -5. If I changed bin to take an int, it works
    > fine. I was expecting it to work even with char, since 251 is less than
    > the max a char can handle. Why it is not working? Thanks.


    Probably because "since 251 is less than the max a char
    can handle" is false. Try this:

    #include <stdio.h>
    #include <limits.h>
    int main(void) {
    printf ("%d <= char <= %d\n", CHAR_MIN, CHAR_MAX);
    return 0;
    }

    This program will print different results on different C
    implementations. Two common outcomes are

    0 <= char <= 255
    and
    -128 <= char <= 127

    but others are possible.

    --
    Eric Sosman
    lid
     
    Eric Sosman, Dec 24, 2008
    #2
    1. Advertising

  3. "M. Azam" <> writes:
    > I thought this should work but it doesn't. When bin is passed argument
    > 251 it get converted in to -5. If I changed bin to take an int, it works
    > fine. I was expecting it to work even with char, since 251 is less than
    > the max a char can handle. Why it is not working? Thanks.

    [...]

    The maximum value a char can handle is CHAR_MAX. It's likely that
    CHAR_MAX is 127 on your system. Plain char can be either signed or
    unsigned; apparently on your system it's signed.

    On a typical system with 8-bit bytes, unsigned char has a range of
    0..255, signed char has a range of -128..+127, and plain char has the
    same range as either signed char or unsigned char. (On some rare
    systems, signed char might have a range of -127..+127; on others,
    bytes might be bigger than 8 bits.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Dec 24, 2008
    #3
  4. M. Azam

    M. Azam Guest

    On Wed, 24 Dec 2008 06:51:14 -0800, Keith Thompson wrote:

    > "M. Azam" <> writes:
    >> I thought this should work but it doesn't. When bin is passed argument
    >> 251 it get converted in to -5. If I changed bin to take an int, it
    >> works fine. I was expecting it to work even with char, since 251 is
    >> less than the max a char can handle. Why it is not working? Thanks.

    > [...]
    >
    > The maximum value a char can handle is CHAR_MAX. It's likely that
    > CHAR_MAX is 127 on your system. Plain char can be either signed or
    > unsigned; apparently on your system it's signed.
    >
    > On a typical system with 8-bit bytes, unsigned char has a range of
    > 0..255, signed char has a range of -128..+127, and plain char has the
    > same range as either signed char or unsigned char. (On some rare
    > systems, signed char might have a range of -127..+127; on others, bytes
    > might be bigger than 8 bits.)


    Yes, by default char is signed, when changed bin signature to take
    unsigned char, it worked fine. Thanks for the response.

    --Zam
     
    M. Azam, Dec 25, 2008
    #4
    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.
Similar Threads
  1. Bhushit Joshipura

    defaulting argument to previous argument

    Bhushit Joshipura, Dec 29, 2003, in forum: C++
    Replies:
    5
    Views:
    419
  2. Trans
    Replies:
    3
    Views:
    200
    Robert Klemme
    Jan 26, 2005
  3. ±è ÁØ¿µ
    Replies:
    7
    Views:
    259
    Einar Magnús Boson
    Nov 27, 2008
  4. lalawawa
    Replies:
    9
    Views:
    138
    Marnen Laibow-Koser
    Jan 18, 2010
  5. User1014
    Replies:
    1
    Views:
    192
    Richard Cornford
    Nov 30, 2006
Loading...

Share This Page