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

C Program Basics

A detail pdf of c basis
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

C Program Basics

A detail pdf of c basis
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 101

Programming for Problem

Solving – 1(W1)
(2024-2025)

Prof Srinivas Prasad

1
Text/Reference Books
Text Book (s):
1. The C Programming Language,2nd Edition, Brian W. Kernighan / Dennis Ritchie,
Pearson, 2015,ISBN:978-9332549449
2. How to Solve it by Computer, R.G. Dromey, 1st Edition,Pearson Education India,
2008,ISBN:978-8120303881
3. Structure and Interpretation of Computer Programs, 2nd Edition, Harold Abelson and
Gerald Jay Sussman with Julie
Sussman,The MIT Press,1996,ISBN:978-0262510875.
Reference(s):
1. Byron Gottfried, Schaum's Outline of Programming with C, 2nd Edition,1996, McGraw-
Hill,ISBN:978-0070240353
2. How Computers Work, Tenth Edition, Ron White, Que Publishing, 2014, ISBN 978-
0789749840
3. SWAYAM, “Problem Solving Through Programming In C”,
https://onlinecourses.nptel.ac.in/noc23_cs53/preview
4. SWAYAM, “Introduction To Programming In C”,
https://onlinecourses.nptel.ac.in/noc22_cs40/preview
5. GitHub Copilot in VS Code,https://code.visualstudio.com/docs/editor/github-copilot
6. The modern coding,https://codieum.com/GitHub Copilot overview

2
What is a computer ?

• A computer is a machine which can accept


data, process the data and supply results.

In

Out
Computer
A computer
It is a machine which can accept data, process them,
and output results.

Central
Input Processing Output
Device Unit Device
(CPU)

Main Memory

Storage (Hard disk)


Von Neumann architecture
The Von Neumann Architecture

Named after John von Neumann, Princeton, he


designed a computer architecture whereby data and
instructions would be retrieved from memory,
operated on by an ALU, and moved back to memory
(or I/O)
This architecture is the basis for most modern
computers (only parallel processors and a few other
unique architectures use a different model)
Von Neumann Architectures

• There is a single pathway used to move both


data and instructions between memory, I/O and
CPU
– the pathway is implemented as a bus
– the single pathway creates a bottleneck
• known as the von Neumann bottleneck

– A variation of this architecture is the Harvard


architecture which separates data and instructions
into two pathways
Input Devices
Output Devices
• Keyboard • VDU / Monitor
• Mouse • Printers
• Joystick • Plotters
• Scanners (OCR) • Sound cards
• Bar code readers • Film and video
• Microphones / • Robot arms
Sound digitizers
• Voice recognition
devices
Storage Peripherals

• Magnetic Tape
– Data stored sequentially (back ups)
• Magnetic Disks
– Direct (random) access possible
– Types
• Hard Disks
• Floppy Disks(NOT USED)
• Optical Disks
– CDROM
– CD-RW
• Flash memory – Pen Drives
CPU
• All computations take place in CPU.
– Temporary storage of data in several
registers.
– It has circuitry to carry out arithmetic and
logic operations, take decisions, etc.
– It fetches instructions from the memory,
interprets (decodes) them, and performs
the requested operation.

10
Main Memory
• Uses semiconductor technology
• Allows direct access
– Memory sizes 2 Gbytes to 4 Gbytes
are typical today.

– Some measures to be remembered


• 1 K = 210 (= 1024)
• 1 M = 220 (= one million approx.)
• 1 G = 230 (= one billion approx.)

11
Memory

• How does memory look like ?


– A list of storage locations, each having a
unique address
– A variable is like a house. The name of the
variable is the address of the house.(NOT
NOW)
Address and Values
Memory Map

0000
Every variable is 0001
mapped to a
particular 8000
memory address 8001
8002 32

C
Variables in Memory

Instruction executed Memory location allocated


to a variable X
X = 10
T
i X = 20 10
m
e X = X +1

X = X*5
Variables in Memory

Memory location allocated


Instruction executed to a variable X

X = 10
T
i X = 20 20
m
e X = X +1

X = X*5
Variables in Memory

Memory location allocated


Instruction executed to a variable X

X = 10
T
i X = 20 21
m
e X = X +1

X = X*5
Variables in Memory

Memory location allocated


Instruction executed to a variable X

X = 10
T
i X = 20 105
m
e X = X +1

