min/max in stdlib.h?!

Discussion in 'C Programming' started by copx, Jan 5, 2008.

  1. copx

    copx Guest

    Are the macros min() and max() part of stdlib.h or not? (according to the
    standard?)

    I have the following problem: I defined my own min() / max() macros because
    my compiler (MinGW) does NOT define them. When I tried to compile my program
    with lcc-win32 the compiler complained about macro redefinition, because
    min() and max() are defined in lcc's stdlib.h..

    So do these macros belong there or not?
     
    copx, Jan 5, 2008
    #1
    1. Advertising

  2. copx

    Ian Collins Guest

    copx wrote:
    > Are the macros min() and max() part of stdlib.h or not? (according to the
    > standard?)
    >
    > I have the following problem: I defined my own min() / max() macros because
    > my compiler (MinGW) does NOT define them. When I tried to compile my program
    > with lcc-win32 the compiler complained about macro redefinition, because
    > min() and max() are defined in lcc's stdlib.h..
    >
    > So do these macros belong there or not?
    >

    No.

    Did you invoke the compiler in conforming mode?

    --
    Ian Collins.
     
    Ian Collins, Jan 5, 2008
    #2
    1. Advertising

  3. copx said:

    > Are the macros min() and max() part of stdlib.h or not? (according to the
    > standard?)


    No.

    > I have the following problem: I defined my own min() / max() macros
    > because my compiler (MinGW) does NOT define them. When I tried to compile
    > my program with lcc-win32 the compiler complained about macro
    > redefinition, because min() and max() are defined in lcc's stdlib.h..
    >
    > So do these macros belong there or not?


    No, they don't. If they are nevertheless placed there by the
    implementation, check that you are invoking the implementation in
    conforming mode. If so, then you have uncovered a bug in the
    implementation.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Jan 5, 2008
    #3
  4. copx

    jacob navia Guest

    copx wrote:
    > Are the macros min() and max() part of stdlib.h or not? (according to the
    > standard?)
    >
    > I have the following problem: I defined my own min() / max() macros because
    > my compiler (MinGW) does NOT define them. When I tried to compile my program
    > with lcc-win32 the compiler complained about macro redefinition, because
    > min() and max() are defined in lcc's stdlib.h..
    >
    > So do these macros belong there or not?
    >
    >
    >


    They are not in the standard.
    Note that the definition in stdlib.h is:

    #ifndef max
    #define max(a,b) (((a) > (b)) ? (a) : (b))
    #define min(a,b) (((a) < (b)) ? (a) : (b))
    #endif

    If you define those macros before including stdlib.h
    yours will be taken. Besides, the compiler just
    emits a warning.

    --
    jacob navia
    jacob at jacob point remcomp point fr
    logiciels/informatique
    http://www.cs.virginia.edu/~lcc-win32
     
    jacob navia, Jan 5, 2008
    #4
  5. copx

    jacob navia Guest

    P.S.
    Please do the same:
    #ifndef max
    #define ... // your definition
    #endif

    This will work in lcc and mingw

    --
    jacob navia
    jacob at jacob point remcomp point fr
    logiciels/informatique
    http://www.cs.virginia.edu/~lcc-win32
     
    jacob navia, Jan 5, 2008
    #5
  6. copx

    Ian Collins Guest

    jacob navia wrote:
    > copx wrote:
    >> Are the macros min() and max() part of stdlib.h or not? (according to
    >> the standard?)
    >>
    >> I have the following problem: I defined my own min() / max() macros
    >> because my compiler (MinGW) does NOT define them. When I tried to
    >> compile my program with lcc-win32 the compiler complained about macro
    >> redefinition, because min() and max() are defined in lcc's stdlib.h..
    >>
    >> So do these macros belong there or not?
    >>
    >>
    >>

    >
    > They are not in the standard.
    > Note that the definition in stdlib.h is:
    >
    > #ifndef max
    > #define max(a,b) (((a) > (b)) ? (a) : (b))
    > #define min(a,b) (((a) < (b)) ? (a) : (b))
    > #endif
    >
    > If you define those macros before including stdlib.h
    > yours will be taken. Besides, the compiler just
    > emits a warning.
    >

    Do they go away in conforming mode? If not, they should.

    You can't expect punters to mess about with the order of macro
    definitions and header inclusions. Or do you expect them to ignore
    warnings?

    --
    Ian Collins.
     
    Ian Collins, Jan 5, 2008
    #6
  7. copx

    jacob navia Guest

    Ian Collins wrote:
    > jacob navia wrote:
    >> copx wrote:
    >>> Are the macros min() and max() part of stdlib.h or not? (according to
    >>> the standard?)
    >>>
    >>> I have the following problem: I defined my own min() / max() macros
    >>> because my compiler (MinGW) does NOT define them. When I tried to
    >>> compile my program with lcc-win32 the compiler complained about macro
    >>> redefinition, because min() and max() are defined in lcc's stdlib.h..
    >>>
    >>> So do these macros belong there or not?
    >>>
    >>>
    >>>

    >> They are not in the standard.
    >> Note that the definition in stdlib.h is:
    >>
    >> #ifndef max
    >> #define max(a,b) (((a) > (b)) ? (a) : (b))
    >> #define min(a,b) (((a) < (b)) ? (a) : (b))
    >> #endif
    >>
    >> If you define those macros before including stdlib.h
    >> yours will be taken. Besides, the compiler just
    >> emits a warning.
    >>

    > Do they go away in conforming mode? If not, they should.
    >
    > You can't expect punters to mess about with the order of macro
    > definitions and header inclusions. Or do you expect them to ignore
    > warnings?
    >


    They go away in conforming mode *now* ...

    :)

    Will be in the next release

    --
    jacob navia
    jacob at jacob point remcomp point fr
    logiciels/informatique
    http://www.cs.virginia.edu/~lcc-win32
     
    jacob navia, Jan 5, 2008
    #7
  8. copx

    copx Guest

    "Ian Collins" <> schrieb im Newsbeitrag
    news:...
    > copx wrote:
    >> Are the macros min() and max() part of stdlib.h or not? (according to the
    >> standard?)
    >>
    >> I have the following problem: I defined my own min() / max() macros
    >> because
    >> my compiler (MinGW) does NOT define them. When I tried to compile my
    >> program
    >> with lcc-win32 the compiler complained about macro redefinition, because
    >> min() and max() are defined in lcc's stdlib.h..
    >>
    >> So do these macros belong there or not?
    >>

    > No.


    Ok, thanks.

    > Did you invoke the compiler in conforming mode?


    Yes, it did.
     
    copx, Jan 5, 2008
    #8
  9. copx said:

    > "Ian Collins" <> schrieb im Newsbeitrag
    > news:...
    >
    >> Did you invoke the compiler in conforming mode?

    >
    > Yes, it did.


    If you invoked the implementation in its conforming mode and it defined min
    and max in stdlib.h, it doesn't really *have* a conforming mode.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Jan 5, 2008
    #9
  10. copx

    Flash Gordon Guest

    jacob navia wrote, On 05/01/08 10:30:

    <snip min/max being defined in stdlib.h even in conforming mode>

    > They go away in conforming mode *now* ...
    >
    > :)
    >
    > Will be in the next release


    Perhaps rather that fixing these things one at a time when people
    complain you should review all of your standard headers and make sure
    than in conforming mode they define what the standard requires and
    nothing more (at least, nothing more that is not in your namespace as
    implementer, you can still define __ya_bo_sucks_to_you if you want).
    --
    Flash Gordon
     
    Flash Gordon, Jan 5, 2008
    #10
  11. copx

    CBFalconer Guest

    copx wrote:
    >
    > Are the macros min() and max() part of stdlib.h or not? (according
    > to the standard?)
    >
    > I have the following problem: I defined my own min() / max()
    > macros because my compiler (MinGW) does NOT define them. When I
    > tried to compile my program with lcc-win32 the compiler
    > complained about macro redefinition, because min() and max() are
    > defined in lcc's stdlib.h..
    >
    > So do these macros belong there or not?


    They do not, and illustrate another failure of lcc-win32 to meet
    the C standard. Quoting from N869:

    7.20 General utilities <stdlib.h>

    [#1] The header <stdlib.h> declares five types and several
    functions of general utility, and defines several
    macros.234)

    ...

    [#3] The macros defined are NULL (described in 7.17);

    EXIT_FAILURE
    and
    EXIT_SUCCESS

    which expand to integer constant expressions that may be |
    used as the argument to the exit function to return
    unsuccessful or successful termination status, respectively,
    to the host environment;

    RAND_MAX

    which expands to an integer constant expression, the value
    of which is the maximum value returned by the rand function;
    and
    MB_CUR_MAX

    which expands to a positive integer expression with type |
    size_t whose value is the maximum number of bytes in a
    multibyte character for the extended character set specified
    by the current locale (category LC_CTYPE), and whose value
    is never greater than MB_LEN_MAX.

    --
    Chuck F (cbfalconer at maineline dot net)
    <http://cbfalconer.home.att.net>
    Try the download section.



    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Jan 5, 2008
    #11
  12. "copx" <> wrote in comp.lang.c:

    > Are the macros min() and max() part of stdlib.h or not? (according to
    > the standard?)



    If ever in doubt about whether something's Standard C, type it into the
    search box on this page:

    http://www.dinkumware.com/manuals/default.aspx

    It also contains a C++ reference, so be sure also to check that it's
    definitely supported in C, and not just in C++.

    --
    Tomás Ó hÉilidhe
     
    Tomás Ó hÉilidhe, Jan 5, 2008
    #12
  13. jacob navia <> writes:
    > copx wrote:
    >> Are the macros min() and max() part of stdlib.h or not? (according
    >> to the standard?)
    >>
    >> I have the following problem: I defined my own min() / max() macros
    >> because my compiler (MinGW) does NOT define them. When I tried to
    >> compile my program with lcc-win32 the compiler complained about
    >> macro redefinition, because min() and max() are defined in lcc's
    >> stdlib.h..
    >>
    >> So do these macros belong there or not?

    >
    > They are not in the standard.
    > Note that the definition in stdlib.h is:
    >
    > #ifndef max
    > #define max(a,b) (((a) > (b)) ? (a) : (b))
    > #define min(a,b) (((a) < (b)) ? (a) : (b))
    > #endif
    >
    > If you define those macros before including stdlib.h
    > yours will be taken. Besides, the compiler just
    > emits a warning.


    I saw your later response in which you said that this is corrected in
    the next release.

    If the warning were the only effect, then this would not be a
    conformance issue (though it would be a QoI issue). But consider the
    following strictly conforming program:

    #include <stdlib.h>
    int max(int a, int b)
    {
    return a > b ? a : b;
    }
    int main(void)
    {
    int n = max(0, 1);
    return 0;
    }

    I don't have lcc-win, but my compiler (when I replace the "#include
    <stdlib.h>" with your definition of max) chokes on the function
    declaration; I presume lcc-win does as well.

    If you wanted to provide min and max macros, I would *strongly*
    recommend (a) calling them MIN and MAX, to emphasize that they're
    macros and may evaluate their arguments more than once, and (b) define
    them in an implementation-specific header, not in a language-defined
    header.

    There's probably existing code that depends on the current behavior --
    but such code is already not portable to other implementations,
    possibly without the author's knowledge.

    In my opinion, an implementation should *never* define extra stuff in
    the standard headers, even conditionally. (Yes, POSIX does this; I'm
    not pleased about that either.)

    --
    Keith Thompson (The_Other_Keith) <>
    [...]
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jan 5, 2008
    #13
  14. copx

    SM Ryan Guest

    "copx" <> wrote:
    # Are the macros min() and max() part of stdlib.h or not? (according to the
    # standard?)
    #
    # I have the following problem: I defined my own min() / max() macros because
    # my compiler (MinGW) does NOT define them. When I tried to compile my program
    # with lcc-win32 the compiler complained about macro redefinition, because
    # min() and max() are defined in lcc's stdlib.h..
    #
    # So do these macros belong there or not?

    If the header is using #define, you can check with #if(n)def
    and rid a previous definition without warning with #undef.
    It's likely easier to adapt than try to change the library.

    --
    SM Ryan http://www.rawbw.com/~wyrmwif/
    She broke your heart and inadvertently drove men to deviant lifestyles.
     
    SM Ryan, Jan 6, 2008
    #14
  15. copx

    Serve Lau Guest

    "Keith Thompson" <> schreef in bericht
    news:...

    > In my opinion, an implementation should *never* define extra stuff in
    > the standard headers, even conditionally. (Yes, POSIX does this; I'm
    > not pleased about that either.)


    I'm pretty sure that visual studio defined min and max in stdlib in previous
    versions and that is probably why lccwin32 did this too. But I check
    msvc2005 now and they apparently changed it to __min and __max. This would
    be fine imo.
     
    Serve Lau, Jan 6, 2008
    #15
  16. copx

    JimS Guest

    On Sun, 6 Jan 2008 10:32:29 +0100, "Serve Lau" <> wrote:

    >
    >"Keith Thompson" <> schreef in bericht
    >news:...
    >
    >> In my opinion, an implementation should *never* define extra stuff in
    >> the standard headers, even conditionally. (Yes, POSIX does this; I'm
    >> not pleased about that either.)

    >
    >I'm pretty sure that visual studio defined min and max in stdlib in previous
    >versions and that is probably why lccwin32 did this too. But I check
    >msvc2005 now and they apparently changed it to __min and __max. This would
    >be fine imo.


    It did used to define them, but Microsoft've fixed all that stuff
    since about 2001. Plenty of other cranky stuff in there now though ;)

    Jim
     
    JimS, Jan 6, 2008
    #16
  17. copx

    jacob navia Guest

    Serve Lau wrote:
    >
    > I'm pretty sure that visual studio defined min and max in stdlib in
    > previous versions and that is probably why lccwin32 did this too.


    Yes. Since then, Microsoft got more serious about standards.
    I did not follow closely what they do now, until you mention it,
    and I checked that they changed that, as you say.

    --
    jacob navia
    jacob at jacob point remcomp point fr
    logiciels/informatique
    http://www.cs.virginia.edu/~lcc-win32
     
    jacob navia, Jan 6, 2008
    #17
  18. copx

    Army1987 Guest

    SM Ryan wrote:

    > "copx" <> wrote:
    > # Are the macros min() and max() part of stdlib.h or not? (according to the
    > # standard?)

    [snip]
    > If the header is using #define, you can check with #if(n)def
    > and rid a previous definition without warning with #undef.
    > It's likely easier to adapt than try to change the library.

    #include <stdio.h>
    int max(int a, int b)
    {
    switch ( (a > b) - (a < b) ) {
    case -1: return puts("b is greater");
    case 0: return puts("They're equal");
    case 1: return puts("a is greater");
    }
    }
    #include <stdlib.h>
    int main(void)
    {
    if ( max(-47, -'/') < 0 )
    return EXIT_FAILURE;
    else
    return EXIT_SUCCESS;
    }
    doesn't work if stdlib.h contains
    #ifdef max
    #undef max
    #endif
    #define max(a, b) (((a) > (b)) ? (a) : (b))

    --
    Army1987 (Replace "NOSPAM" with "email")
     
    Army1987, Jan 6, 2008
    #18
  19. copx

    CBFalconer Guest

    Serve Lau wrote:
    > "Keith Thompson" <> schreef:
    >
    >> In my opinion, an implementation should *never* define extra
    >> stuff in the standard headers, even conditionally. (Yes, POSIX
    >> does this; I'm not pleased about that either.)

    >
    > I'm pretty sure that visual studio defined min and max in stdlib
    > in previous versions and that is probably why lccwin32 did this
    > too. But I check msvc2005 now and they apparently changed it to
    > __min and __max. This would be fine imo.


    What Navia and/or Microsoft do has nothing whatsoever to do with
    it. Simply read the C standard, or something reasonably close,
    such as N869 or N1276.

    --
    Chuck F (cbfalconer at maineline dot net)
    <http://cbfalconer.home.att.net>
    Try the download section.



    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Jan 6, 2008
    #19
  20. copx

    CBFalconer Guest

    Army1987 wrote:
    > SM Ryan wrote:
    >

    .... snip ...
    >> It's likely easier to adapt than try to change the library.

    >
    > #include <stdio.h>
    > int max(int a, int b) {
    > switch ( (a > b) - (a < b) ) {
    > case -1: return puts("b is greater");
    > case 0: return puts("They're equal");
    > case 1: return puts("a is greater");
    > }
    > }

    .... snip ...
    >
    > doesn't work if stdlib.h contains

    anything at all.

    Just plain 'doesn't work'. Lookup the return value of puts.

    --
    Chuck F (cbfalconer at maineline dot net)
    <http://cbfalconer.home.att.net>
    Try the download section.



    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Jan 6, 2008
    #20
    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. Patrick

    Max Min

    Patrick, Jul 29, 2004, in forum: VHDL
    Replies:
    1
    Views:
    3,057
    Jonathan Bromley
    Jul 29, 2004
  2. Lois
    Replies:
    1
    Views:
    3,411
    Ryan Stewart
    Dec 27, 2004
  3. juergen
    Replies:
    3
    Views:
    584
    opalinski from opalpaweb
    Sep 20, 2006
  4. Albert Hopkins

    When is min(a, b) != min(b, a)?

    Albert Hopkins, Jan 21, 2008, in forum: Python
    Replies:
    31
    Views:
    863
    Albert van der Horst
    Feb 4, 2008
  5. carmen

    Converting hrs and min to just min

    carmen, Aug 10, 2004, in forum: ASP General
    Replies:
    4
    Views:
    154
    carmen
    Aug 10, 2004
Loading...

Share This Page