By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,408 Members | 945 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,408 IT Pros & Developers. It's quick & easy.

macro operators?

P: n/a
Can someone list the various macro operators and what they mean. Came
across a function macro:

#define max(a, b) ((a)>(b)?(a):(b))

What does "?" amd ":" mean in this statement?

Zach

Feb 21 '07 #1
Share this Question
Share on Google+
18 Replies


P: n/a
On Feb 20, 7:14 pm, "Zach" <net...@gmail.comwrote:
Can someone list the various macro operators and what they mean. Came
across a function macro:

#define max(a, b) ((a)>(b)?(a):(b))

What does "?" amd ":" mean in this statement?
http://www.phim.unibe.ch/comp_doc/c_...nditional.html
K&R2, p51,58
H&S3, p198

Related, from the C-FAQ:
3.16: I have a complicated expression which I have to assign to one of
two variables, depending on a condition. Can I use code like
this?

((condition) ? a : b) = complicated_expression;

A: No. The ?: operator, like most operators, yields a value, and
you can't assign to a value. (In other words, ?: does not yield
an "lvalue".) If you really want to, you can try something like

*((condition) ? &a : &b) = complicated_expression;

although this is admittedly not as pretty.

References: ISO Sec. 6.3.15; H&S Sec. 7.1 pp. 179-180.

Feb 21 '07 #2

P: n/a
use of ? and : means it's a conditional function. in ur stmt "#define
max(a, b) ((a)>(b)?(a):(b))
" larger number between a and b will return from function max. use of
macro, means the value of max is assigned at compile time. this makes
efficient to the program execution.
Zach wrote:
Can someone list the various macro operators and what they mean. Came
across a function macro:

#define max(a, b) ((a)>(b)?(a):(b))

What does "?" amd ":" mean in this statement?

Zach
Feb 21 '07 #3

P: n/a
On Feb 20, 7:49 pm, "madhawi" <madhaw...@gmail.comwrote:
use of ? and : means it's a conditional function. in ur stmt "#define
max(a, b) ((a)>(b)?(a):(b))
" larger number between a and b will return from function max. use of
macro, means the value of max is assigned at compile time. this makes
efficient to the program execution.
And also quite dangerous for all of the subtle side effects it causes.

foo = max(a++,b++);

[snip]

Feb 21 '07 #4

P: n/a
"Zach" <ne****@gmail.comwrites:
Can someone list the various macro operators and what they mean. Came
across a function macro:

#define max(a, b) ((a)>(b)?(a):(b))

What does "?" amd ":" mean in this statement?
The "?:" conditional operator is not specific to macros; this
particular macro definition just happens to use it.

Any decent C textbook or other reference should tell you about the
conditional operator. If you don't have such a reference, you should
get one (K&R2 is very good).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 21 '07 #5

P: n/a
"user923005" <dc*****@connx.comwrites:
On Feb 20, 7:49 pm, "madhawi" <madhaw...@gmail.comwrote:
>use of ? and : means it's a conditional function. in ur stmt "#define
max(a, b) ((a)>(b)?(a):(b))
" larger number between a and b will return from function max. use of
macro, means the value of max is assigned at compile time. this makes
efficient to the program execution.

And also quite dangerous for all of the subtle side effects it causes.

foo = max(a++,b++);
For knowledgeable programmers, this can be avoided by giving the macro
an all-caps name, such as:

#define MAX(a, b) ((a)>(b)?(a):(b))

Macros conventionally have all-caps names. (This isn't an absolute
rule, just a convention.) This should warn the user that MAX() isn't
an ordinary function, and might evaluate its arguments more than once.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 21 '07 #6

P: n/a
On 20 Feb 2007 19:49:41 -0800, "madhawi" <ma*******@gmail.comwrote
in comp.lang.c:

Please don't top post. Material in your reply belongs after quoted
text it refers to. I have reformatted your post, just this once.
Zach wrote:
Can someone list the various macro operators and what they mean. Came
across a function macro:

#define max(a, b) ((a)>(b)?(a):(b))

What does "?" amd ":" mean in this statement?