X = X*5
Variables (contd.)

X = 20

20 X
Y=15

X = Y+3 ? Y

Y=x/6
Variables (contd.)

X = 20

20 X
Y=15

X = Y+3 15 Y

Y=x/6
Variables (contd.)

X = 20

18 X
Y=15

X = Y+3 15 Y

Y=x/6
Variables (contd.)

X = 20

18 X
Y=15

X = Y+3 3 Y

Y=X/6
Computer Memory

•RAM: Random Access Memory


•ROM: Read Only Memory
•PROM (programmable read-only memory).
•EPROM (erasable programmable read-only memory-ultraviolet light).
•EEPROM( electrically erasable programmable ROM-electricity).
•le ROM
•EPROM: Erasable Programmable ROM
•Electrically Erasable Programmable ROM 23
Typical Configuration of a PC

24
How does a computer work?
• Main difference with a calculator.
– Stored program concept.
• What is a program?
– Set of instructions for carrying out a specific
task.
• Where are programs stored?
– In secondary memory, when first created.
– Brought into main memory, during execution.
• Question: How are numbers stored
internally?
25
Software

• A set of instructions that tells the computer


what to do
• Two main types of software
– Application software(Application Software
includes programs that do real work for user)
– System software(controls the hardware so that
application software can perform a task)

26

System Software
Application Software
Classification of Software
• Two broad categories:
1. Application Software
• Used to solve a specific problem.
• Editor, financial accounting, weather
forecasting, etc.
2. System Software
• Helps in running other programs.
• Compiler, operating system, etc.

27
Operating Systems
• Makes the computer easy to use.
– Basically the computer is very difficult
to use.
– Understands only machine language.
• Operating systems make computers
easy to use.

28
Contd.
• Popular operating systems:
– DOS: single-user
– Windows 2000/XP: single-user multitasking
– Unix: multi-user
– Linux: a free version of Unix
• Question:
– How multiple users can work on the same
computer?

29
Digital Computers use Binary Numbers!
• A digital computer is built out of tiny
electronic switches.
– For ease of manufacturing and
reliability, switches can be in one of two
states, ON and OFF.
– Switch can represent 0 and 1 (a digit in
the binary number system).
• A computer works based on the
binary number system.
30
 When we type some letters or words, the
computer translates them in numbers as
computers can understand only numbers.

 The value of each digit in a number can be


determined using −
The digit
The position of the digit in the number
The base of the number system
Number System :: The Basics
• We are accustomed to using the so-
called decimal number system.
– Ten digits :: 0,1,2,3,4,5,6,7,8,9
– Every digit position has a weight which is
a power of 10 (Called PV).
• Example:
– 234 = 2 x 102 + 3 x 101 + 4 x 100

250.67 = 2 x 102 + 5 x 101 + 0 x 100 +


6 x 10-1 + 7 x 10-2

32
Common Number Systems
Used by Used in
System Base Symbols humans? computers?

Decimal Yes No
10 0, 1, … 9
Binary 2 0, 1 No Yes
Octal No
8 0, 1, … 7 No
Hexa- No
decimal 16 0, 1, … 9, No
A, B, … F
Quantities/Counting (1 of 3)
Decimal Binary Octal Hexa-
decimal
0 0 0 0
1 1 1 1
2 10 2 2
3 11 3 3
4 100 4 4
5 101 5 5
6 110 6 6
7 111 7 7
Quantities/Counting (2 of 3)
Decimal Binary Octal Hexa-
decimal

8 1000 10 8
9 1001 11 9
10 1010 12 A
11 1011 13 B
12 1100 14 C
13 1101 15 D
14 1110 16 E
15 1111 17 F
Quantities/Counting (3 of 3)
Decimal Binary Octal Hexa-
decimal
16 10000 20 10
17 10001 21 11
18 10010 22 12
19 10011 23 13
20 10100 24 14
21 10101 25 15
22 10110 26 16
23 10111 27 17
Conversion Among Bases

• The possibilities:

Decimal Octal

Binary Hexadecimal
Weight

12510 => 5 x 100 = 5


2 x 101 = 20
1 x 102 = 100
125
Base
Binary to Decimal

Decimal Octal

Binary Hexadecimal
Binary to Decimal
• Technique
–Multiply each bit by n
2 , where
n is the “weight” of the bit
–The weight is the position of
the bit, starting from 0 on the
right
–Add the results
Example
Bit “0”

