I need help Im using MATLAB No hard codingSolutions generall
Solution
\'s generally a good idea to use extra parentheses when using complex macros. Notice that in the above example, the variable \"x\" is always within its own set of parentheses. This way, it will be evaluated in whole, before being compared to 0 or multiplied by -1. Also, the entire macro is surrounded by parentheses, to prevent it from being contaminated by other code. If you\'re not careful, you run the risk of having the compiler misinterpret your code.
Because of side-effects it is considered a very bad idea to use macro functions as described above.
int x = -10;
int y = ABSOLUTE_VALUE( x++ );
If ABSOLUTE_VALUE() were a real function \'x\' would now have the value of \'-9\', but because it was an argument in a macro it was expanded twice and thus has a value of -8.
Example:
To illustrate the dangers of macros, consider this naive macro
#define MAX(a,b) a>b?a:b
and the code
i = MAX(2,3)+five;
j = MAX(3,2)+5;
check this and consider what the fee after execution might be. The statements are turned into
int i = 2>3?2:3+5;
int j = 3>2?3:2+5;
thus, after execution i=eight and j=3 in preference to the expected end result of i=j=8! that is why you were recommended to use an additional set of parenthesis above, however regardless of these, the road is fraught with risks. The alert reader would possibly fast recognise that if a or b includes expressions, the definition must parenthesize every use of a,b in the macro definition, like this:
#define MAX(a,b) ((a)>(b)?(a):(b))
This works, furnished a,b don\'t have any aspect consequences. indeed,
