Where is max/min ?

M

Martin Ambuhl

Steven Woody wrote, pointlessly hiding part of his question in the
subject header, that header being:
Subject: Where is max/min ?
In what header file where these macros are defined in standard C?

a) If your question is worth asking, it is worth putting in the body of
your message. Given the almost random character of subject headers,
many people treat them as meaningless scribbles.

b) There are no macros called min or max in C. If there were, they
would probably be called MIN and MAX. Nor are there any such functions.

c) It is trivial to write such functions for any particular type:

inline int imax(int a, int b) { return (a > b) ? a : b; }

That makes it rather pointless to clutter up the standard namespace
with them.

It is not so trivial to write macros with similar functionality. You
might want to do this to defeat the type system, but it isn't
advisable, since most attempts (without using non-standard extensions
to the language) would involve evaluating at least one of the
parameters more than once. Since the parameters may involve
side-effects this is not recommended.
 
N

Nate Eldredge

Richard Heathfield said:
Steven Woody said:

[Subject header: Where is max/min ?]
In what header file where these macros are defined in standard C?

They aren't. Standard C doesn't define max or min. Your implementation
might, but if it does then it has to do it in a way that doesn't interfere
with any strictly conforming program.

There are however fmax/fmaxf/fmaxl to find the maximum of two double /
float / long double values, and fmin/fminf/fminl respectively. They are
standard C and declared in <math.h>. They are functions rather than
macros.
 
S

Steven Woody

Steven Woody wrote, pointlessly hiding part of his question in the
subject header, that header being:
Subject: Where is max/min ?


a) If your question is worth asking, it is worth putting in the body of
your message.  Given the almost random character of subject headers,
many people treat them as meaningless scribbles.

Do you mean my email subject is not clear or too short to describe the
problem? If so, I am sorry.
b) There are no macros called min or max in C.  If there were, they
would probably be called MIN and MAX.  Nor are there any such functions..

I know how to write them in macro definition, but I always like to
find a build-in one if there is any. By your answer, I know there is
actually a standard build-in one.
c) It is trivial to write such functions for any particular type:

    inline int imax(int a, int b) { return (a > b) ? a : b; }

    That makes it rather pointless to clutter up the standard namespace
    with them.

Writing many kind of inline max/min is really tedious. What side-
effect can happen on a macro like
"#define min(a,b) ((a)<(b)?(a):(b))"

Thanks!
 
I

Ian Collins

Steven said:
Writing many kind of inline max/min is really tedious. What side-
effect can happen on a macro like
"#define min(a,b) ((a)<(b)?(a):(b))"

Consider

min(*(p++), *(p1++));

or

min(someFn(), otherFn());
 
M

Martin Ambuhl

Steven said:
I know how to write them in macro definition, but I always like to
find a build-in one if there is any. By your answer, I know there is
actually a standard build-in one.

By my answer, which states that there are *no* such macros and there are
*no* such funtions, you conclude that there are such things.
In your next class in logic you can explain how "there are no x" implies
"there are x".

Further, I doubt that "I know how to write them in macro definition" is
true, unless you mean that you know how to write a potentially dangerous
macro.

What side-
effect can happen on a macro like
"#define min(a,b) ((a)<(b)?(a):(b))"

foo = min(x++,y--);

or suppose f() and g() are functions with side effects:

foo = min(f(),g());

This should be covered in even the most elementary C course.
 
J

Joachim Schmitz

Steven said:
Do you mean my email subject is not clear or too short to describe the
problem? If so, I am sorry.

