473,320 Members | 1,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

macro operators?

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
18 2606
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
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
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
"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
"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
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
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
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: foo | last post by:
I'm creating a debug class called debug_mem_allocation for the purpose of finding memory leaks. I used macro's to replace the new and delete operators. My problem is with trying to replace the...
11
by: ex laguna | last post by:
How do I find the lowest bit position of a mask using only macros? I want to do everything in compile time. That mean, there cannot be control statements such as if, while, for, etc. in the...
2
by: Serve La | last post by:
Now that implementations are becoming available, I've started to learn more about C99. Now I was reading about the isgreater/isgreaterequal/isless/... macro's and I'm wondering about why they are...
10
by: Praveen.Kumar.SP | last post by:
Hi Could anyone solve the problem for the code below The Code: #include "stdio.h" #include "iostream.h" void Temp( int a, char* str,...)
19
by: aaragon | last post by:
Hi everyone. A very simple question. I would like to know what is better in terms of performance. I want to use a simple function to obtain the minimum of two values. One way could be using a...
16
by: Santosh Nayak | last post by:
Hi, Is there any way to catch the losing bit occurring due to Right Shift Operator ? e.g int a = 5 ; a = a >1 ; // // a is now 2 and the least significant bit is lost // // I want this...
37
by: junky_fellow | last post by:
hi guys, Can you please suggest that in what cases should a macro be preferred over inline function and viceversa ? Is there any case where using a macro will be more efficient as compared to...
8
by: junjiec | last post by:
Hi, Please tell me how could I have a #$B!J(Bsharp sign) expanded in the macro such as #define INCLUDE(filename) #include <filename> which could do the expand below to include a file...
1
by: todWulff | last post by:
Good day folks. Let me open with the statement that I am not a C++/C programmer. The environment that I am programming in is ARMbasic, an embedded BASIC targeted toward ARM-based...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.