Zach
use of ? and : means it's a conditional function. in ur stmt "#define
max(a, b) ((a)>(b)?(a):(b))
" larger number between a and b will return from function max. use of
This is a macro, not a function at all. So it can't return a value.
Only a return statement in a function returns a value.
macro, means the value of max is assigned at compile time. this makes
This is completely incorrect. The value of this expression is
determined at run time. The expression has no value at all at compile
time.
efficient to the program execution.
Perhaps it is more efficient, perhaps it is not. If the body of the
function is in scope when it is called, a compiler might inline it
anyway.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Feb 21 '07 #7

P: n/a
Jack Klein wrote:
Only a return statement in a function returns a value.
The unary & operator is also described by the C standard
as returning a value.

--
pete
Feb 22 '07 #8

P: n/a
user923005 wrote:
>
On Feb 20, 7:49 pm, "madhawi" <madhaw...@gmail.comwrote:
use of ? and : means it's a conditional function.
in ur stmt "#define
max(a, b) ((a)>(b)?(a):(b))
" larger number between a and b will return from function max.
use of
macro, means the value of max is assigned at compile time.
this makes
efficient to the program execution.

And also quite dangerous for all of the subtle side effects it causes.
Your use of the word "also"
makes it seem as though you agree
with something that madhawi has stated.

The value of max is determinable at compile time only if
the values of a and b are determinable at compile time.

It makes absolutely no difference to the efficiency
of program execution whether the conditional operator
is placed in a macro, or if the conditional operator is inlined.

--
pete
Feb 22 '07 #9

P: n/a
On Feb 21, 5:46 pm, pete <pfil...@mindspring.comwrote:
user923005 wrote:
On Feb 20, 7:49 pm, "madhawi" <madhaw...@gmail.comwrote:
use of ? and : means it's a conditional function.
in ur stmt "#define
max(a, b) ((a)>(b)?(a):(b))
" larger number between a and b will return from function max.
use of
macro, means the value of max is assigned at compile time.
this makes
efficient to the program execution.
And also quite dangerous for all of the subtle side effects it causes.

Your use of the word "also"
makes it seem as though you agree
with something that madhawi has stated.
I did not intend to intimate partial agreement.
The value of max is determinable at compile time only if
the values of a and b are determinable at compile time.

It makes absolutely no difference to the efficiency
of program execution whether the conditional operator
is placed in a macro, or if the conditional operator is inlined.
I have railed against the use of macros for functions on many
occasions (though I will admit to doing it myself from time to time).

Feb 22 '07 #10

P: n/a
pete <pf*****@mindspring.comwrites:
Jack Klein wrote:
>Only a return statement in a function returns a value.

The unary & operator is also described by the C standard
as returning a value.
C99 6.5.3.2p3:

The unary & operator yields the address of its operand.

Functions *return* values; expressions *yield* values. (A function
call, of course, is an expression that yields the value returned by
the called function.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 22 '07 #11

P: n/a
Keith Thompson wrote:
>
pete <pf*****@mindspring.comwrites:
Jack Klein wrote:
Only a return statement in a function returns a value.
The unary & operator is also described by the C standard
as returning a value.

C99 6.5.3.2p3:

The unary & operator yields the address of its operand.
That's not what my copy says.
ISO/IEC
9899
Second edition
1999-12-01

6.5.3.2 Address and indirection operators
3 The unary & operator returns the address of its operand.

--
pete
Feb 22 '07 #12

P: n/a
Keith Thompson said:
pete <pf*****@mindspring.comwrites:
>Jack Klein wrote:
>>Only a return statement in a function returns a value.

The unary & operator is also described by the C standard
as returning a value.

C99 6.5.3.2p3:

The unary & operator yields the address of its operand.

Functions *return* values; expressions *yield* values. (A function
call, of course, is an expression that yields the value returned by
the called function.)
This is a change from C90, which some of us still consider canonical.
:-)

For your reference, the original text was:

"Some operators (the unary operator ~ , and the binary operators << ,
>, & , ^ , and | , collectively described as bitwise operators
)shall have operands that have integral type. These operators return
values that depend on the internal representations of integers, and
thus have implementation-defined aspects for signed types."
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Feb 22 '07 #13

P: n/a
On Thu, 22 Feb 2007 01:37:35 GMT, pete <pf*****@mindspring.comwrote
in comp.lang.c:
Jack Klein wrote:
Only a return statement in a function returns a value.

