Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > min/max in stdlib.h?!

Reply
Thread Tools

min/max in stdlib.h?!

 
 
CBFalconer
Guest
Posts: n/a
 
      01-05-2008
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

 
Reply With Quote
 
 
 
 
Tomás Ó hÉilidhe
Guest
Posts: n/a
 
      01-05-2008
"copx" <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      01-05-2008
jacob navia <(E-Mail Removed)> 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) <(E-Mail Removed)>
[...]
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
SM Ryan
Guest
Posts: n/a
 
      01-06-2008
"copx" <(E-Mail Removed)> 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.
 
Reply With Quote
 
Serve Lau
Guest
Posts: n/a
 
      01-06-2008

"Keith Thompson" <(E-Mail Removed)> schreef in bericht
news:(E-Mail Removed)...

> 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.

 
Reply With Quote
 
JimS
Guest
Posts: n/a
 
      01-06-2008
On Sun, 6 Jan 2008 10:32:29 +0100, "Serve Lau" <(E-Mail Removed)> wrote:

>
>"Keith Thompson" <(E-Mail Removed)> schreef in bericht
>news:(E-Mail Removed)...
>
>> 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
 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      01-06-2008
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
 
Reply With Quote
 
Army1987
Guest
Posts: n/a
 
      01-06-2008
SM Ryan wrote:

> "copx" <(E-Mail Removed)> 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")
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      01-06-2008
Serve Lau wrote:
> "Keith Thompson" <(E-Mail Removed)> 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

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      01-06-2008
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments