Why is not the same as the printed result
CodePudding user response:
The interference fringes in the photo you see I've got a pain in my skull, will not be able to copy the text coming?Questions about the above i++ because # define are direct replacement, so the second picture of SQ (i++) will be replaced with (i++) * (i++), added a,
CodePudding user response:
For reference://take parameters of a certain similarities between macro definition and function, but essentially there's a difference:
//1. The function call, the first to find out the value of the argument expressions and then plug in parameter, and using the macro with parameters just do character replacement,
//2. The function calls are processed in the program is running, temporary memory location for the parameter distribution, and the macro substitution is in the preprocessing stage, when the replacement does not allocate memory unit,
//no value transfer processing, also does not have the concept of "return value",
//3. The function of the argument and parameter to define the types, the two types of requirements, such as inconsistent, should be type conversion, and macro type does not exist problem, macro name type,
//it or type of parameters, is just a symbol representing, replacement, plug in the specified string to define macros, string can be any type of data,
//4. The function can only get a return value, and use macros can try to get some results,
//5. Use the number of macro, macro expansion after the source program gets longer, because each launch a program growth, while the function calls will not make the source program,
//6. Macro substitution does not account for running time, account for only a preprocessing time, while the function calls for running time (allocation unit, the scene, value transfer, return),
# include
# define SQ (y) (y) * (y))
//take parameters of macro definition: take parameters of macro definition is not a simple string substitution, but also to replace parameters
//# define S (r) PI * r * r
//area=S (a + b); -> area=PI * a, a + b + b *.
//# define S (r) PI * * (r) (r)
//area=S (a + b); -> area=PI * (a + b) * (a + b);
Int main ()
{
int i=1;
While (i<{
=5)Printf (" % d \ n ", SQ (i++));
//this is replaced by:
//printf (" % d \ n ", ((i++) * (i++)));
}
return 0;
}
//I=1, 2 & gt; While (i<=5) - & gt; Printf (" % d \ n ", ((i++) * (i++))); 1 * 2=2 (i++), I (i++)=3;
//I=3-12 & gt; While (i<=5) - & gt; Printf (" % d \ n ", ((i++) * (i++))); 3 * 4=12 (i++), I (i++)=5;
//30 I=5 - & gt; While (i<=5) - & gt; Printf (" % d \ n ", ((i++) * (i++))); 5 * 6=30 (i++), I (i++)=6;
//please press any key to continue...
CodePudding user response:
//when the called function:
//I=1 to 1 & gt; While (i<=5) - & gt; Printf (" SQ=% d \ n ", SQ (i++)); 1 * 1=` 1, I (i++)=2;
//I=2-4 & gt; While (i<=5) - & gt; Printf (" SQ=% d \ n ", SQ (i++)); 2 * 2=4, I (i++)=3;
//I=3-9 & gt; While (i<=5) - & gt; Printf (" SQ=% d \ n ", SQ (i++)); 3 * 3=9, I (i++)=4;
//I=4-16 & gt; While (i<=5) - & gt; Printf (" SQ=% d \ n ", SQ (i++)); 16, 4 * 4=I (i++)=5;
//I=5-25 & gt; While (i<=5) - & gt; Printf (" SQ=% d \ n ", SQ (i++)); 5 * 5=25, I (i++)=6;
//please press any key to continue...