Skip to content

Commit 80d2c58

Browse files
committed
Creating your own macros
1 parent dcb17e0 commit 80d2c58

4 files changed

+61
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <stdio.h>
2+
3+
#define UpTo(i,n) for((i)=0;(i)<(n);(i)++) printf("The current step is %d.\n",i)
4+
#define UpTo1(i,n) for((i)=0;(i)<(n);(i)++)
5+
6+
int main(void)
7+
{
8+
int i;
9+
10+
UpTo(i,10);
11+
12+
UpTo1(i,50)
13+
{
14+
printf("The current step is %d.\n",i);
15+
}
16+
17+
return 0;
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <stdio.h>
2+
3+
#define MAX(a,b) ( (a)>(b) ? (a) : (b))
4+
5+
int main(void)
6+
{
7+
int a=43, b=45;
8+
9+
printf("The maximum between the two arguments is MAX(%d,%d):%d.\n",a,b,MAX(a,b));
10+
11+
return 0;
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <stdio.h>
2+
3+
#define FOO (BAR)
4+
#define BAR ("This is interesting")
5+
6+
int main(void)
7+
{
8+
printf("%s.\n",BAR);
9+
return 0;
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <stdio.h>
2+
3+
#define PI 3.14
4+
#define AREA_CIRCLE(r) ((PI)*(r)*(r)) //place the variable between parentheses to keep the order of operations when replacing it with expressions
5+
6+
7+
int main(void)
8+
{
9+
10+
double area=0.0;
11+
area=AREA_CIRCLE(4);
12+
13+
printf("The area of the circle is %f.\n",area);
14+
15+
int c=2;
16+
area=AREA_CIRCLE(c+5);
17+
18+
printf("The area of the circle is %f.\n",area);
19+
20+
return 0;
21+
}

0 commit comments

Comments
 (0)