# Chapter 2 Declarations and Expressions¶

## Program Source Code 2.1, page no: 41¶

In [3]:
from ctypes import c_int,c_uint

SignedInt = c_int(2000000000)

unsignVar = c_uint(2000000000)
SignedResult=c_int(0)
UnsignedInt=c_uint(0)

#calculation result exceeds range of permitted values
SignedResult.value = (SignedInt.value * 2)
SignedResult.value =SignedResult.value / 3

#calculation result is within permissible range
UnsignedResult = (SignedInt.value * 2) / 3;

print "Demonstrates wrong result caused by exceeding range"
print "Signed Integer Calculation giving result as: "
print "(",SignedInt.value,
print "*2)/3= ",SignedResult.value
print "Demonstrates correct result caused by permissible range"
print "Unsigned Integer Calculation giving result as: "
print "(",UnsignedInt.value,
print "*2)/3= ",UnsignedResult

Demonstrates wrong result caused by exceeding range
Signed Integer Calculation giving result as:
( 2000000000 *2)/3=  -98322432
Demonstrates correct result caused by permissible range
Unsigned Integer Calculation giving result as:
( 0 *2)/3=  1333333333


## Program Source Code 2.2, page no: 47¶

In [2]:
#variable declaraton
a=10
b=5
print 'a= ',a,   #printing the values
print ', b= ',b
a=b
print 'a= ',a,
print ', b= ',b
b=7
print 'a= ',a,
print ', b= ',b

a=  10 , b=  5
a=  5 , b=  5
a=  5 , b=  7


## Program Source Code 2.3, page no: 49¶

In [3]:
#The ++ operator is not available in Python
a=10
b=5
c=15
print 'a= ',a,
print ', b= ',b,
print ', c= ',c
a=a+1   #no preincrement syntax in python
c=a+b
b=b+1
print 'a= ',a,
print ', b= ',b,
print ', c= ',c

a=  10 , b=  5 , c=  15
a=  11 , b=  6 , c=  16


## Program Source Code 2.4, page no: 50¶

In [6]:
a=10
b=5
c=15
print 'a= ',a,
print ', b= ',b,
print ', c= ',c  #no preincrement syntax in python
c=a+b
a=a+1
b=b+1
print 'a= ',a,
print ', b= ',b,
print ', c= ',c

a=  10 , b=  5 , c=  15
a=  11 , b=  6 , c=  15


## Program Source Code 2.5, page no: 54¶

In [4]:
from ctypes import c_short,c_long

SignedShort = c_short(30000)

SignedShort.value=SignedShort.value*10
SignedShort.value=SignedShort.value/10
print 'SignedShort= ',SignedShort.value       #wrong answer

SignedShort=30000

#result within limits
SignedLong=(c_long)(SignedShort*10)
SignedLong=(c_long)(SignedShort/10)
print 'SignedShort= ',SignedShort      #right answer

SignedShort=  -2768
SignedShort=  30000

In [ ]: