+ 4

[Solved] Macro vs function ???

Pls explain the result. I am much new to macro's or overloading in C. https://code.sololearn.com/cHMXTEK229jp/?ref=app

13th Jul 2019, 11:42 PM
To_be_unnamed(_?_^@%∞)
To_be_unnamed(_?_^@%∞) - avatar
13 Antworten
+ 4
hepy(z) (7*z) y=2; hepy(y+1); the value pass to the macro will replace the expression z=y+1 7*y+1; y=2 7*2+1 //multiplication precede addition 14+1=15
13th Jul 2019, 11:50 PM
✳AsterisK✳
✳AsterisK✳ - avatar
+ 2
It's not operator overloading and there is no operator overloading in c. It's about macros.
15th Jul 2019, 7:06 AM
Manoj
Manoj - avatar
+ 1
This is not called operator overloading though.
14th Jul 2019, 1:32 AM
Sonic
Sonic - avatar
+ 1
Can we say that "hapy (z)" is a function, "z" is an argument, and "(7 * z)" is the return value?
14th Jul 2019, 8:04 PM
Alex Gyperkate
Alex Gyperkate - avatar
+ 1
✳AsterisK✳ thank you. But why 7*z will transform to 7*y+1 while z = (y+1)? The case is what you wrote but I have little idea about macros (that's why I hate them). Edit: Understood. Macro passes expressions as is unlike function. So if I add one more () arround y+1, 21 is the result. Anyway thanks.
14th Jul 2019, 10:19 PM
To_be_unnamed(_?_^@%∞)
To_be_unnamed(_?_^@%∞) - avatar
+ 1
To_be_unnamed(_?_^@%∞) and yet the analogy with the function can be used. For example: #include <stdio.h> int macro(int z) { return (z+1); } int main() { int y=2; printf("%d",macro(7*y)); return 0; }
14th Jul 2019, 11:10 PM
Alex Gyperkate
Alex Gyperkate - avatar
+ 1
what is the difference? https://code.sololearn.com/cbrFD2qqFM64/?ref=app Macros can be called as small functions that are not as overhead to process. If we have to write a function (having a small definition) that needs to be called recursively (again and again), then we should prefer a macro over a function.
15th Jul 2019, 5:03 AM
Alex Gyperkate
Alex Gyperkate - avatar
+ 1
I seem to have found a solution, look at the code please https://code.sololearn.com/cbrFD2qqFM64/?ref=app
15th Jul 2019, 8:25 AM
Alex Gyperkate
Alex Gyperkate - avatar
15th Jul 2019, 8:57 AM
To_be_unnamed(_?_^@%∞)
To_be_unnamed(_?_^@%∞) - avatar
0
Sonic yeah it sould be macro
14th Jul 2019, 10:14 PM
To_be_unnamed(_?_^@%∞)
To_be_unnamed(_?_^@%∞) - avatar
0
Alex Gyperkate if hapy(z) is a function then the result would be 21 not 15
14th Jul 2019, 10:16 PM
To_be_unnamed(_?_^@%∞)
To_be_unnamed(_?_^@%∞) - avatar
0
Alex Gyperkate that's a different question.
14th Jul 2019, 11:48 PM
To_be_unnamed(_?_^@%∞)
To_be_unnamed(_?_^@%∞) - avatar
0
This is elementary, but it is possible to make an analogy The #define directive is used to create object-like macros for constants based on values or expressions. #define can also be used to create function-like macros with arguments that will be replaced by the preprocessor. A function: • is a block of code that performs a specific task • is reusable • makes a program easier to test • can be modified without changing the calling program
17th Jul 2019, 8:11 AM
Alex Gyperkate
Alex Gyperkate - avatar