# Macros return values

Show 80 post(s) from this thread on one page
Page 2 of 3 First 123 Last
• 08-16-2012
laserlight
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.

• 08-16-2012
christop
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.
• 08-16-2012
sjmp
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.
• 08-16-2012
sjmp
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);                }```
• 08-16-2012
Subsonics
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)); }```
• 08-16-2012
sjmp
I deleted the post you are referring to. I was trying to redo it, but it was wrong. Please refer to #19
• 08-16-2012
iMalc
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?
• 08-16-2012
Click_here
This is an excellent tutorial on macros C-Programming "The C Preprocessor"
I think it explains everything you need to know about macros :)
• 08-16-2012
whiteflags
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.
• 08-17-2012
sjmp
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?
• 08-17-2012
root4
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?
• 08-17-2012
sjmp
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)); }```
• 08-17-2012
root4
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.
• 08-17-2012
root4
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));"
• 08-17-2012
Kirilenko
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.
Show 80 post(s) from this thread on one page
Page 2 of 3 First 123 Last