CHAPTER 7:THE C PREPROCESSOR

EXAMPLE ON PAGE:226

In [1]:
 
def UPPER():                                                    #set UPPER() as 25
    return 25
for i in range(1,UPPER()+1,1):
    print "%d\n" % (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

EXAMPLE ON PAGE:227

In [2]:
 
def PI():                                                #set PI() as 3.14
    return 3.14
r=6.25
area=PI()*r*r
print "Area of circle=%f\n" % (area)
Area of circle=122.656250

EXAMPLE ON PAGE:229

In [3]:
 
def AND(x,y):                                                   #set AND(x,y) for "and" operation
    return (x and y)
def OR(a,b):                                                    #set OR(a,b) for "or" operation
    return (a or b)
f=1
x=4
y=90
if (AND(f<5,OR(x<=20,y<=45))):
    print "Your PC will always work fine...\n"
else:
    print "In front of the maintenance man\n"
Your PC will always work fine...

EXAMPLE ON PAGE:229

In [4]:
 
def AND(x,y):                                                 #set AND(x,y) for "and" operation
    return (x and y)
def ARANGE():
    return (AND(a>25,a<50))                                   #check for a>25 and a<50
a=30
if (ARANGE()):
    print "within range\n"
else:
    print "out of range\n"
within range

EXAMPLE ON PAGE:230

In [5]:
 
def FOUND():
    print "The Yankee Doodle Virus\n"
signature='Y'
if (signature=='Y'):
    FOUND()
else:
    print "Safe... as yet!\n"
The Yankee Doodle Virus

EXAMPLE ON PAGE:230

In [6]:
 
def AREA(x):
    return (3.14*x*x)
r1=6.25
r2=2.5
a=AREA(r1)
print "Area of circle=%f\n" % (a)
a=AREA(r2)
print "Area of circle=%f\n" % (a)
Area of circle=122.656250

Area of circle=19.625000

EXAMPLE ON PAGE:231

In [7]:
 
r1=6.25
r2=2.5
a=3.14*r1*r1
print "Area of circle=%f\n" % (a)
a=3.14*r2*r2
print "Area of circle=%f\n" % (a)
Area of circle=122.656250

Area of circle=19.625000

EXAMPLE ON PAGE:231

In [8]:
 
def ISDIGIT(y):
    return (y>=48 and y<=57)
print "Enter any digit"
ch=raw_input()
if (ISDIGIT(ord(ch))):
    print "You entered a digit\n"
else:
    print "Illegal input\n"
Enter any digit
7
You entered a digit

EXAMPLE ON PAGE:232

In [4]:
 
def SQUARE(n):
    return (n*n)
j=64/SQUARE(4)                                   #it's function not macros ,so j=4
print "j=%d\n" % (j)
j=4

EXAMPLE ON PAGE:233

In [3]:
 
def HLINE():
    for i in range(0,79,1):
        print "%c" % 45,            #characters are bytes and therefore encoding-dependent(non utf-8)
    print "\n"
def VLINE(X,Y):
    print "%c" % 124                #If you want to write 179 in place of 124 then use utf-8 encoding
HLINE()
for y in range(1,25,1):
    VLINE(39,y)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|

EXAMPLE ON PAGE:236-237

In [5]:
 
OKAY=False
if OKAY:
    import logging
    print "statement 1"
    print "statement 2"       #detects virus
    print "statement 3"       
    print "statement 4"       #specific to stone virus
print "statement 5"
print "statement 6"
print "statement 7"
statement 5
statement 6
statement 7

EXAMPLE ON PAGE:237

In [14]:
 
INTEL=False
if INTEL:
    print "code suitable for an Intel PC"
else:
    print "code suitable for a Motorola PC"
print "code common to both the computers"
code suitable for a Motorola PC
code common to both the computers

EXAMPLE ON PAGE:238

In [15]:
 
INTEL=True
if INTEL:
    print "code suitable for a Intel PC"
else:
    print "code suitable for a motorola PC"
print "code common to both the computers"
code suitable for a Intel PC
code common to both the computers

EXAMPLE ON PAGE:239

In [16]:
 
TEST=6
if TEST<=5:
    print "statement 1"
    print "statement 2"
    print "statement 3"
else:
    print "statement 4"
    print "statement 5"
    print "statement 6"
statement 4
statement 5
statement 6

EXAMPLE ON PAGE:239

In [17]:
 
ADAPTER='SVGA'
if ADAPTER=='VGA':
    print "code for video graphics array"
else:
    if ADAPTER=='SVGA':
        print "code for super video graphics array"
    else:
        print "code for extended graphics array"
code for super video graphics array

EXAMPLE ON PAGE:239-240

In [18]:
 
ADAPTER='SVGA'
if ADAPTER=='VGA':
    print "code for video graphics array"
elif ADAPTER=='SVGA':
    print "code for super video graphics array"
else:
    print "code for extented graphics adapter"
code for super video graphics array

EXAMPLE ON PAGE:241

In [19]:
 
def fun1():
    print "Inside fun1\n"
def fun2():
    print "Inside fun 2\n"
def main():
    print "Inside main\n"
fun1()
main()
fun2()
Inside fun1

Inside main

Inside fun 2

EXAMPLE ON PAGE:242

In [20]:
 
def f1():
    a=5
def f2(x):
    print "Inside f2"                             #passed parameter is not being used
def f3():
    x=6
    return x
    x+=1                                          #control can never reach this statement
f1()
f2(7)
f3()
Inside f2
Out[20]:
6