strchr declaration

Discussion in 'C Programming' started by ts, Mar 14, 2007.

  1. ts

    ts Guest

    Hello all,

    Can somebody explain why strchr is declared the way it is? Here is
    the declaration:

    char *strchr(const char *s, int c);

    Mainly I do not understand why the second parameter has the 'int'
    type. From my knowledge it is not even portable. On a machine where
    'char' has the same size as an 'int' and where 'char' has the same
    behaviour as an 'unsigned char' the following piece of code could not
    work as expected:

    {
    char c;
    char *p;

    /* some statements here initializing c */

    p = strchr(my_string, c);
    }

    I am not a portability expert, so probably I am wrong. But having the
    second parameter of 'char' type is less confusing and does not change
    the behaviour at all.

    thx
    cristi
     
    ts, Mar 14, 2007
    #1
    1. Advertising

  2. ts

    Chris Dollin Guest

    ts wrote:

    > Can somebody explain why strchr is declared the way it is? Here is
    > the declaration:
    >
    > char *strchr(const char *s, int c);
    >
    > Mainly I do not understand why the second parameter has the 'int'
    > type.


    Historical reasons, I believe. Remember that `strchr` existed before
    function prototypes did, and that a character literal such as 'X'
    has type /int/ not type /char/. Suppose (as would be the case for
    a /lot/ of code move from pre-Standard to post_Standard) that
    `strchr` is declared without a prototype. Then the default argument
    promotions apply, so `char`s get promoted to `int`s, so if the
    /definition/ of `strchr` has second argument `char` the effects are
    undefined. This is a Bad Thing.

    > From my knowledge it is not even portable. On a machine where
    > 'char' has the same size as an 'int'


    .... various problems arise in any case, if I recall correctly. An
    easy fix is to make such an implementation freestanding, in which
    case they don't have to provide the C standard library [1]!

    > I am not a portability expert, so probably I am wrong. But having the
    > second parameter of 'char' type is less confusing and does not change
    > the behaviour at all.


    It would be, but for history; and it does, even if only by a little.

    [1] Perhaps this is "fixed" as in "You're well and truly fixed.".

    --
    Chris "electric hedgehog" Dollin
    "What I don't understand is this ..." Trevor Chaplin, /The Beiderbeck Affair/
     
    Chris Dollin, Mar 14, 2007
    #2
    1. Advertising

  3. Florian Weingarten, Mar 14, 2007
    #3
    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. Replies:
    32
    Views:
    7,086
    Default User
    Aug 22, 2003
  2. Martin Dickopp

    Re: Question about strchr() / strrchr()

    Martin Dickopp, Aug 16, 2003, in forum: C Programming
    Replies:
    4
    Views:
    2,255
    Irrwahn Grausewitz
    Aug 16, 2003
  3. Servé Lau

    a shorter strchr

    Servé Lau, Feb 17, 2004, in forum: C Programming
    Replies:
    9
    Views:
    676
  4. David Warner

    strchr & finding multiple occurances of a char

    David Warner, Apr 25, 2005, in forum: C Programming
    Replies:
    4
    Views:
    1,361
    CBFalconer
    Apr 26, 2005
  5. earthling

    strchr

    earthling, Nov 15, 2008, in forum: C Programming
    Replies:
    2
    Views:
    446
    earthling
    Nov 16, 2008
Loading...

Share This Page