446,408 Members | 945 Online
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
18 Replies

 P: n/a On Feb 20, 7:14 pm, "Zach" (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" (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" (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 San Diego Supercomputer Center <* We must do something. This is something. Therefore, we must do this. Feb 21 '07 #5

 P: n/a "user923005" use of ? and : means it's a conditional function. in ur stmt "#definemax(a, b) ((a)>(b)?(a):(b))" larger number between a and b will return from function max. use ofmacro, means the value of max is assigned at compile time. this makesefficient 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 San Diego Supercomputer Center <* 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" (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" (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 (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 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 San Diego Supercomputer Center <* We must do something. This is something. Therefore, we must do this. Feb 22 '07 #11

 P: n/a Keith Thompson wrote: > pete

 P: n/a Keith Thompson said: pete Jack Klein wrote: >>Only a return statement in a function returns a value. The unary & operator is also described by the C standardas 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

 P: n/a Jack Klein wrote: > On Thu, 22 Feb 2007 01:37:35 GMT, pete >, &, ^, 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 pete San Diego Supercomputer Center <* We must do something. This is something. Therefore, we must do this. Feb 22 '07 #17

 P: n/a Richard Heathfield pete >Jack Klein wrote:Only a return statement in a function returns a value.The unary & operator is also described by the C standardas returning a value. C99 6.5.3.2p3: The unary & operator yields the address of its operand.Functions *return* values; expressions *yield* values. (A functioncall, of course, is an expression that yields the value returned bythe 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 San Diego Supercomputer Center <* 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 >Jack Klein wrote:Only a return statement in a function returns a value.The unary & operator is also described by the C standardas 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. Feb 22 '07 #19