S
Steven Woody
Hi,
In what header file where these macros are defined in standard C?
Thanks.
In what header file where these macros are defined in standard C?
Thanks.
In what header file where these macros are defined in standard C?
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.
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.
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.
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))"
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.
What side-
effect can happen on a macro like
"#define min(a,b) ((a)<(b)?(a)b))"
Steven said:Do you mean my email subject is not clear or too short to describe the
problem? If so, I am sorry.
lcc-win defines them in stdlib.hSteven said:Hi,
In what header file where these macros are defined in standard C?
Thanks.
jacob said:lcc-win defines them in stdlib.h
Microsoft msvc does the same
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.
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.
jacob navia said:lcc-win defines them in stdlib.h
Microsoft msvc does the same
Consider
min(*(p++), *(p1++));
or
min(someFn(), otherFn());
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.
[/QUOTE]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.
Steven said:What side-
effect can happen on a macro like
"#define min(a,b) ((a)<(b)?(a)b))"
Writing many kind of inline max/min is really tedious.
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.
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)
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.