1010112 =>1 x 20 = 1
1 x 21 = 2
0 x 22 = 0
1 x 23 = 8
0 x 24 = 0
1 x 25 = 32
4310
Decimal to Binary

Decimal Octal

Binary Hexadecimal
Decimal to Binary

• Technique
– Divide by two, keep track of the remainder
– First remainder is bit 0 (LSB, least-significant
bit)
– Second remainder is bit 1
– Etc.
Example

12510 = ?2 2 125
2 62 1
2 31 0
2 15 1
2 7 1
2 3 1
2 1 1
0 1

12510 = 11111012
Octal to Decimal

Decimal Octal

Binary Hexadecimal
Octal to Decimal

• Technique
– Multiply each bit by 8n, where n is the “weight”
of the bit
– The weight is the position of the bit, starting
from 0 on the right
– Add the results
Example

7248 => 4 x 80 = 4
2 x 81 = 16
7 x 82 = 448
46810
Hexadecimal to Decimal

Decimal Octal

Binary Hexadecimal
Hexadecimal to Decimal

• Technique
– Multiply each bit by 16n, where n is the
“weight” of the bit
– The weight is the position of the bit, starting
from 0 on the right
– Add the results
Example

ABC16 => C x 160 = 12 x 1 = 12


B x 161 = 11 x 16 = 176
A x 162 = 10 x 256 = 2560
274810
Decimal to Binary

Decimal Octal

Binary Hexadecimal
Octal to Binary

Decimal Octal

Binary Hexadecimal
Octal to Binary

• Technique
– Convert each octal digit to a 3-bit equivalent
binary representation
Example

7058 = ?2

7 0 5

111 000 101

7058 = 1110001012
Hexadecimal to Binary

Decimal Octal

Binary Hexadecimal
Hexadecimal to Binary

• Technique
– Convert each hexadecimal digit to a 4-bit
equivalent binary representation
Example

10AF16 = ?2

1 0 A F

0001 0000 1010 1111

10AF16 = 00010000101011112
Decimal to Octal

Decimal Octal

Binary Hexadecimal
Decimal to Octal

• Technique
– Divide by 8
– Keep track of the remainder
Example

123410 = ?8

8 1234
8 154 2
8 19 2
8 2 3
0 2

123410 = 23228
Decimal to Hexadecimal

Decimal Octal

Binary Hexadecimal
Decimal to Hexadecimal

• Technique
– Divide by 16
– Keep track of the remainder
Example

123410 = ?16

16 1234
16 77 2
16 4 13 = D
0 4

123410 = 4D216
Binary to Octal

Decimal Octal

Binary Hexadecimal
Binary to Octal

• Technique
– Group bits in threes, starting on right
– Convert to octal digits
Example

10110101112 = ?8

1 011 010 111

1 3 2 7

10110101112 = 13278
Binary to Hexadecimal

Decimal Octal

Binary Hexadecimal
Binary to Hexadecimal

• Technique
– Group bits in fours, starting on right
– Convert to hexadecimal digits
Example

10101110112 = ?16

10 1011 1011

2 B B

10101110112 = 2BB16
Octal to Hexadecimal

Decimal Octal

Binary Hexadecimal
Octal to Hexadecimal

• Technique
– Use binary as an intermediary
Example

10768 = ?16

1 0 7 6

001 000 111 110

2 3 E

10768 = 23E16
Hexadecimal to Octal

Decimal Octal

Binary Hexadecimal
Hexadecimal to Octal

• Technique
– Use binary as an intermediary
Example

1F0C16 = ?8

1 F 0 C

0001 1111 0000 1100

1 7 4 1 4

1F0C16 = 174148
Hexadecimal Number System

• A compact way of representing binary


numbers.
• 16 different symbols (radix = 16).
0  0000 8  1000
1  0001 9  1001
2  0010 A  1010
3  0011 B  1011
4  0100 C  1100
5  0101 D  1101
6  0110 E  1110
7  0111 F  1111
Binary-to-Hexadecimal Conversion
• For the integer part,
– Scan the binary number from right to left.
– Translate each group of four bits into the
corresponding hexadecimal digit.
• Add leading zeros if necessary.
• For the fractional part,
– Scan the binary number from left to right.
– Translate each group of four bits into the
corresponding hexadecimal digit.
• Add trailing zeros if necessary.
Example

