#include <stdio.h>
// 2.1
#define subm(a,b) (a - b)
#define cubem(a) (a * a * a)
#define minm minf
#define oddm oddf
//---------------------------Given Code------------------------------------
int subf(int a, int b) {
return a - b;
}
int cubef(int a) {
return a * a * a;
}
int minf(int a, int b) {
if (a <= b) {
return a;
} else {
return b;
}
}
int oddf(int a) {
if (a % 2 == 0) {
return 0;
} else {
return 1;
}
}
//----------------------------Given Code----------------------------------
// 2.2
int main() {
int a = 5;
int b = 7;
subf(a,b);printf("subf = %d\n", subf(a,b));
subm(a,b);printf("subm = %d\n", subm(a,b));
subf(a++,b--);printf("subf = %d\n", subf(a++,b--));
subm(a++,b--);printf("subm = %d\n", subm(a++,b--));
cubef(a);printf("cubef = %d\n", cubef(a));
cubem(a);printf("cubem = %d\n", cubem(a));
cubef(--a);printf("cubef = %d\n", cubef(--a));
cubem(--a);printf("cubem = %d\n", cubem(--a));
minf(a,b);printf("minf = %d\n", minf(a,b));
minm(a,b);printf("minm = %d\n", minm(a,b));
minf(--a,--b);printf("minf = %d\n", minf(--a,--b));
minm(--a,--b);printf("minm = %d\n", minm(--a,--b));
oddf(a);printf("oddf = %d\n", oddf(a));
oddm(a);printf("oddm = %d\n", oddm(a));
oddf(a++);printf("oddf = %d\n", oddf(a++));
oddm(a++);printf("oddm = %d\n", oddm(a++));
}
I'm having some trouble with putting the functions inside the macro. My professor wants us to understand how macro and functions are processed. The way I'm doing it is basically as you see here, but it's not working correctly, or at least
#define cubem(a) (a * a * a)
Is generating an error and I don't know why. Can someone please help?
edit: the error is as shown
hw02q2.c:42:31: warning: multiple unsequenced modifications to 'a'
[-Wunsequenced]
printf("cubem = %d\n", cubem(--a));
^~
hw02q2.c:4:19: note: expanded from macro 'cubem'
#define cubem(a) (a * a * a)
The reason is that
#define cubem(a) (a * a * a)
/* and later using it .... */
printf("cubem = %d\n", cubem(--a));
does text substitution, and produces
printf("cubem = %d\n", (--a * --a * --a));
which modifies a three times in one statement. That is undefined behaviour according to the C standard.
In comparison,
int cubef(int a) {
return a * a * a;
}
/* and later */
printf("cubef = %d\n", cubef(--a));
evaluates --a once, passes the resultant value to cubef().
If you really want to "put a function in a macro", then do something like
#define cubem(a) cubef(a)
which causes the statement
printf("cubem = %d\n", cubem(--a));
to become
printf("cubem = %d\n", cubef(--a));
The problem with this is that it does not work with a macro that uses its argument more than once. For example
int sq(int a) {return a * a;}
#define cubesq(a) (a * sq(a)) /* uses a more than once
causes
printf("cubesq = %d\n", cubesq(--a));
to be seen by the compiler as
printf("cubem = %d\n", (--a * cubesq(--a));
which, again, modifies a more once and causes undefined behaviour.
The reason is simple: the preprocessor does TEXT SUBSTITUTION and modifies source code seen by a later phase of the compiler.
Instead of "trying to put a function inside a macro", simply don't use macros. Write functions. Use functions. Or use macros, but respect their limitations (i.e. they don't work like functions do, even if they look like functions).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With