No, he meant to say that you should put your question into the body of your
article (which isn't an email, BTW), possibly in addition to putting it into
the Subject.

Bye, Jojo
 
J

jacob navia

Steven said:
Hi,

In what header file where these macros are defined in standard C?
Thanks.
lcc-win defines them in stdlib.h
Microsoft msvc does the same
 
M

Martin Ambuhl

jacob said:
lcc-win defines them in stdlib.h
Microsoft msvc does the same

Note the question, asking for their location "in standard C?".
Note that the identifiers "max" and "min" are not in the
implementation's namespace.
That lcc-win and Microsoft might have these in stdlib.n (ho angle
brackets!) is irrelevant. Not only is that answer not repsonsive to the
question asked, but it passes over without comment that lcc-win and
Microsft are (if Jacob is right) illegally invading the programmer's
namepace and potentially breaking completely legal portable programs.

But Jacob doesn't give a shit.
 
J

jacob navia

Martin said:
Note the question, asking for their location "in standard C?".
Note that the identifiers "max" and "min" are not in the
implementation's namespace.
That lcc-win and Microsoft might have these in stdlib.n (ho angle
brackets!) is irrelevant. Not only is that answer not repsonsive to the
question asked, but it passes over without comment that lcc-win and
Microsft are (if Jacob is right) illegally invading the programmer's
namepace and potentially breaking completely legal portable programs.

But Jacob doesn't give a shit.

About your opinion?
You are right. I do not value it very highly.

Both Microsoftr and lcc-win do not invade the user space. Microsoft
uses __max/__min, and lcc-win will not define them unless -ansic
is required by the user.

I have repeated this in lengthy discussions all over the years.

Why standard C doesn't define them? It is another bad decision.
 
G

Guest

Both Microsoftr and lcc-win do not invade the user space. Microsoft
uses __max/__min, and lcc-win will not define them unless -ansic
is required by the user.

I assume you mean min and max by lcc-win are only defined if -ansic is
*not* specified.
 
K

Keith Thompson

jacob navia said:
lcc-win defines them in stdlib.h

Which means that any code that depends on lcc-win's definitions will
not be portable to other implementations. Note that Steven
specifically asked about standard C.
Microsoft msvc does the same

According to your later followup, MSVC *doesn't* do the same; it
defines __min and __max.

You also said later that your min and max macros aren't defined in
lcc-win's conforming mode ("-ansic"). Just out of curiosity, why did
you choose (a) to define them in <stdlib.h> rather than in some
lcc-win-specific header, where you wouldn't have to worry about
disabling them in conforming mode, and (b) to define them as min and
max rather than MIN and MAX? As you know, one of the benefits of the
convention of using all-caps names for macros is that it reminds the
programmer to avoid arguments with side effects.
 
J

jacob navia

Keith said:
Which means that any code that depends on lcc-win's definitions will
not be portable to other implementations. Note that Steven
specifically asked about standard C.


According to your later followup, MSVC *doesn't* do the same; it
defines __min and __max.

You also said later that your min and max macros aren't defined in
lcc-win's conforming mode ("-ansic"). Just out of curiosity, why did
you choose (a) to define them in <stdlib.h> rather than in some
lcc-win-specific header, where you wouldn't have to worry about
disabling them in conforming mode, and (b) to define them as min and
max rather than MIN and MAX? As you know, one of the benefits of the
convention of using all-caps names for macros is that it reminds the
programmer to avoid arguments with side effects.

(a)
Because MSVC defined them in stdlib.h as min/max before they got
more compliant with bad decisions of the standards committee.

Now, using __min() is maybe better but there *is* a lot of code
that uses this.

C++ defined a template for min/max. Obviously you should never do simple
when you can do more complicated stuff. Then, it is illegal to do:

min(1,2.5)
because the type of the left is int, and the type of the right is double
and BOTH arguments should be of the same type. This led to a lot of work
in a customer I have, where a poor soul was forced to work for a week
following all usages of min/max in a huge code base and change them to
something else.

(b)
Same reason
 
R

Richard Tobin

a) If your question is worth asking, it is worth putting in the body of
your message. Given the almost random character of subject headers,
many people treat them as meaningless scribbles.
[/QUOTE]
Do you mean my email subject is not clear or too short to describe the
problem? If so, I am sorry.

He means that you should put the whole question in the body of the
message, not just in the subject line. Apparently some people use
newsreaders that make it hard to see the subject line while reading
the message, though why they complain to posters rather than the
authors of the newsreader is unclear. The idea mentioned above that
"many people" treat the subect line as "meaningless scribbles" is a
new one, and the solution to it seems obvious.

-- Richard
 
V

vippstar

b) There are no macros called min or max in C. If there were, they
would probably be called MIN and MAX. Nor are there any such functions.

Why is that?
getc and putc are macros, but they're not named GETC and PUTC.
The standard doesn't seem to follow such convention... (although
macros, not macro-like functions, are all-caps, such as INT_MAX)
 
B

Bartc

Steven said:
What side-
effect can happen on a macro like
"#define min(a,b) ((a)<(b)?(a):(b))"

Provided you are aware of the side-effects mentioned in other replies, this
is fairly innocuous. At worst you might have to evaluate one of a or b
twice.

Just avoid assignments, ++/-- and function calls (and perhaps unknown
macros) as parameters.
Writing many kind of inline max/min is really tedious.

Ideally these functions should have been built-in since only the compiler
has the ability to reuse the /value/ of an expression rather than have to
reevaluate. But in the absence of this, using min/max functions isn't that
big a deal, and you only really need 2 or 4 of them (signed/unsigned, maybe
int/long int; while float/double already have min/max apparently).
 
P

Phil Carmody

Bartc said:
Provided you are aware of the side-effects mentioned in other replies, this
is fairly innocuous. At worst you might have to evaluate one of a or b
twice.

Just avoid assignments, ++/-- and function calls (and perhaps unknown
macros) as parameters.

And volatiles. And strings (char*s), if you're prepared for the worst.

Phil
 
B

Ben Bacarisse

Why is that?
getc and putc are macros,

They are not, they are listed a functions. They may be implemented as
macros and they have a special dispensation in this regard but it is
going too far to say that "they are macros".
but they're not named GETC and PUTC.
The standard doesn't seem to follow such convention... (although
macros, not macro-like functions, are all-caps, such as INT_MAX)

I agree with your point, but not your examples. Better examples are
offsetof, assert, isfinite (and friends) along with the type generic
math macros in tgmath.h.
 

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

Min() max() and clamp() 2
min/max in stdlib.h?! 40
Why no min / max 14
min max from tuples in list 23
max of min 6
min-max template 3
min/max by value - 25% faster 10
Min max program 10

Members online

Forum statistics

Threads
473,792
Messages
2,569,639
Members
45,352
Latest member
SherriePet

Latest Threads

Top