Chapter 7: The C Preprocessor

Macro Expansion, Page number: 244

In [1]:
#Macro declaration
UPPER = 25

for i in range(1,UPPER+1): #macro expansion
    print(i)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

Macro Definition, Page number: 244

In [2]:
#Macro declaration
PI = 3.1415

#Variable declaration
r = 6.25 

#Calculation
area = PI * r * r

#Result
print "Area of circle = ", area 
Area of circle =  122.71484375

Macros with Arguments, Page number: 248

In [3]:
#Macro declaration
def AREA(x): #define AREA(x) ( 3.14 * x * x )
    return(3.14 * x * x )

#Variable declaration
r1 = 6.25
r2 = 2.5

#Result
a = AREA(r1)
print  "Area of circle = ", a 
a = AREA(r2) 
print "Area of circle = ", a
 
Area of circle =  122.65625
Area of circle =  19.625

Macros with Arguments, Page number: 249

In [7]:
#Macro declaration
def ISDIGIT(y): #define ISDIGIT(y) ( y >= 48 && y <= 57 )
    return( y >= 48 and y <= 57 )

#Input from user
#ch = raw_input("Enter any digit ")
ch = 'a'

#Result
if ISDIGIT ( ch ):
    print  "You entered a digit" 
else:
    print  "Illegal input"  
Illegal input
In [ ]: