Thread: Macros return values

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by sjmp
    So my macro functions max2, max3

    should not be the conditional ops that solve the problem?
    They should be. (I note that your macros MAX2 and MAX3 in post #2 are not function-style macros.)

    Quote Originally Posted by sjmp
    I have to initialize the variables in the function main.
    Yes, or some other function. In this case, trying to initialise variables in the function-style macro is a wrong approach.

    Quote Originally Posted by sjmp
    everything i did works. but clearly I am missing the entire point of this exercise.
    What is your current code?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    945
    Yes, I think you are missing the point of the exercise. The point is to create macros named MAX2 and MAX3 which take 2 and 3 arguments. Your macros MAX2 and MAX3 do not take arguments, so they cannot be used with any variable except for x, y, and z. Others have clearly shown how to create macros that take arguments. All you have to do is rewrite your macros to take arguments the same way.

  3. #18
    Registered User
    Join Date
    Jul 2012
    Posts
    55
    I think I get your point(s) - this is not the way to use a function macro. Got it. But how would I do it better for my assignment.

    Create 2 macros (Max2(x,y) & MAX3(x,y,z) which must return the max value of the given arguments. Macro max3 must use macro max2 - add macros to header file.

    I thought the point of the macro was to do the work(formula....) and then in function main you call it and reference the value of the arguments.

  4. #19
    Registered User
    Join Date
    Jul 2012
    Posts
    55
    Here is my current code:

    defs.h
    Code:
    #ifndef _max_
    #define MAX2 ((x > y) ? x : y)				
    #define MAX3 ((MAX2 > z) ? MAX2 : z)		
    #endif
    code.c

    Code:
    #include <stdio.h>
    #include "defs.h"
    
    
    void main()
    {
    	int x = 10;			
    	int y = 20;
    	int z = 25;
    	
    
    
    	printf("Max value of MAX2 is: %d\n", MAX2);		
    	printf("Max value of MAX3 is: %d\n", MAX3);		
    
    
    }

  5. #20
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Macros doesn't have return values, they are a simple "search and replace" mechanism provided by the preprocessor. "int MAX2" and "int MAX3" in main are local variables which have nothing to do with your macro.

    The post I replied to looked like this before it was mysteriously edited/removed:

    Code:
    #define MAX2(x,y)
    #define MAX3(x,y,z)
    
    int main()
    {
        int x, y, z;
    
        int MAX2 = (x > y) ? x : y;
        int MAX3 = (MAX2 > z) ? MAX2 : z;
    
        printf("Max value of MAX2 is: %d\n", MAX2(x,y));
        printf("Max value of MAX3 is: %d\n", MAX3(x,y,z));
    }
    Last edited by Subsonics; 08-16-2012 at 12:48 PM.

  6. #21
    Registered User
    Join Date
    Jul 2012
    Posts
    55
    I deleted the post you are referring to. I was trying to redo it, but it was wrong. Please refer to #19

  7. #22
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Perhaps what you need now is an example.
    Here is an example of a macro that uses parameters:
    Code:
    #define DETERMINANT(a, b, c) ((b)*(b) - 4*(a)*(c))
    
    int main(void)
    {
        printf("The determinant is %d", DETERMINANT(2, 2+3, 3));
    }
    From seeing that, are you able to write the MAX2 using arguments now?
    Last edited by iMalc; 08-16-2012 at 02:25 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #23
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    This is an excellent tutorial on macros C-Programming "The C Preprocessor"
    I think it explains everything you need to know about macros

  9. #24
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Maybe it would help to see iMalc's code turn into this as well:

    Code:
    int main(void)
    {
        printf("The determinant is %d", ((2+3)*(2+3) - 4*(2)*(3)));
    }
    This is what main.o has inside; the result of "gcc -E main.c". That is the interesting part anyway. A macro function like DETERMINANT(a,b,c) only looks like a function. What you know about regular macros applies just as much to macro functions.
    Last edited by whiteflags; 08-16-2012 at 07:07 PM.

  10. #25
    Registered User
    Join Date
    Jul 2012
    Posts
    55
    iMalc,

    I wrote MAX2 as:
    #define MAX2(x,y) ((x>y) ? x : y)

    Now in defining MAX3 I need to use MAX2 - but I does not look like I can use the macro name of MAX2 in this macro function

    It should be defined as this #define MAX3(x,y,z) but then using another conditional op ((MAX2 > z) ? MAX2 : z)

    But this does not work in the printf ("%d\n", MAX3(MAX2, Z)

    It does not look feasible to use #define MAX3(MAX2, z)

    Could you shed some light as why?

  11. #26
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    You can call MAX2(x, y) from MAX3(x, y, z) but not the way you've done earlier.
    Think of MAX2 as being a function, *not a value*, you pass values as arguments to function, you don't pass functions to function (well not in this case).

    Here's an example, let's say you want to compute the value of tan(x) but you only have the functions sin(), cos() and divby() (where divby(x, y) computes x/y).
    So you would do (independently of using macro functions or C functions):
    Code:
    float tan(float x) { return divby(sin(x), cos(x)); }
    What you're doing in you're code is:
    Code:
    float tan(float x) { return divby(sin, cos); }
    Do you see why it can't work?
    Last edited by root4; 08-17-2012 at 09:20 AM.

  12. #27
    Registered User
    Join Date
    Jul 2012
    Posts
    55
    So this is as far a I have gotten. Stuck on MAX3. Am I on the right track?

    Code:
    #ifndef MAX
    #define MAX2(x,y) ((x>y) ? x : y)
    #define MAX3(MAX2,z) ((MAX2 > z) ? MAX2 : z)
    #endif
    Code:
    void main()
    {
    	printf("Max value of MAX2 is: %d\n", MAX2(10,20));
    	printf("Max value of MAX3 is: %d\n", MAX3(MAX2,25));
    }

  13. #28
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    I don't see anything changed since the last time.

    MAX2 is fine, but MAX3 is a macro function which, by definition, takes 3 arguments: #define MAX3(x, y, z) ...
    Now, how do you compose x, y, z and MAX2 to get it right?

    NOTE: as said earlier in the thread, in a macro definition, be careful to enclose each macro parameter between parenthesis, e.g. "#define ADD(x, y) ((x)+(y))". check the FAQ to understand why.
    Last edited by root4; 08-17-2012 at 10:13 AM.

  14. #29
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    Maybe you should backtrack a bit.
    Remember macros can be used mostly in two ways:
    * either as symbolic constants, e.g. "#define PI 3.14", in which case, you use directly the symbol in your code, e.g. "printf("pi = %f\n", PI);"
    * or as generic functions, e.g. "#define ADD(x, y) ((x)+(y))", in which case you call it as a regular C function, e.g. "printf("5+3 = %u\n", ADD(5, 3));"

  15. #30
    Registered User Kirilenko's Avatar
    Join Date
    Aug 2012
    Location
    France
    Posts
    3
    Well, do you know how to use functions ? With a function-like macro, the behavior is the same : you need to call your macro with arguments. For example, to call your macro MAX2 in the macro MAX3, you should just specify the arguments in a comma-separated list. For example :
    Code:
    #define MAX3(x, y, z) (MAX2(MAX2(x, y), z))
    To speak a bit about terminology, a macro does not "return" a value, but it can be evaluated as an expression, which can have a certain value.
    Last edited by Kirilenko; 08-17-2012 at 01:11 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to use return values
    By Furious5k in forum C++ Programming
    Replies: 3
    Last Post: 02-05-2009, 09:07 PM
  2. Return values
    By pritin in forum C++ Programming
    Replies: 9
    Last Post: 03-26-2007, 05:24 PM
  3. I'll try again: C++ DLL return values for VB
    By WebSnozz in forum C++ Programming
    Replies: 6
    Last Post: 01-20-2003, 01:12 AM
  4. Return values
    By Juganoo in forum C Programming
    Replies: 2
    Last Post: 01-03-2003, 09:28 PM
  5. Return Values ( C )
    By Inept Pig in forum C Programming
    Replies: 2
    Last Post: 04-16-2002, 10:02 AM

Tags for this Thread