C Programming Week1
C Programming Week1
Course Outline
• Introduction to Computing
Course Material – SD, SB, PSK, NSN, DK, TAG – CS&E, IIT M 1 2
SD, PSK, NSN, DK, TAG – CS&E, IIT M
SD, PSK, NSN, DK, TAG – CS&E, IIT M 3 SD, PSK, NSN, DK, TAG – CS&E, IIT M 4
SD, PSK, NSN, DK, TAG – CS&E, IIT M 5 SD, PSK, NSN, DK, TAG – CS&E, IIT M 6
1
09/08/17
SD, PSK, NSN, DK, TAG – CS&E, IIT M 11 SD, PSK, NSN, DK, TAG – CS&E, IIT M 12
2
09/08/17
PROCESSOR
MEMORY
Instructions Programs
• Instructions take data stored in variables as • A program is a sequence of instructions
arguments • Normally the processor works as follows,
• Some instructions do some operation on the data – Step A: pick next instruction in the sequence
and store it back in some variable – Step B: get data for the instruction to operate upon
– e.g. The instruction “X X+1” on integer type says – Step C: execute instruction on data (or “jump”)
that “Take the integer stored in X, add 1 to it, and – Step D: store results in designated location (variable)
store it back in (location) X”
– Step E: go to Step A
• Other instructions tell the processor to do
• Such programs are known as imperative
something
programs
– e.g. “jump” to a particular instruction next, or to exit
SD, PSK, NSN, DK, TAG – CS&E, IIT M 17 SD, PSK, NSN, DK, TAG – CS&E, IIT M 18
3
09/08/17
Conclusion Books
• P. Deitel and H. Deitel. C: How to Program.
• Computer is fast but restricted • V. Rajaraman. Computer Programming in C.
• R. G. Dromey. How to Solve It By Computer.
• So we must learn to use its speed • Kernighan and Ritchie. The C Programming
Language.
• And manage its restrictions • Kernighan and Pike. The Unix Programming
Environment.
SD, PSK, NSN, DK, TAG – CS&E, IIT M 23 SD, PSK, NSN, DK, TAG – CS&E, IIT M 24
4
09/08/17
System Bus
SD, PSK, NSN, DK, TAG – CS&E, IIT M 25 SD, PSK, NSN, DK, TAG – CS&E, IIT M 26
More (try more filename on your Unix/Linux machine) Some More (commands are in /bin, /usr/bin. Use ls)
• Memory • System Bus
– Place where information is stored – Essentially a set of wires, used by the other units to
– Primary Memory communicate with each other
• Electronic devices, used primarily for temporary storage – transfers data at a very high rate
• Characterized by their speedy response • ALU – Arithmetic and Logic Unit
– Secondary Memory – Processes data - add, subtract, multiply, …
• Devices for long-term storage
– Decides – after comparing with another value, for
• Contained well tuned mechanical components, magnetic
example
storage media – floppies, hard disks
• Compact Disks use optical technology
SD, PSK, NSN, DK, TAG – CS&E, IIT M 27 SD, PSK, NSN, DK, TAG – CS&E, IIT M 28
Finally (check man cp, man mv, man ls, man k search string) The CPU (editors vi, emacs used to create text)
• Control Unit • Can fetch an instruction from memory
– Controls the interaction among other units • Execute the instruction
– Knows each unit by its name, responds to requests • Store the result in memory
fairly, reacts quickly on certain critical events • A program – a set of instructions
– Gives up control periodically in the interest of the • An instruction has the following structure
system Operation operands destination
• A simple operation
CPU = Control Unit + ALU add a, b Adds the contents of memory locations a and b
and stores the result in location a
SD, PSK, NSN, DK, TAG – CS&E, IIT M 29 SD, PSK, NSN, DK, TAG – CS&E, IIT M 30
5
09/08/17
SD, PSK, NSN, DK, TAG – CS&E, IIT M 33 SD, PSK, NSN, DK, TAG – CS&E, IIT M 34
6
09/08/17
37
NOT WIDELY USED 38
SD, PSK, NSN, DK, TAG – CS&E, IIT M SD, PSK, NSN, DK, TAG – CS&E, IIT M
1 1 1 0 1
Fractional Part (0.11101)2 = + + + + = 0.90625
2 22 23 24 25
SD, PSK, NSN, DK, TAG – CS&E, IIT M 41 SD, PSK, NSN, DK, TAG – CS&E, IIT M 42
7
09/08/17
Decimal Fraction à Binary Fraction (1) Decimal Fraction à Binary Fraction (2)
Convert (0.90625)10 to binary fraction Convert (0.9)10 to binary fraction
0.90625 0.9
×2 0.90625 = ½(1+0.8125) ×2
= ½(1+ ½(1+0.625)) For some fractions, we do
0.8125 + integer part 1 0.8 + integer part 1
×2 = ½(1+ ½(1+ ½(1+0.25))) ×2 not get 0.0 at any stage!
0.625 + integer part 1 = ½(1+½(1+ ½(1+½(0+0.5)))) 0.6 + integer part 1 These fractions require an
×2 = ½(1+½(1+½(1+½(0+½(1+0.0))))) ×2 infinite number of bits!
0.25 + integer part 1 = ½+1/22+1/23+0/24 +1/25 0.2 + integer part 1 Cannot be represented
×2 = (0.11101)2 ×2 exactly!
0.5 + integer part 0 0.4 + integer part 0
×2 ×2
0 + integer part 1 0.8 + integer part 0 Repetition
Thus, (0.90625)10 = (0.11101)2 (0.9)10 = 0.11100110011001100 . . . = 0.11100
SD, PSK, NSN, DK, TAG – CS&E, IIT M 43 SD, PSK, NSN, DK, TAG – CS&E, IIT M 44
= (-1.001111 1100)2 × 25
Decimal Value of stored number (-39.9)10
= (-1. 001111 1100 1100 1100 11001) × 25
23 bit