0% found this document useful (0 votes)
8 views

Week2 and Week3

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Week2 and Week3

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 186

From Turtle to C



• Programming your ”friend”.





• Programming the ”turtle”. Week 1
• Interact with turtle at run time !






From Turtle to C


• Programming your ”friend”.





• Programming the ”turtle”. Week 1
• Interact with turtle at run time !







• First C program.






• Compilation and Execution. 




• The computing machine. 

• Variables and memory. This week



• Input and output in C. 




• Assignment and other operators. 




Lab1 and Survey : Some stats
• Q1: Receive input and turtle draws a
polygon with that many sides
• Q2: Turtle draws a crude pic of a car.
• Q3: Textonly Turtle prints some text.
• (Optional) Q4: Turtle draws a
sophisticated car.
Lab1 and Survey : Some stats
• Q1: Receive input and turtle draws a • # attended: 86.
polygon with that many sides
• # solved Q1: 85.
• Q2: Turtle draws a crude pic of a car.
• # solved Q2: 85.
• Q3: Textonly Turtle prints some text.
• # solved Q3 : 85.
• (Optional) Q4: Turtle draws a
• # solved Q3 : 74.
sophisticated car.
Lab1 and Survey : Some stats
• Q1: Receive input and turtle draws a • # attended: 86.
polygon with that many sides
• # solved Q1: 85.
• Q2: Turtle draws a crude pic of a car.
• # solved Q2: 85.
• Q3: Textonly Turtle prints some text.
• # solved Q3 : 85.
• (Optional) Q4: Turtle draws a
• # solved Q3 : 74.
sophisticated car.

Take-aways from survey and lab:


• About 1 in 3 are totally new to programming but still are
picking up quite fast. Course can challenge them.
Lab1 and Survey : Some stats
• Q1: Receive input and turtle draws a • # attended: 86.
polygon with that many sides
• # solved Q1: 85.
• Q2: Turtle draws a crude pic of a car.
• # solved Q2: 85.
• Q3: Textonly Turtle prints some text.
• # solved Q3 : 85.
• (Optional) Q4: Turtle draws a
• # solved Q3 : 74.
sophisticated car.

Take-aways from survey and lab:


• About 1 in 3 are totally new to programming but still are
picking up quite fast. Course can challenge them.
• Dual approach - we cover everything in class. Common labs
will be set at the limits of those who are new to programming.
At the same time, we will challenge the experienced as extra
questions in the lab/contact hours.
Goals for Today & Tomorrow

End Goals:
• First C program.
• Edit, Compile, Execute – what is that?
• Structure of a C program.
• The printf command.
• syntax and semantics of scanf command.
Goals for Today & Tomorrow

End Goals:
• First C program.
• Edit, Compile, Execute – what is that?
• Structure of a C program.
• The printf command.
• syntax and semantics of scanf command.

Detour:
• Computing on Computers.
• A bird’s eye-view of the computing machine.
• How are numbers represented?
What do we want our C program to achieve?
What do we want our C program to achieve?

********
********
********
********

• Haven’t we already done it?


What do we want our C program to achieve?

********
********
********
********

• Haven’t we already done it? Yes, but not in C.


What do we want our C program to achieve?

********
********
********
********

• Haven’t we already done it? Yes, but not in C.


• What do I need to know to achieve output this in C?
What do we want our C program to achieve?

********
********
********
********

• Haven’t we already done it? Yes, but not in C.


• What do I need to know to achieve output this in C?
• The command for printing in C.
My First C program

#include<stdio.h>
int main() {
printf("********\n");
printf("********\n");
printf("********\n");
printf("********\n");
return 0;
}
My First C program

#include<stdio.h>
int main() {
printf("********\n");
printf("********\n");
printf("********\n");
printf("********\n");
return 0;
}

• stdio.h : standard library of input and output.


• main : a function that every C program must have.
• printf : a useful library function to print several things in C.
My First C program

#include<stdio.h>
int main() {
printf("********\n");
printf("********\n");
printf("********\n");
printf("********\n");
return 0;
}

• stdio.h : standard library of input and output.


• main : a function that every C program must have.
• printf : a useful library function to print several things in C.
To do anything more useful than merely printing we need to have
more operations / commands and storage to store temporary
computations.
Translating High level commands to binary

• C, Turtle, C++ : High level programming languages.


Translating High level commands to binary

• C, Turtle, C++ : High level programming languages.


• Computers don’t understand them.
Translating High level commands to binary

• C, Turtle, C++ : High level programming languages.


• Computers don’t understand them.
A turtle trained computer / C trained computer is slight abuse
Translating High level commands to binary

• C, Turtle, C++ : High level programming languages.


• Computers don’t understand them.
A turtle trained computer / C trained computer is slight abuse
• The computer only understands binary.
Translating High level commands to binary

• C, Turtle, C++ : High level programming languages.


• Computers don’t understand them.
A turtle trained computer / C trained computer is slight abuse
• The computer only understands binary.
• Compiler: Useful tool to convert high-level programs to binary.
Translating High level commands to binary

• C, Turtle, C++ : High level programming languages.


• Computers don’t understand them.
A turtle trained computer / C trained computer is slight abuse
• The computer only understands binary.
• Compiler: Useful tool to convert high-level programs to binary.
• gcc : A compiler for C programs.
• simplecpp / s++ : A compiler for programs using turtle.
• g++: A compiler for C++ programs.
Translating High level commands to binary

• C, Turtle, C++ : High level programming languages.


• Computers don’t understand them.
A turtle trained computer / C trained computer is slight abuse
• The computer only understands binary.
• Compiler: Useful tool to convert high-level programs to binary.
• gcc : A compiler for C programs.
• simplecpp / s++ : A compiler for programs using turtle.
• g++: A compiler for C++ programs.
• When we say a ”C trained computer” we mean we have a
compiler for a C programs.
Compilation process:
Compilation process:

• gcc first.c
Compilation process:

• gcc first.c
• Executable code is created in a file called a.out
Compilation process:

• gcc first.c
• Executable code is created in a file called a.out
• Execute the executable code by typing ./a.out
Things to remember while writing a program

• A computer is very rigid about the rules / syntax.


• You will see compile time errors or warnings.
Things to remember while writing a program

• A computer is very rigid about the rules / syntax.


