C Program Basics
C Program Basics
Solving – 1(W1)
(2024-2025)
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 ?
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
• 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.
11
Memory
0000
Every variable is 0001
mapped to a
particular 8000
memory address 8001
8002 32
C
Variables in Memory
X = X*5
Variables in Memory
X = 10
T
i X = 20 20
m
e X = X +1
X = X*5
Variables in Memory
X = 10
T
i X = 20 21
m
e X = X +1
X = X*5
Variables in Memory
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
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
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.
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
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
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
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
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 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
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
1 7 4 1 4
1F0C16 = 174148
Hexadecimal Number System
bn-1 bn-2 b1 b0
Sign Magnitude
Contd.
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
/ 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.
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)
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.
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).
• https://www.rapidtables.com/convert/num
ber/base-converter.html