min/max in stdlib.h?!

C

copx

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?
 
I

Ian Collins

copx said:
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?
 
R

Richard Heathfield

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

jacob navia

copx said:
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.
 
J

jacob navia

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

This will work in lcc and mingw
 
I

Ian Collins

jacob said:
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?
 
J

jacob navia

Ian said:
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
 
R

Richard Heathfield

copx said:
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.
 
F

Flash Gordon

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

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

CBFalconer

copx said:
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.
 
K

Keith Thompson

jacob navia said:
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.)
 
S

SM Ryan

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

Serve Lau

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

JimS

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
 
J

jacob navia

Serve said:
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.
 
A

Army1987

SM said:
# 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))
 
C

CBFalconer

Serve said:
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.
 
C

CBFalconer

Army1987 said:
SM Ryan wrote:
.... snip ...

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top