1. (1011 0100 0011)2 = (B43)16


2. (10 1010 0001)2 = (2A1)16
Hexadecimal-to-Binary Conversion

• Translate every hexadecimal digit


into its 4-bit binary equivalent.
• Examples:
(3A5)16 = (0011 1010 0101)2
(12.3D)16 = (0001 0010 . 0011 1101)2
(1.8)16 = (0001 . 1000)2
Unsigned Binary Numbers

• An n-bit binary number


B = bn-1bn-2 …. b2b1b0
• 2n distinct combinations are possible, 0 to 2n-1.
• For example, for n = 3, there are 8
distinct combinations.
– 000, 001, 010, 011, 100, 101, 110, 111
• Range of numbers that can be
represented
n=8  0 to 28-1 (255)
n=16  0 to 216-1 (65535)
n=32  0 to 232-1 (4294967295)
Signed Integer Representation

• Many of the numerical data items


that are used in a program are
signed (positive or negative).
– Question:: How to represent sign?
• Three possible approaches:
– Sign-magnitude representation
– One’s complement representation
– Two’s complement representation
Sign-magnitude Representation

• For an n-bit number representation


– The most significant bit (MSB) indicates sign
0  positive
1  negative
– The remaining n-1 bits represent magnitude.

bn-1 bn-2 b1 b0

Sign Magnitude
Contd.

• Range of numbers that can be


represented:
Maximum :: + (2n-1 – 1)
Minimum ::  (2n-1 – 1)
• A problem:
Two different representations of
zero.
+0  0 000….0
-0  1 000….0
Floating-point Numbers
• The representations discussed so far applies
only to integers.
– Cannot represent numbers with fractional parts.
• We can assume a decimal point before a 2’s
complement number.
– In that case, pure fractions (without integer parts)
can be represented.
• We can also assume the decimal point
somewhere in between.
– This lacks flexibility.
– Very large and very small numbers cannot be
represented.
Representation of Floating-Point Numbers
• A floating-point number F is represented
by a doublet <M,E> :
F = M x BE
• B  exponent base (usually 2)
• M  mantissa
• E  exponent
– M is usually represented in binary form, with
an implied decimal point before it.
• For example,
In decimal,
0.235 x 106
In binary,
0.101011 x 20110
Example :: 32-bit Representation

M E
24 8
– M represents a 2’s complement fraction
1 > M > -1
– E represents the exponent (in 2’s complement
form)
127 > E > -128
• Points to note:
– The number of significant digits depends on the
number of bits in M.
• 6 significant digits for 24-bit mantissa.
– The range of the number depends on the number
of bits in E.
• 1038 to 10-38 for 8-bit exponent.
The Reality
• The representation for floating-
point numbers as shown is just
for illustration.
• The actual representation is a
little more complex.
• In C:
– float :: 32-bit representation
– double :: 64-bit representation
Common Powers (1 of 2)

• Base 10
Power Term Symbol Value
10-12 pico p .000000000001
10-9 nano n .000000001
10-6 micro  .000001
10-3 milli m .001
103 kilo k 1000
106 mega M 1000000
109 giga G 1000000000
1012 tera T 1000000000000
Common Powers (2 of 2)

• Base 2

Power Term Symbol Value


210 Kilo K 1024
220 Mega M 1048576
230 Giga G 1073741824

• In computing, particularly w.r.t. memory,


the base-2 interpretation generally applies
Example

1. Double click on My Computer


2. Right click on C:
3. Click on Properties

/ 230 =
Concept of Bits and Bytes
• Bit
– A single binary digit (0 or 1).
• Nibble
– A collection of four bits (say, 0110).
• Byte
– A collection of eight bits (say, 01000111).
• Word
– Depends on the computer.
– Typically 4 or 8 bytes (that is, 32 or 64 bits).

91
Size of Numbers
• A k-digit decimal number
– Can express unsigned integers in the range
0 to 10k – 1
• For k=3, from 0 to 999.

• A k-bit binary number


– Can express unsigned integers in the range
0 to 2k – 1
• For k=8, from 0 to 255.
• For k=10, from 0 to 1023.