• You will see compile time errors or warnings.
Try removing a semicolon;
Things to remember while writing a program

• A computer is very rigid about the rules / syntax.


• You will see compile time errors or warnings.
Try removing a semicolon;
Try removing int before main.
Things to remember while writing a program

• A computer is very rigid about the rules / syntax.


• You will see compile time errors or warnings.
Try removing a semicolon;
Try removing int before main.
• However it is accomodative of spaces.
Things to remember while writing a program

• A computer is very rigid about the rules / syntax.


• You will see compile time errors or warnings.
Try removing a semicolon;
Try removing int before main.
• However it is accomodative of spaces. Yet it is in our interest
to write readable / indented code.
Things to remember while writing a program

• A computer is very rigid about the rules / syntax.


• You will see compile time errors or warnings.
Try removing a semicolon;
Try removing int before main.
• However it is accomodative of spaces. Yet it is in our interest
to write readable / indented code.

Aim of the lab today : to experiment with this !!


Compilation process: Igniting curiosity
Compilation process: Igniting curiosity

• Compiler itself if a program !.


Compilation process: Igniting curiosity

• Compiler itself if a program !.


• Yes, a program can take another program as input and and
process it.
Compilation process: Igniting curiosity

• Compiler itself if a program !.


• Yes, a program can take another program as input and and
process it. (a revolutionary idea).
Compilation process: Igniting curiosity

• Compiler itself if a program !.


• Yes, a program can take another program as input and and
process it. (a revolutionary idea).
• In what language is a compiler written? Who compiles the
compiler? Who executes the compiler?
Compilation process: Igniting curiosity

• Compiler itself if a program !.


• Yes, a program can take another program as input and and
process it. (a revolutionary idea).
• In what language is a compiler written? Who compiles the
compiler? Who executes the compiler? We need to
understand the process better
Execution process: A few general ideas.
Execution process: A few general ideas.
• Ask yourself : How would your friend
execute your instructions?
Execution process: A few general ideas.
• Ask yourself : How would your friend
execute your instructions? Line by line.
Execution process: A few general ideas.
• Ask yourself : How would your friend
execute your instructions? Line by line.
• Control is at statement w : Computer
is currently executing statement w .
• Control flow : The order in which
statements get executed. Execution
starts at top and goes down.
Execution process: A few general ideas.
• Ask yourself : How would your friend
execute your instructions? Line by line.
• Control is at statement w : Computer
is currently executing statement w .
• Control flow : The order in which
statements get executed. Execution
starts at top and goes down. Retraced
if there is a repeat statement.
Execution process: A few general ideas.
• Ask yourself : How would your friend
execute your instructions? Line by line.
• Control is at statement w : Computer
is currently executing statement w .
• Control flow : The order in which
statements get executed. Execution
starts at top and goes down. Retraced
if there is a repeat statement.
• Variable used for storing data.
Execution process: A few general ideas.
• Ask yourself : How would your friend
execute your instructions? Line by line.
• Control is at statement w : Computer
is currently executing statement w .
• Control flow : The order in which
statements get executed. Execution
starts at top and goes down. Retraced
if there is a repeat statement.
• Variable used for storing data.
• Computer memory : blackboard.
Execution process: A few general ideas.
• Ask yourself : How would your friend
execute your instructions? Line by line.
• Control is at statement w : Computer
is currently executing statement w .
• Control flow : The order in which
statements get executed. Execution
starts at top and goes down. Retraced
if there is a repeat statement.
• Variable used for storing data.
• Computer memory : blackboard.
• Variable : Region on the board in which you can write a value.
• Variables have names, e.g. nsides. We can use the name to
refer to the value written in the variable. Details later.
The Computing Machine : von Neuman Architecture
The Computing Machine

• Two key parts : processor and a memory.


The Computing Machine

• Two key parts : processor and a memory.


• The memory can be thought of as a series of locations to
store information.
• Each memory location has an ”address”.
A brief look into the history...
From Abacus to Apple

• Counting frame.
• One of the earliest form
of calculator.
• Still used by kids to do
fast simple arithmetic.
From Abacus to Apple

• Counting frame.
• One of the earliest form
of calculator.
• Still used by kids to do
fast simple arithmetic.

• Followed by mechanical calculators by B. Pascal (1642),


G. W. Leibniz (1671).
• Used cogs / interlocking gears.

• Performed +, −, ∗, / and x.
• Leibniz is credited of creating the binary system.
Jaquard looms (1804)
Charles Babbage (1791–1871)

• Regarded as the “Father


of Computer”.
• Conceived of a machine
that has all the parts of a
modern computer, input,
a memory, a processor,
and an output (1850).
Difference Engine (1850)

Difference engine built from Babbage’s design


(London Science Museum).
Ada Lovelace (1815–1852)
Ada Lovelace (1815–1852)

• “Wrote” the description


of the mechanical
computer of Babbage.
• Regarded as the first
programmer ever.
• The programming
language ADA is named
after her.
Alan Turing (1912 – 1954)
Alan Turing (1912 – 1954)

• Father of Theoretical
Computer Science (TCS)
and Artificial Intelligence
(AI).
• Turing machine – a
model for a general
purpose computer.
• Turing test – how
intelligent is a machine?
First Electronic Computer : ENIAC 1946

Electronic Numerical Integerator


And Calculator.
First Electronic Computer : ENIAC 1946

• 50,000 vacuum
tubes, diodes,
relays, resistors,
capacitors.
• 5 million
hand-soldered
joints.
• Weighed 27 tons.
• Covered 167m2
area.
• Consumed 150
Electronic Numerical Integerator kW of power.
And Calculator.
1946 – 1976

Integrated Circuits
Transistors
1946 – 1976

Integrated Circuits
Transistors

Apple Macintosh
Today’s World : Core i7 Processor

2008-15: Intel Core i7 Processor


Clock speed: > 2.5 GHz
No. of Transistors: 0.731 − 1.3B
Technology: 45 − 22nm CMOS Area: 263 − 181mm2 .
Modern computing devices
Data Centers: Processing/Storing Huge volume of data
Even Cooling them is a big deal ...
What do we learn from History?

