#include #include #define square1( a ) a * a #define square2( a ) ((a) * (a)) /* * Another potential problem with macros is that argument * expressions that are not carefully parenthesized can produce * erroneous results due to operator precedence and binding. * * For example, 2*square1( 3 + 4 ) expands to * * result = 2 * 3 + 4 * 3 + 4, which is interprete by the preprocessor as * * result = (2 * 3) + (4 * 3) + 4 * * but 2*square2( 3 + 4 ) expands to * * result = 2 *((3 + 4) * (3 + 4)) */ int square(int a); int main(void) { int result; printf("Sample 1: using macro: square1( a ) a * a on the input expression '3+4'\n"); result = 2*square1(3+4); printf("Value of result: %d\n", result); printf("Sample 2: using macro: square2( a ) ((a) * (a)) on the input expression '3+4'\n"); result = 2*square2(3+4); printf("Value of result: %d\n", result); result = 2*square(3+4); printf("Value of result from the function call square(): %d\n", result); return EXIT_SUCCESS; } int square(int a) { return a*a; }