92
Representation of Characters
• Many applications deal with non-numerical data.
– Characters and strings.
– How to encode non-numerical data?
• Three standards in use:
– Extended Binary Coded Decimal Interchange Code
(EBCDIC)
• Used in older IBM machines.
– American Standard Code for Information Interchange
(ASCII)
• Most widely used today.
– UNICODE
• Used to represent all international characters.
• Used by Java.
93
ASCII Code
• Each individual character is numerically
encoded into a unique 7-bit binary code.
– A total of 27 or 128 different characters.
– A character is normally encoded in a byte (8
bits), with the MSB not been used.
• The binary encoding of the characters
follow a regular ordering.
– Digits are ordered consecutively in their
proper numerical sequence (0 to 9).
– Letters (uppercase and lowercase) are
arranged consecutively in their proper
alphabetic order.
94
Some Common ASCII Codes
‘A’ :: 41 (H) 65 (D) ‘0’ :: 30 (H) 48 (D)
‘B’ :: 42 (H) 66 (D) ‘1’ :: 31 (H) 49 (D)
……….. ………..
‘Z’ :: 5A (H) 90 (D) ‘9’ :: 39 (H) 57 (D)

‘a’ :: 61 (H) 97 (D) ‘(‘ :: 28 (H) 40 (D)


‘b’ :: 62 (H) 98 (D) ‘+’ :: 2B (H) 43 (D)
……….. ‘?’ :: 3F (H) 63 (D)
‘z’ :: 7A (H) 122 (D) ‘\n’ :: 0A (H) 10 (D)
‘\0’ :: 00 (H) 00 (D)

95
Encoding Schemes : ASCII, and Unicode
The ability of a computer system to understand signals or letters
depends on its character set. Character set has its standards known
as character set code like ASCII, ISCII and Unicode which are
encoding languages with unique characteristics that define their
usage.
ASCII ( American Standard Code for Information Interchange) most
of the micro computers, mini computers and some mainframe
computers uses this code.
ASCII code has two versions - ASCII – 7 and ASCII – 8.
•ASCII – 7 code use 7 bits for one signal or character. By this, 27
=128 different characters can be used.
•ASCII – 8 code use 8 bits for one signal or character. By this, 28
=256 different characters can be used.

ASCII is a standard that numbers each characters from the character


set.
It includes
 26 small and 26 capital letters of the basic Latin alphabet. A to Z,
 Digits 0 to 9,Basic punctuation: ?, !, ", (, {, [, and of course the full stop (.).
 Simple mathematical symbols: +, -, =, %.
 Some other signs useful such as *, #, $,
Unicode

Unicode is a new universal coding standard adopted by all new platforms. It is promoted by Unicode
Consortium.Unicode provides a unique number for every character irrespective of the platform,
program and the language. It is a character coding system designed to support the worldwide
interchange, processing, and display of the written texts of the diverse languages.
UNICODE has become the largest adopted standard across the net. That means you can store and
represent most of the written languages in the world in UNICODE (including most Indic variations)
format.
It is a world wide character-encoding standard . Its main objective is to enable a single,unique
character set that is capable of supporting all characters from all scripts, as well as symbols , that
are commonly utilized for computer processing throughout the world. Unicode is a variable bit
encoding that doesn't fit into one 8 bit alone.It provides every character a special numeric value
as well as a name.It provides encode all the characters used for writing for almost all languages.
How Do Computers Store Images, audio, video?

Digital image consists of a bunch of little squares, each having a single color, and
each of which is called a pixel

Binary Images:It is the simplest type of image. It takes only two values
i.e, Black and White or 0 and 1.
Gray-scale images:Grayscale images are monochrome images, Means
they have only one color. Grayscale images do not contain any
information about color. Each pixel determines available different grey
levels.
Color Image:The images are represented as red, green and blue (RGB
images). And each color image has 24 bits/pixel means 8 bits for each of
the three color band(RGB).

Since computers represent data


in digital form, (as bits and bytes)
the sound in analog form must
be converted to digital form to be
retained. Likewise then the digital
representation of the sound must
then be converted to analog form
for our ears to hear it.
An Unbalanced System

Source: Bob Colwell keynote ISCA’29 2002


How multiple users can work ?

• Computers are often connected in a


network.
• Many users may work on a computer.
– Over the network.
– At the same time.
– CPU and other resources are shared among
the different programs.
TOOLs

• https://www.rapidtables.com/convert/num
ber/base-converter.html

You might also like