The unary & operator is also described by the C standard
as returning a value.
Yes, an unfortunate oversight that crept into C99.

In C there is a return statement that, when used with an expression,
returns a value. It is also possible for a return statement to omit
the expression, therefore not all return statements return values.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Feb 22 '07 #14

P: n/a
Jack Klein wrote:
>
On Thu, 22 Feb 2007 01:37:35 GMT, pete <pf*****@mindspring.comwrote
in comp.lang.c:
Jack Klein wrote:
Only a return statement in a function returns a value.
The unary & operator is also described by the C standard
as returning a value.

Yes, an unfortunate oversight that crept into C99.
But the C90 standard, 6.3 Expressions, paragraph 4,
describes a whole bunch of operators as returning values.

ISO/IEC 9899: 1990

6.3 Expressions

Some operators
(the unary operator ~, and the binary operators <<, >>, &, ^, and |,
collectively described as bitwise operators)
shall have operands that have integral type.
These operators return values
that depend on the internal representations of integers,
and thus have implementation-defined aspects for signed types.

--
pete
Feb 22 '07 #15

P: n/a
Zach wrote:
Can someone list the various macro operators and what they mean.
There's no such thing as a macro operator. There are just operators.
Came across a function macro:

#define max(a, b) ((a)>(b)?(a):(b))

What does "?" amd ":" mean in this statement?
It's the conditional operator -- the only three-operand operator in C.
It takes 3 expressions, here called E1, E2 and E3.

E1 ? E2 : E3

If E1 is not equal to zero, then it will evaluate E2 and have E2's
value. Otherwise, it will evaluate E3 and have E3's value.

--
Simon.
Feb 22 '07 #16

P: n/a
pete <pf*****@mindspring.comwrites:
Keith Thompson wrote:
>pete <pf*****@mindspring.comwrites:
Jack Klein wrote:

Only a return statement in a function returns a value.

The unary & operator is also described by the C standard
as returning a value.

C99 6.5.3.2p3:

The unary & operator yields the address of its operand.

That's not what my copy says.
ISO/IEC
9899
Second edition
1999-12-01

6.5.3.2 Address and indirection operators
3 The unary & operator returns the address of its operand.
Sorry, I was quoting from n1124, and I didn't notice that there's a
change bar on that line. Apparently the committee felt that the word
"returns" was incorrect, and changed it to "yields".

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 22 '07 #17

P: n/a
Richard Heathfield <rj*@see.sig.invalidwrites:
Keith Thompson said:
>pete <pf*****@mindspring.comwrites:
>>Jack Klein wrote:

Only a return statement in a function returns a value.

The unary & operator is also described by the C standard
as returning a value.

C99 6.5.3.2p3:

The unary & operator yields the address of its operand.

Functions *return* values; expressions *yield* values. (A function
call, of course, is an expression that yields the value returned by
the called function.)

This is a change from C90, which some of us still consider canonical.
:-)
It's actually a change from C99; my canon misfired.
For your reference, the original text was:

"Some operators (the unary operator ~ , and the binary operators << ,
>>, & , ^ , and | , collectively described as bitwise operators
)shall have operands that have integral type. These operators return
values that depend on the internal representations of integers, and
thus have implementation-defined aspects for signed types."
n1124 6.5p4:

Some operators (the unary operator ~, and the binary operators <<,
>>, &, ^, and |, collectively described as bitwise operators) are
required to have operands that have integer type. These operators
yield values that depend on the internal representations of
integers, and have implementation-defined and undefined aspects
for signed types.

There's a change bar on the line with the word "yields".

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 22 '07 #18

P: n/a
pete wrote:
Keith Thompson wrote:
>pete <pf*****@mindspring.comwrites:
>>Jack Klein wrote:

Only a return statement in a function returns a value.

The unary & operator is also described by the C standard
as returning a value.

C99 6.5.3.2p3:

The unary & operator yields the address of its operand.

That's not what my copy says.
ISO/IEC
9899
Second edition
1999-12-01

6.5.3.2 Address and indirection operators
3 The unary & operator returns the address of its operand.
N1124 has 'yields' here. The line is marked as changed from C99.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

Feb 22 '07 #19

This discussion thread is closed

Replies have been disabled for this discussion.