• Untiring efforts by several pioneers (Babbage, Turing, Von


Neuman, George Boole, Steve Jobs and many unnamed
computer scientists and innovators) have given us the
technology that we have today.
• Several failed attempts and received criticisms for path
breaking ideas.
• Most of the advances were made by necessity and driven by
passion and immense hard work.
The Computing Machine : von Neuman Architecture
The Computing Machine

• A program is a sequence of instructions assembled for some


given task.
• Most instructions operate on data.
• Some instructions control the flow of the operations.
The Computing Machine

• A program is a sequence of instructions assembled for some


given task.
• Most instructions operate on data.
• Some instructions control the flow of the operations.
The Computing Machine

• All information - Numbers, text, graphics and images, video,


audio, program instructions, are all stored in the memory.
How does the computer represent data?

• To store : Numbers, text, graphics and images, video, audio,


program instructions.
How does the computer represent data?

• To store : Numbers, text, graphics and images, video, audio,


program instructions.
• In some way, all information is digitized - broken down into
pieces and represented as numbers.
How does the computer represent data?

• To store : Numbers, text, graphics and images, video, audio,


program instructions.
• In some way, all information is digitized - broken down into
pieces and represented as numbers.
• Example : Representing Text Digitally.
How does the computer represent data?

• To store : Numbers, text, graphics and images, video, audio,


program instructions.
• In some way, all information is digitized - broken down into
pieces and represented as numbers.
• Example : Representing Text Digitally.
• Every character is stored as a number, including spaces, digits,
and punctuation.
How does the computer represent data?

• To store : Numbers, text, graphics and images, video, audio,


program instructions.
• In some way, all information is digitized - broken down into
pieces and represented as numbers.
• Example : Representing Text Digitally.
• Every character is stored as a number, including spaces, digits,
and punctuation.
• Corresponding upper and lower case letters are separate
characters.
The ASCII table
American Standard Code for Information Interchange (ASCII).
The ASCII table
American Standard Code for Information Interchange (ASCII).
But, how does number get stored?

