Macros are available in most high-level programming languages. The body of a mac
ID: 3897891 • Letter: M
Question
Macros are available in most high-level programming languages. The body of a macro is simply used to replace a macro-cal1 during the preprocessing stage. A macro introduces a "true inline" function that is normally more efficient than an "out line" function. However, macros suffer from the side-effect, unwanted, or unexpected modifications to variables Macros should be used cautiously. The main purpose of the following programs are to demonstrate the differences between a function and a macro. Other purposes include demonstrating the differences between different programming environments, and learning different ways of writing comments, formatted input and output, variable declaration and initialization, unary operation ++, macro definition/call, function definition/call, if -then-else and loop structures, etc. Observe each of the functions below and understand their functionality. Use both GNU gcc under Unix AND Visual Studio to implement the code in this question int subf(int a, int b) { return a - b; int cubef(int a) { return a*a a; int minf(int a, int b) return a; return b; ?f (aExplanation / Answer
#include <stdio.h>
#define subm(a, b) (a*b)
#define cubem(a) (a * a * a)
#define minm(a, b) (a <= b? a: b)
#define oddm(a) (a % 2 == 0? 0 : 1)
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;
}
int main()
{
int a = 5, b = 7;
a = 5; b = 7;
printf("a = %d, b = %d ", a, b);
printf("subf(a, b) = %d ",subf(a, b));
a = 5; b = 7;
printf("subm(a, b) = %d ",subm(a, b));
a = 5; b = 7;
printf(" subf(a++, b--) = %d ", subf(a++, b--));
a = 5; b = 7;
printf("subm(a++, b--) = %d ", subm(a++, b--));
a = 5; b = 7;
printf("cubef(a) = %d ", cubef(a));
a = 5; b = 7;
printf("cubem(a) = %d ", cubem(a));
a = 5; b = 7;
printf("cubef(--a) = %d ", cubef(--a));
a = 5; b = 7;
printf("cubem(--a) = %d ", cubem(--a));
a = 5; b = 7;
printf("minf(a, b) = %d ", minf(a, b));
a = 5; b = 7;
printf("minm(a, b) = %d ", minm(a, b));
a = 5; b = 7;
printf("minf(--a, --b) = %d ", minf(--a, --b));
a = 5; b = 7;
printf("minm(--a, --b) = %d ", minm(--a, --b));
a = 5; b = 7;
printf("oddf(a) = %d ", oddf(a));
a = 5; b = 7;
printf("oddm(a) = %d ", oddm(a));
a = 5; b = 7;
printf("oddf(a++) = %d ", oddf(a++));
a = 5; b = 7;
printf("oddm(a++) = %d ", oddm(a++));
}
output
=====
a = 5, b = 7
subf(a, b) = 35
subm(a, b) = 35
subf(a++, b--) = 35
subm(a++, b--) = 35
cubef(a) = 125
cubem(a) = 125
cubef(--a) = 64
cubem(--a) = 24
minf(a, b) = 5
minm(a, b) = 5
minf(--a, --b) = 4
minm(--a, --b) = 3
oddf(a) = 1
oddm(a) = 1
oddf(a++) = 1
oddm(a++) = 1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.