Chapter 21 : The C Preprocessor

Example: 1, Page No.5.149

In [11]:
#Print square and cube Values by using macro substitutions. 
def sq(n):
    return n*n
    
    
def cube(n):
    return n*n*n
    

a=5
b=3
s=c=0

#Function calling
s=sq(a)
c=cube(b)

#Result
print "Square value of 5 is %d"%(s)
print "Cube value of 3 is %d"%(c)
Square value of 5 is 25
Cube value of 3 is 27

Example: 2, Page No. 5.151

In [12]:
#Print the Values using macro substitution.

VAL=40

print "The value is %d"%VAL
The value is 40

Example: 3, Page No.:5.151

In [15]:
#Print Value and text using preprocessing.
VAL=35

HELLO="HELLO"

res=VAL-5

print "Res=VAL-5:Res= %d"%res

print "HELLO"
Res=VAL-5:Res= 30
HELLO

Example: 4, Page No.:5.152

In [16]:
#Program to print the value using preprocessors.
USD=1
UKP=1

currency_rate=46
currency_rate=100

rs=10*currency_rate
print  "The Value of rs is : %d"%rs
The Value of rs is : 1000