Number Systems.
• Decimal (base 10 - uses 10 symbols {0 . . . 9}. Eg : 0, 1, 2, 3,
4, 5, 6, 7, 8, 9, 10, 11, 12, 13 . . ..
But, how does number get stored?

Number Systems.
• Decimal (base 10 - uses 10 symbols {0 . . . 9}. Eg : 0, 1, 2, 3,
4, 5, 6, 7, 8, 9, 10, 11, 12, 13 . . ..
• Unary (base 1 - uses 1 symbol)
Eg : 1, 11, 111, 1111, . . ..
But, how does number get stored?

Number Systems.
• Decimal (base 10 - uses 10 symbols {0 . . . 9}. Eg : 0, 1, 2, 3,
4, 5, 6, 7, 8, 9, 10, 11, 12, 13 . . ..
• Unary (base 1 - uses 1 symbol)
Eg : 1, 11, 111, 1111, . . ..
• Binary (base 2) – uses 2 symbols {0,1})
Eg : 0, 1, 10, 11, 100, 101, 110, 111, 1000, 1001, 1010 . . .
But, how does number get stored?

Number Systems.
• Decimal (base 10 - uses 10 symbols {0 . . . 9}. Eg : 0, 1, 2, 3,
4, 5, 6, 7, 8, 9, 10, 11, 12, 13 . . ..
• Unary (base 1 - uses 1 symbol)
Eg : 1, 11, 111, 1111, . . ..
• Binary (base 2) – uses 2 symbols {0,1})
Eg : 0, 1, 10, 11, 100, 101, 110, 111, 1000, 1001, 1010 . . .
• Octal (base 8 – uses 8 symbols {0 . . . 7})
Eg : 0, 1, 2, 3, 4, 5, 6, 7, 10, 11, 12, 13 . . .
But, how does number get stored?

Number Systems.
• Decimal (base 10 - uses 10 symbols {0 . . . 9}. Eg : 0, 1, 2, 3,
4, 5, 6, 7, 8, 9, 10, 11, 12, 13 . . ..
• Unary (base 1 - uses 1 symbol)
Eg : 1, 11, 111, 1111, . . ..
• Binary (base 2) – uses 2 symbols {0,1})
Eg : 0, 1, 10, 11, 100, 101, 110, 111, 1000, 1001, 1010 . . .
• Octal (base 8 – uses 8 symbols {0 . . . 7})
Eg : 0, 1, 2, 3, 4, 5, 6, 7, 10, 11, 12, 13 . . .
• Hexadecimal(base 16 – uses A-F for 10-15)
Eg : 0, 1, ..., 9, A, B, C, D, E, F, 10, 11, ... 19, 1A, 1B, ...
But, how does number get stored?

Number Systems.
• Decimal (base 10 - uses 10 symbols {0 . . . 9}. Eg : 0, 1, 2, 3,
4, 5, 6, 7, 8, 9, 10, 11, 12, 13 . . ..
• Unary (base 1 - uses 1 symbol)
Eg : 1, 11, 111, 1111, . . ..
• Binary (base 2) – uses 2 symbols {0,1})
Eg : 0, 1, 10, 11, 100, 101, 110, 111, 1000, 1001, 1010 . . .
• Octal (base 8 – uses 8 symbols {0 . . . 7})
Eg : 0, 1, 2, 3, 4, 5, 6, 7, 10, 11, 12, 13 . . .
• Hexadecimal(base 16 – uses A-F for 10-15)
Eg : 0, 1, ..., 9, A, B, C, D, E, F, 10, 11, ... 19, 1A, 1B, ...
Goals for this week:

End Goals:
• First C program.
• Edit, Compile, Execute – what is that?
• Structure of a C program.
• The printf command.
• The scanf command.
• Operators in C .
• Assignment statement.
Goals for this week:

End Goals:
• First C program.
• Edit, Compile, Execute – what is that?
• Structure of a C program.
• The printf command.
• The scanf command.
• Operators in C .
• Assignment statement.

Detour:
• History - from Abacus to Apple
• A bird’s eye-view of the computing machine.
• How are numbers represented?
But, how does number get stored?

Number Systems.
• Decimal (base 10 - uses 10 symbols {0 . . . 9}. Eg : 0, 1, 2, 3,
4, 5, 6, 7, 8, 9, 10, 11, 12, 13 . . ..
But, how does number get stored?

Number Systems.
• Decimal (base 10 - uses 10 symbols {0 . . . 9}. Eg : 0, 1, 2, 3,
4, 5, 6, 7, 8, 9, 10, 11, 12, 13 . . ..
• Unary (base 1 - uses 1 symbol)
Eg : 1, 11, 111, 1111, . . ..
But, how does number get stored?

Number Systems.
• Decimal (base 10 - uses 10 symbols {0 . . . 9}. Eg : 0, 1, 2, 3,
4, 5, 6, 7, 8, 9, 10, 11, 12, 13 . . ..
• Unary (base 1 - uses 1 symbol)
Eg : 1, 11, 111, 1111, . . ..
• Binary (base 2) – uses 2 symbols {0,1})
Eg : 0, 1, 10, 11, 100, 101, 110, 111, 1000, 1001, 1010 . . .
But, how does number get stored?

Number Systems.
• Decimal (base 10 - uses 10 symbols {0 . . . 9}. Eg : 0, 1, 2, 3,
4, 5, 6, 7, 8, 9, 10, 11, 12, 13 . . ..
• Unary (base 1 - uses 1 symbol)
Eg : 1, 11, 111, 1111, . . ..
• Binary (base 2) – uses 2 symbols {0,1})
Eg : 0, 1, 10, 11, 100, 101, 110, 111, 1000, 1001, 1010 . . .
• Octal (base 8 – uses 8 symbols {0 . . . 7})
Eg : 0, 1, 2, 3, 4, 5, 6, 7, 10, 11, 12, 13 . . .
But, how does number get stored?

Number Systems.
• Decimal (base 10 - uses 10 symbols {0 . . . 9}. Eg : 0, 1, 2, 3,
4, 5, 6, 7, 8, 9, 10, 11, 12, 13 . . ..
• Unary (base 1 - uses 1 symbol)
Eg : 1, 11, 111, 1111, . . ..
• Binary (base 2) – uses 2 symbols {0,1})
Eg : 0, 1, 10, 11, 100, 101, 110, 111, 1000, 1001, 1010 . . .
• Octal (base 8 – uses 8 symbols {0 . . . 7})
Eg : 0, 1, 2, 3, 4, 5, 6, 7, 10, 11, 12, 13 . . .
• Hexadecimal(base 16 – uses A-F for 10-15)
Eg : 0, 1, ..., 9, A, B, C, D, E, F, 10, 11, ... 19, 1A, 1B, ...
But, how does number get stored?

Number Systems.
• Decimal (base 10 - uses 10 symbols {0 . . . 9}. Eg : 0, 1, 2, 3,
4, 5, 6, 7, 8, 9, 10, 11, 12, 13 . . ..
• Unary (base 1 - uses 1 symbol)
Eg : 1, 11, 111, 1111, . . ..
• Binary (base 2) – uses 2 symbols {0,1})
Eg : 0, 1, 10, 11, 100, 101, 110, 111, 1000, 1001, 1010 . . .
• Octal (base 8 – uses 8 symbols {0 . . . 7})
Eg : 0, 1, 2, 3, 4, 5, 6, 7, 10, 11, 12, 13 . . .
• Hexadecimal(base 16 – uses A-F for 10-15)
Eg : 0, 1, ..., 9, A, B, C, D, E, F, 10, 11, ... 19, 1A, 1B, ...
Quick Primer on Number System : Base n

Take every ”digit” and multiply by increasing powers of n and add.


Quick Primer on Number System : Base n

Take every ”digit” and multiply by increasing powers of n and add.


Converting from Decimal to Binary

Conver the decimal number 39 to binary (base 2).


Which Number System? Binary !

• Devices that store and process information are cheaper and


more reliable if they have to represent only two states.
Which Number System? Binary !

• Devices that store and process information are cheaper and


more reliable if they have to represent only two states.
• A single bit can represent two possible states, like a light bulb
that is either on (1) or off (0). Hence representable by even
voltage levels in wires.
Which Number System? Binary !

• Devices that store and process information are cheaper and


more reliable if they have to represent only two states.
• A single bit can represent two possible states, like a light bulb
that is either on (1) or off (0). Hence representable by even
voltage levels in wires.
• The other number systems can be ”encoded in” binary
Which Number System? Binary !

• Devices that store and process information are cheaper and


more reliable if they have to represent only two states.
• A single bit can represent two possible states, like a light bulb
that is either on (1) or off (0). Hence representable by even
voltage levels in wires.
• The other number systems can be ”encoded in” binary
Representing values in Binary

If we have m bits, we can represent 2m unique different values.


Representing values in Binary

If we have m bits, we can represent 2m unique different values.


A useful circle :
Representing negative numbers
Representing negative numbers

Sign Magnitude notation


• Use one bit for sign, others for magnitude of the number.
Representing negative numbers

Sign Magnitude notation


• Use one bit for sign, others for magnitude of the number.

Sign Magn.
0 0 0 0
0 0 1 +1
0 1 0 +2
0 1 1 +3
1 0 0 0
1 0 1 -1
1 1 0 -2
1 1 1 -3
Representing negative numbers

Sign Magnitude notation


• Use one bit for sign, others for magnitude of the number.

Sign Magn.
0 0 0 0
0 0 1 +1
0 1 0 +2
0 1 1 +3
1 0 0 0
1 0 1 -1
1 1 0 -2
1 1 1 -3

• using n bits: −(2n−1 − 1) . . . (2n−1 − 1).


• zero has two representations.
Representing negative numbers
Ones complement notation
• for a negative number n, represent the number by the bit
complement of its binary rep. using k bits.
Representing negative numbers
Ones complement notation
• for a negative number n, represent the number by the bit
complement of its binary rep. using k bits.
Sign Magn. Ones comp.
0 0 0 0 0
0 0 1 +1 +1
0 1 0 +2 +2
0 1 1 +3 +3
1 0 0 0 -3
1 0 1 -1 -2
1 1 0 -2 -1
1 1 1 -3 0
Representing negative numbers
Ones complement notation
• for a negative number n, represent the number by the bit
complement of its binary rep. using k bits.
Sign Magn. Ones comp.
0 0 0 0 0
0 0 1 +1 +1
0 1 0 +2 +2
0 1 1 +3 +3
1 0 0 0 -3
1 0 1 -1 -2
1 1 0 -2 -1
1 1 1 -3 0
• using n bits: −(2n−1 − 1) . . . (2n−1 − 1).
• zero has two representations.
• not very widely used representation.
Representing negative numbers - A neat trick
Representing negative numbers - A neat trick
Representing negative numbers - A neat trick
Representing negative numbers - A neat trick

Twos complement notation


• for a negative number n, represent the number as 2k − n.
Representing negative numbers - A neat trick

Twos complement notation


• for a negative number n, represent the number as 2k − n.

Sign Magn. Ones comp. Twos comp.


0 0 0 0 0 0
0 0 1 +1 +1 +1
0 1 0 +2 +2 +2
0 1 1 +3 +3 +3
1 0 0 0 -3 -4
1 0 1 -1 -2 -3
1 1 0 -2 -1 -2
1 1 1 -3 0 -1
Representing negative numbers - A neat trick

Twos complement notation


• for a negative number n, represent the number as 2k − n.

Sign Magn. Ones comp. Twos comp.


0 0 0 0 0 0
0 0 1 +1 +1 +1
0 1 0 +2 +2 +2
0 1 1 +3 +3 +3
1 0 0 0 -3 -4
1 0 1 -1 -2 -3
1 1 0 -2 -1 -2
1 1 1 -3 0 -1

• using n bits: −(2n−1 ) . . . (2n−1 − 1).


• widely used representation.
Representing negative numbers

Arithmetic with these representations

Sign Magn. Ones comp. Twos comp.


0 0 0 0 0 0
0 0 1 +1 +1 +1
0 1 0 +2 +2 +2
0 1 1 +3 +3 +3
1 0 0 0 -3 -4
1 0 1 -1 -2 -3
1 1 0 -2 -1 -2
1 1 1 -3 0 -1
Representing negative numbers

Arithmetic with these representations

Sign Magn. Ones comp. Twos comp.


0 0 0 0 0 0
0 0 1 +1 +1 +1
0 1 0 +2 +2 +2
0 1 1 +3 +3 +3
1 0 0 0 -3 -4
1 0 1 -1 -2 -3
1 1 0 -2 -1 -2
1 1 1 -3 0 -1

• 2 + (−3)
Representing negative numbers

Arithmetic with these representations

Sign Magn. Ones comp. Twos comp.


0 0 0 0 0 0
0 0 1 +1 +1 +1
0 1 0 +2 +2 +2
0 1 1 +3 +3 +3
1 0 0 0 -3 -4
1 0 1 -1 -2 -3
1 1 0 -2 -1 -2
1 1 1 -3 0 -1

• 2 + (−3)
• 3 + (−2)
More examples : The case of 4 bits
More examples : The case of 4 bits
More examples : The case of 4 bits

Overflow Detection Rule : If two numbers with the same sign


(both positive or both negative) are added, then overflow occurs if
and only if the result has the opposite sign.
What do we choose ...

• 2s complement form is suitable in terms of efficiency of


manipulation circuitary.
• 2s has a unique representation for 0.
• With n bits :
• Sign-Magnitude form represents −2n−1 − 1 to 2n−1 − 1.
• 1s complement form represents −2n−1 − 1 to 2n−1 − 1.
• 2s complement form represents −2n−1 to 2n−1 − 1.
Example: Suppose we decide that an integer is going to be stored
as 32 bits. What is the range of values that we can store there
without overflow.
What do we choose ...

• 2s complement form is suitable in terms of efficiency of


manipulation circuitary.
• 2s has a unique representation for 0.
• With n bits :
• Sign-Magnitude form represents −2n−1 − 1 to 2n−1 − 1.
• 1s complement form represents −2n−1 − 1 to 2n−1 − 1.
• 2s complement form represents −2n−1 to 2n−1 − 1.
Example: Suppose we decide that an integer is going to be stored
as 32 bits. What is the range of values that we can store there
without overflow. −231 to 231 − 1.

−2147483648 to 2147483647
Take Aways so far ...

• Compilation and Execution.


• Programs themselves are converted to binary by a compiler
before they can be executed!
• Both data and programs are both represented as binary.
• Learned about and from history.
• To solve anything on a computer – we need to represent it as
a number. Numbers themselves are represented using some
form of binary.
• Learned how exactly these numbers are represented.
Take Aways so far ...

• Compilation and Execution.


• Programs themselves are converted to binary by a compiler
before they can be executed!
• Both data and programs are both represented as binary.
• Learned about and from history.
• To solve anything on a computer – we need to represent it as
a number. Numbers themselves are represented using some
form of binary.
• Learned how exactly these numbers are represented.
• The representation form is chosen for efficient manipulation.
Recall our First C program

#include <stdio.h>

/* My first C program */

main() {
printf("Hello World!\n");
}
Recall our First C program

#include <stdio.h>

/* My first C program */

main() {
printf("Hello World!\n");
}

• stdio.h : standard library of input and output.


• main : a function that every C program must have.
• printf : a useful library function to print several things in C.
Recall our First C program

#include <stdio.h>

/* My first C program */

main() {
printf("Hello World!\n");
}

• stdio.h : standard library of input and output.


• main : a function that every C program must have.
• printf : a useful library function to print several things in C.
To do anything more useful than merely printing we need to have
more operations / commands and storage to store temporary
computations.
Variables in C
Variable : A symbol (representing a memory location) whose value
the program do not know at programming time, but will come to
know only when running the program.
Variables in C
Variable : A symbol (representing a memory location) whose value
the program do not know at programming time, but will come to
know only when running the program.
Example Task: Add given two numbers x and y and print the value
to the user. The two numbers are given at run time.
The program should:
• Define two variables x and y . Let the computer know that we
plan to use to ”variables” which are expected to take this type
of data.
• What type of values can x and y take?
Variables in C
Variable : A symbol (representing a memory location) whose value
the program do not know at programming time, but will come to
know only when running the program.
Example Task: Add given two numbers x and y and print the value
to the user. The two numbers are given at run time.
The program should:
• Define two variables x and y . Let the computer know that we
plan to use to ”variables” which are expected to take this type
of data.
• What type of values can x and y take?
integer, positive integers, fractions?
Variables in C
Variable : A symbol (representing a memory location) whose value
the program do not know at programming time, but will come to
know only when running the program.
Example Task: Add given two numbers x and y and print the value
to the user. The two numbers are given at run time.
The program should:
• Define two variables x and y . Let the computer know that we
plan to use to ”variables” which are expected to take this type
of data.
• What type of values can x and y take?
integer, positive integers, fractions?
• What do x and y represent?
Variables in C
Variable : A symbol (representing a memory location) whose value
the program do not know at programming time, but will come to
know only when running the program.
Example Task: Add given two numbers x and y and print the value
to the user. The two numbers are given at run time.
The program should:
• Define two variables x and y . Let the computer know that we
plan to use to ”variables” which are expected to take this type
of data.
• What type of values can x and y take?
integer, positive integers, fractions?
• What do x and y represent?
Say marks in Maths and marks in Physics respectively.
Variables in C
Variable : A symbol (representing a memory location) whose value
the program do not know at programming time, but will come to
know only when running the program.
Example Task: Add given two numbers x and y and print the value
to the user. The two numbers are given at run time.
The program should:
• Define two variables x and y . Let the computer know that we
plan to use to ”variables” which are expected to take this type
of data.
• What type of values can x and y take?
integer, positive integers, fractions?
• What do x and y represent?
Say marks in Maths and marks in Physics respectively.

• Use the + operator defined to sum up the values of x and y .


• We should print using printf command.
Sum of 2 numbers

#include <stdio.h>

/* sum 2 integers */

main() {
int x;
int y;

printf("%d\n", x+y);
}
Sum of 2 numbers

• int : defines that x, y are


of type integers.
#include <stdio.h> • x+y : evaluates x+y and
asks printf commands
/* sum 2 integers */ to display that result.

main() {
int x;
int y;

printf("%d\n", x+y);
}
Sum of 2 numbers

• int : defines that x, y are


of type integers.
#include <stdio.h> • x+y : evaluates x+y and
asks printf commands
/* sum 2 integers */ to display that result.
• printf command
main() {
replaces the %d in the
int x;
”format string” with the
int y;
value of x+y and diplays
the resulting.
printf("%d\n", x+y);
}
Sum of 2 numbers

• int : defines that x, y are


of type integers.
#include <stdio.h> • x+y : evaluates x+y and
asks printf commands
/* sum 2 integers */ to display that result.
• printf command
main() {
replaces the %d in the
int x;
”format string” with the
int y;
value of x+y and diplays
the resulting.
printf("%d\n", x+y);
} • What will be the output?
Sum of 2 numbers

• int : defines that x, y are


of type integers.
#include <stdio.h> • x+y : evaluates x+y and
asks printf commands
/* sum 2 integers */ to display that result.
• printf command
main() {
replaces the %d in the
int x;
”format string” with the
int y;
value of x+y and diplays
the resulting.
printf("%d\n", x+y);
} • What will be the output?
• Initialization or reading
of x and y missing.
Sum of 2 numbers

• int : defines that x, y are


of type integers.
#include <stdio.h> • x+y : evaluates x+y and
asks printf commands
/* sum 2 integers */ to display that result.
• printf command
main() {
replaces the %d in the
int x;
”format string” with the
int y;
value of x+y and diplays
the resulting.
printf("%d\n", x+y);
} • What will be the output?
• Initialization or reading
of x and y missing.
Sum of 2 numbers – with initialization

• int : defines that x, y are of


#include <stdio.h> type integers and initializes
them to values 98 and 99
/* sum 2 integers */ respectively.
• x+y : evaluates x+y and
main() { asks printf commands to
int x = 98; display that result.
int y = 99;

printf("%d\n", x+y);
}
Sum of 2 numbers – with initialization

• int : defines that x, y are of


#include <stdio.h> type integers and initializes
them to values 98 and 99
/* sum 2 integers */ respectively.
• x+y : evaluates x+y and
main() { asks printf commands to
int x = 98; display that result.
int y = 99; • printf command replaces
the %d in the ”format string”
printf("%d\n", x+y); with the value of x+y and
} diplays the resulting.
Sum of 2 numbers – with initialization

• int : defines that x, y are of


#include <stdio.h> type integers and initializes
them to values 98 and 99
/* sum 2 integers */ respectively.
• x+y : evaluates x+y and
main() { asks printf commands to
int x = 98; display that result.
int y = 99; • printf command replaces
the %d in the ”format string”
printf("%d\n", x+y); with the value of x+y and
} diplays the resulting.
• What will be the output?
Sum of 2 numbers – with initialization

• int : defines that x, y are of


#include <stdio.h> type integers and initializes
them to values 98 and 99
/* sum 2 integers */ respectively.
• x+y : evaluates x+y and
main() { asks printf commands to
int x = 98; display that result.
int y = 99; • printf command replaces
the %d in the ”format string”
printf("%d\n", x+y); with the value of x+y and
} diplays the resulting.
• What will be the output?
Reading the input from the user scanf
#include <stdio.h>

/* sum 2 integers */ • The printf prompts the


user.
main() {
• scanf waits for user input.
int x;
int y; • scanf interprets user input
as per format.
printf("Enter x: "); • scanf stores the value read
scanf("%d",&x); in the given ”address”.
• The variable and the address
printf("Enter y: "); has to be declared before
scanf("%d",&y); giving the address to scanf.

printf("%d\n", x+y);
}
Can we handle ”characters” instead of ”integers”

Problem: Read a character from user and print it two times.


#include <stdio.h>
• declaration - reserves 8 bits
/* printing characters */ of memory for x.
• The first printf prompts
main() { and scanf waits.
char x; • scanf stores the value read
in the given ”address”.
printf("Enter char: ");
• printf prints the value
scanf("%c",&x);
replacing %c with the
corresponding character.
printf("%c-%c",x,x);
}
Can we handle ”characters” instead of ”integers”

Problem: Read a character from user and print it two times.


#include <stdio.h>
• declaration - reserves 8 bits
/* printing characters */ of memory for x.
• The first printf prompts
main() { and scanf waits.
char x; • scanf stores the value read
in the given ”address”.
printf("Enter char: ");
• printf prints the value
scanf("%c",&x);
replacing %c with the
corresponding character.
printf("%c-%c",x,x);
}
Mischief : Let us print a character with %d.
Input and Output statements: printf and scanf

printf(format-string, var1, var2, ... , var3);


• printf allows multiple inputs in a single statement.
• Format string determines how the output is printed.
Input and Output statements: printf and scanf

printf(format-string, var1, var2, ... , var3);


• printf allows multiple inputs in a single statement.
• Format string determines how the output is printed.

scanf(format-string, &var1, &var2, ... , &var3);


• scanf allows multiple inputs in a single statement.
• Format string determines how the input is interpreted.
• Notice the & preceeding the variables.
Multiple input scanf and printf

#include <stdio.h>

/* sum 2 integers */

main() {
int x;
int y;

printf("Enter x y: ");
scanf("%d %d",&x,&y);

printf("%d %d\n", x+y,x-y);


}
Arithmetic Operators in C

• Addition, Subtraction, Multiplication, Division (+, −, ∗, /)


Arithmetic Operators in C

• Addition, Subtraction, Multiplication, Division (+, −, ∗, /)


Some issues to be aware of :
Arithmetic Operators in C

• Addition, Subtraction, Multiplication, Division (+, −, ∗, /)


Some issues to be aware of :
Arithmetic Operators in C

• Addition, Subtraction, Multiplication, Division (+, −, ∗, /)


Some issues to be aware of :
• Attempted division by 0 will be complained about at run time.
Experiment this.
Arithmetic Operators in C

• Addition, Subtraction, Multiplication, Division (+, −, ∗, /)


Some issues to be aware of :
• Attempted division by 0 will be complained about at run time.
Experiment this.
• Integer division will give integer part of the result only.
Arithmetic Operators in C

• Addition, Subtraction, Multiplication, Division (+, −, ∗, /)


Some issues to be aware of :
• Attempted division by 0 will be complained about at run time.
Experiment this.
• Integer division will give integer part of the result only.
• What will x − y − z compute?
Arithmetic Operators in C

• Addition, Subtraction, Multiplication, Division (+, −, ∗, /)


Some issues to be aware of :
• Attempted division by 0 will be complained about at run time.
Experiment this.
• Integer division will give integer part of the result only.
• What will x − y − z compute? Operator precedence - left to
right order. Thus x − y − z is same as (x − y ) − z.
Arithmetic Operators in C

• Addition, Subtraction, Multiplication, Division (+, −, ∗, /)


Some issues to be aware of :
• Attempted division by 0 will be complained about at run time.
Experiment this.
• Integer division will give integer part of the result only.
• What will x − y − z compute? Operator precedence - left to
right order. Thus x − y − z is same as (x − y ) − z.
• What about x + y − z?
Arithmetic Operators in C

• Addition, Subtraction, Multiplication, Division (+, −, ∗, /)


Some issues to be aware of :
• Attempted division by 0 will be complained about at run time.
Experiment this.
• Integer division will give integer part of the result only.
• What will x − y − z compute? Operator precedence - left to
right order. Thus x − y − z is same as (x − y ) − z.
• What about x + y − z? Left to right order for same group
operators. + and − are of the same group. Thus, it will be
(x + y ) − z.
Arithmetic Operators in C

• Addition, Subtraction, Multiplication, Division (+, −, ∗, /)


Some issues to be aware of :
• Attempted division by 0 will be complained about at run time.
Experiment this.
• Integer division will give integer part of the result only.
• What will x − y − z compute? Operator precedence - left to
right order. Thus x − y − z is same as (x − y ) − z.
• What about x + y − z? Left to right order for same group
operators. + and − are of the same group. Thus, it will be
(x + y ) − z.
• Multiplication and division gets higher precedence.
Example x − y ∗ z. It will be x − (y ∗ z).
Arithmetic Operators in C

• Addition, Subtraction, Multiplication, Division (+, −, ∗, /)


Some issues to be aware of :
• Attempted division by 0 will be complained about at run time.
Experiment this.
• Integer division will give integer part of the result only.
• What will x − y − z compute? Operator precedence - left to
right order. Thus x − y − z is same as (x − y ) − z.
• What about x + y − z? Left to right order for same group
operators. + and − are of the same group. Thus, it will be
(x + y ) − z.
• Multiplication and division gets higher precedence.
Example x − y ∗ z. It will be x − (y ∗ z).
Example : x ∗ y /z?
Arithmetic Operators in C

• Addition, Subtraction, Multiplication, Division (+, −, ∗, /)


Some issues to be aware of :
• Attempted division by 0 will be complained about at run time.
Experiment this.
• Integer division will give integer part of the result only.
• What will x − y − z compute? Operator precedence - left to
right order. Thus x − y − z is same as (x − y ) − z.
• What about x + y − z? Left to right order for same group
operators. + and − are of the same group. Thus, it will be
(x + y ) − z.
• Multiplication and division gets higher precedence.
Example x − y ∗ z. It will be x − (y ∗ z).
Example : x ∗ y /z? (x ∗ y )/z.
Arithmetic Operators in C

• Addition, Subtraction, Multiplication, Division (+, −, ∗, /)


Some issues to be aware of :
• Attempted division by 0 will be complained about at run time.
Experiment this.
• Integer division will give integer part of the result only.
• What will x − y − z compute? Operator precedence - left to
right order. Thus x − y − z is same as (x − y ) − z.
• What about x + y − z? Left to right order for same group
operators. + and − are of the same group. Thus, it will be
(x + y ) − z.
• Multiplication and division gets higher precedence.
Example x − y ∗ z. It will be x − (y ∗ z).
Example : x ∗ y /z? (x ∗ y )/z.
• Modulus operator: %
x % y : remainder when x is divided by y .
Arithmetic Operators in C

• Addition, Subtraction, Multiplication, Division (+, −, ∗, /)


Some issues to be aware of :
• Attempted division by 0 will be complained about at run time.
Experiment this.
• Integer division will give integer part of the result only.
• What will x − y − z compute? Operator precedence - left to
right order. Thus x − y − z is same as (x − y ) − z.
• What about x + y − z? Left to right order for same group
operators. + and − are of the same group. Thus, it will be
(x + y ) − z.
• Multiplication and division gets higher precedence.
Example x − y ∗ z. It will be x − (y ∗ z).
Example : x ∗ y /z? (x ∗ y )/z.
• Modulus operator: %
x % y : remainder when x is divided by y .
Quick Problem to Solve :

Extracting the last two digits of a given integer:


Quick Problem to Solve :

Extracting the last two digits of a given integer:


Developing the idea:
• Let us assume n is an integer variable which is declared.
• Assume that we have read the input from the user to the
variable n.
Quick Problem to Solve :

Extracting the last two digits of a given integer:


Developing the idea:
• Let us assume n is an integer variable which is declared.
• Assume that we have read the input from the user to the
variable n.
• How will you extract the right most digit.
Quick Problem to Solve :

Extracting the last two digits of a given integer:


Developing the idea:
• Let us assume n is an integer variable which is declared.
• Assume that we have read the input from the user to the
variable n.
• How will you extract the right most digit. n%10.
Quick Problem to Solve :

Extracting the last two digits of a given integer:


Developing the idea:
• Let us assume n is an integer variable which is declared.
• Assume that we have read the input from the user to the
variable n.
• How will you extract the right most digit. n%10.
• How will you extract the second last digit?
Quick Problem to Solve :

Extracting the last two digits of a given integer:


Developing the idea:
• Let us assume n is an integer variable which is declared.
• Assume that we have read the input from the user to the
variable n.
• How will you extract the right most digit. n%10.
• How will you extract the second last digit? (n/10)%10.
Quick Problem to Solve :

Extracting the last two digits of a given integer:


Developing the idea:
• Let us assume n is an integer variable which is declared.
• Assume that we have read the input from the user to the
variable n.
• How will you extract the right most digit. n%10.
• How will you extract the second last digit? (n/10)%10.

Another Question : Print the last bit of the binary equivalent


representation of the decimal number n :
Quick Problem to Solve :

Extracting the last two digits of a given integer:


Developing the idea:
• Let us assume n is an integer variable which is declared.
• Assume that we have read the input from the user to the
variable n.
• How will you extract the right most digit. n%10.
• How will you extract the second last digit? (n/10)%10.

Another Question : Print the last bit of the binary equivalent


representation of the decimal number n : n%2.
Quick Problem to Solve :

Extracting the last two digits of a given integer:


Developing the idea:
• Let us assume n is an integer variable which is declared.
• Assume that we have read the input from the user to the
variable n.
• How will you extract the right most digit. n%10.
• How will you extract the second last digit? (n/10)%10.

Another Question : Print the last bit of the binary equivalent


representation of the decimal number n : n%2.

Yet Another Question : Print the complement of the last bit of the
binary equivalent representation of the decimal number n :
Quick Problem to Solve :

Extracting the last two digits of a given integer:


Developing the idea:
• Let us assume n is an integer variable which is declared.
• Assume that we have read the input from the user to the
variable n.
• How will you extract the right most digit. n%10.
• How will you extract the second last digit? (n/10)%10.

Another Question : Print the last bit of the binary equivalent


representation of the decimal number n : n%2.

Yet Another Question : Print the complement of the last bit of the
binary equivalent representation of the decimal number n :
(n+1)%2 or ((n%2)+1)%2
Comment about ranges of values:

declaration memory range


int x 32 bits -2,147,483,648 to 2,147,483,647
Comment about ranges of values:

declaration memory range


int x 32 bits -2,147,483,648 to 2,147,483,647
short x 16 bits -32,768 to 32,767
Comment about ranges of values:

declaration memory range


int x 32 bits -2,147,483,648 to 2,147,483,647
short x 16 bits -32,768 to 32,767
long x 64 bits -9223372036854775808
to 9223372036854775807
Comment about ranges of values:

declaration memory range


int x 32 bits -2,147,483,648 to 2,147,483,647
short x 16 bits -32,768 to 32,767
long x 64 bits -9223372036854775808
to 9223372036854775807
unsigned int x 32 bits 0 to 4,294,967,295
Comment about ranges of values:

declaration memory range


int x 32 bits -2,147,483,648 to 2,147,483,647
short x 16 bits -32,768 to 32,767
long x 64 bits -9223372036854775808
to 9223372036854775807
unsigned int x 32 bits 0 to 4,294,967,295
unsigned short x 16 bits 0 to 65,535
Comment about ranges of values:

declaration memory range


int x 32 bits -2,147,483,648 to 2,147,483,647
short x 16 bits -32,768 to 32,767
long x 64 bits -9223372036854775808
to 9223372036854775807
unsigned int x 32 bits 0 to 4,294,967,295
unsigned short x 16 bits 0 to 65,535
char x 8 bits ASCII code
Comment about ranges of values:

declaration memory range


int x 32 bits -2,147,483,648 to 2,147,483,647
short x 16 bits -32,768 to 32,767
long x 64 bits -9223372036854775808
to 9223372036854775807
unsigned int x 32 bits 0 to 4,294,967,295
unsigned short x 16 bits 0 to 65,535
char x 8 bits ASCII code

Curiousity : unsigned long long int stores:


Comment about ranges of values:

declaration memory range


int x 32 bits -2,147,483,648 to 2,147,483,647
short x 16 bits -32,768 to 32,767
long x 64 bits -9223372036854775808
to 9223372036854775807
unsigned int x 32 bits 0 to 4,294,967,295
unsigned short x 16 bits 0 to 65,535
char x 8 bits ASCII code

Curiousity : unsigned long long int stores:


0 to 18,446,744,073,709,551,615
Comment about ranges of values:

declaration memory range


int x 32 bits -2,147,483,648 to 2,147,483,647
short x 16 bits -32,768 to 32,767
long x 64 bits -9223372036854775808
to 9223372036854775807
unsigned int x 32 bits 0 to 4,294,967,295
unsigned short x 16 bits 0 to 65,535
char x 8 bits ASCII code

Curiousity : unsigned long long int stores:


0 to 18,446,744,073,709,551,615
sizeof(x) : will give you the number of bytes used to store the
variable x.
Let us write a program for it.
What did we do this week?


• Programming your ”friend”.





• Programming the ”turtle”. Week 1
• Interact with turtle at run time !






What did we do this week?


• Programming your ”friend”.





• Programming the ”turtle”. Week 1
• Interact with turtle at run time !







• First C program.






• Compilation and Execution. 




• The computing machine. 

• Variables and memory. This week



• Input and output in C. 




• Assignment and other operators. 



You might also like