Fundamental of Computer Programming
(FOCP)
BY
DR. TANUSHREE MEENA
ASSISTANT PROFESSOR
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
INDIAN INSTITUTE OF INFORMATION TECHNOLOGY
BHOPAL, M.P. , INDIA
1
Course Instructions
❑ Compulsory Attendance in Class (75%).
❑ Exams : Mid Term, End Sem CLAs (CLA: Continuous Learning
Assessment)
❑ CLAs: Mini Test, Quizzes, Assignments, Group Discussion, Active
Learning: Flipped Classes, Poster Presentation, Report,
Problem/Project based Learning, Minute Paper, Viva (*No Late
Submissions)
❑ Online Learning.
❑ Google/Teams Classroom.
❑ Demonstration of Lab work.
2
Distribution of Marks (Theory)
Continuous Evaluation (40 marks) End Term Evaluation
(60 marks)
Mid Term Mini Test Attendance Class Assignments / Presentation Written
Performance / Active Learning
20 marks 20 marks 60 marks
Distribution of Marks (Practical)
Continuous Evaluation (40 marks) End Term Evaluation (60 marks)
Attendance Practical Experiment Results Lab Written Lab Exp. Viva
Performance Demo
40 marks 15 marks 15 marks 30 marks
List of Textbooks
❖ Kernighan , B.W. and D.M.Ritchie , The C Programming Language , 2nd ed., Pearson
Education India , 2015.
❖ Yashavant Kanetkar, Let Us C: Authentic Guide to C Programming Language (18th
Edition) , BPB Publications,India,2021.
❖ KingK.N , C Programming :A Modern Approach , 2nded., W.W.Norton & Company,
2008.
❖ E.Bala guru swamy , Programming in ANSIC , Eighth edition , McGraw Hill Education
(India) Private Limited , Noida , Uttar Pradesh , 2019.
4
Course Content
Module-1: Fundamentals of Programming
Brief History of Computing and Computers , Basic Organization of Computer , Representing Information as BitPatterns , NumberSystem ,
Basics of Computer Languages , Generation of Programming Languages , Compilers , Interpreter , Programming Environments and
Debugging , Types of Errors and Debugging Techniques , Problem-SolvingAspects , Introduction to Algorithms , Flowcharts , Pseudocode.
Module-2: Basic Programming and Control Statements
StructureofCProgram,LifeCycle of Program from Source Code to Executable,Keywords,Identifiers,PrimitiveDataTypesinC,
Variables,Constants, Input/Output Statements inC,Operators,TypeConversionandType Casting,Conditional Branching Statements , Iterative
Statements , Nested Loops , Break and Continue Statements.
Module-3: Modular Programming and Recursion
Functions , Declaration , Definition , Call and Return , Call by Value, Call by Reference , Showcase Stack Usage with help of Debugger ,
Scope of Variables , Storage Classes , Recursive Functions , Recursion vs Iteration.
Module-4: Array-based Programming
Arrays , One-dimensional , Two Dimensional , and Multidimensional Arrays , Operations on Array , Traversal , Insertion , Deletion , Merging
and Searching , Inter-Function Communication via Arrays , Passing a Row , Passing the Entire Array, Matrices , Strings , Read Operation ,
Writing and Manipulating Strings.
Module-5: CPointers , DMA and Structure
Pointers , Understanding Computer Memory, Accessing via Pointers , Pointers to Arrays , Drawback of Pointers , Dynamic Memory
Allocation , Structures , Unions.
Module-6:File in C Programming
File Handling , File Redirection , File Pointers , Preprocessor , Library Functions , Low-Level Programming.
What is Computer?
• Derived from word Compute.
• A computer is an electronic device that takes in data (input), processes it according to programmed
instructions (software), and produces results (output), while also storing information for later use.
History of Computing
Pre-1600
Early Computing Devices
to 1800s
1800s Mechanical Era
1900 to Electromechanical and Electronic Era
1940s
1940 to
Generations of Computer
Till Now
Early Computing Devices
❖Abacus (c. 4000 BCE): One of the earliest known tools for arithmetic, used in
ancient China and Mesopotamia. Used beads sliding on rods to perform
arithmetic operations. Still used today in some parts of Asia for teaching math
and mental calculation.
❖ Napier’s Bones (1617): Invented by John Napier to simplify multiplication
and division. Consisted of rods inscribed with multiplication tables—users
aligned them to calculate products. Based on lattice multiplication, it was a
precursor to mechanical calculators.
❖Pascaline (1642): A mechanical calculator by Blaise Pascal that could perform
addition and subtraction using a series of gears and wheels. One of the first
mechanical calculators to be mass-produced.
❖Leibniz’s Stepped Reckoner (1673): Improved on Pascal’s design to perform
all four basic operations. Used a stepped drum mechanism to perform all four
arithmetic operations.
Mechanical Era
❖Jacquard Loom (1804)
• Used punch cards to control weaving patterns in textiles.
• Considered the first programmable machine.
❖Difference Engine (1822)
• Designed by Charles Babbage to automate polynomial calculations.
• Could compute and print mathematical tables.
❖Analytical Engine (1837)
• Also, by Babbage, it was the first design for a general-purpose
computer.
• Included components like a “mill” (CPU), “store” (memory), and punch
card input/output
• Ada Lovelace wrote the first algorithm for it, making her the world’s
first programmer.
Electromechanical and Electronic Era
❖Tabulating Machine (1890)
• Invented by Herman Hollerith for the U.S. Census.
• Used punch cards and electrical circuits to tabulate data.
• Led to the founding of IBM.
❖ Z3 (1941)
• Built by Konrad Zuse in Germany.
• First programmable, fully automatic digital computer.
• Used electromechanical relays and binary arithmetic.
❖ENIAC (1945)
• First general-purpose electronic computer.
• Used vacuum tubes and could perform 5,000 additions per second.
• Required rewiring to change programs—no stored-program capability.
Generation of Computers
Generation Number Time duration Technology
1st 1940-1956 Vacuum Tubes
2nd 1956-1963 Transistors
3rd 1964-1971 Integrated Circuits
4th 1971-2010 Microprocessors
5th 2010-Present AI & Quantum Tech
First Generation: Vacuum Tube Computers
Technology Vacuum tubes for circuitry,
magnetic drums for memory.
Programming Machine language (binary
code).
Size & Power: Massive machines that
consumed a lot of electricity
and generated heat.
Examples: ENIAC, UNIVAC I, IBM 701.
Speed: Very slow—measured in
milliseconds.
Conclusion: Limited memory space and Processing Power, required Air Conditioning.
Second Generation: Transistor Computers
Technology Transistors replaced vacuum
tubes—smaller, more reliable,
and energy-efficient.
Programming Assembly language and early
high-level languages like
COBOL and FORTRAN.
Size & Power: Smaller and more efficient than
first-gen.
Examples: IBM 1401, CDC 1604
Speed: Microseconds.
Conclusion: Used in business, universities, and government; introduced batch processing &
multiprogramming.
Third Generation: Integrated Circuit (IC) Computers
Technology Integrated Circuits (ICs)
combined multiple transistors
on a single chip.
Programming High-level languages like
BASIC, Pascal.
Size & Power: Even smaller, more reliable,
and cheaper.
Examples: IBM System/360, PDP-8.
Speed: Nanoseconds.
Conclusion: Introduced operating systems, keyboards, monitors, and time-sharing.
Fourth Generation: Microprocessor Computers
Technology Microprocessors (entire CPU
on a single chip).
Programming Advanced high-level languages
(C, C++, Java).
Size & Power: Desktop and personal
computers became common.
Examples: Intel 4004, Apple Macintosh,
IBM PC.
Speed: Picoseconds.
Conclusion: Rise of personal computing, graphical user interfaces (GUIs), and the
internet.
Fifth Generation: Artificial Intelligence & Beyond
Technology AI, machine learning, quantum
computing, cloud computing.
Programming Natural language processing,
neural networks, Python, R.
Size & Power: Ultra-portable (smartphones,
wearables), cloud-based
infrastructure..
Examples: IBM Watson, Google DeepMind,
quantum prototypes.
Speed: Real-time processing, parallel
computing.
Conclusion:Automation, self-learning systems.
Fundamentals of Computer
Hardware's
Software's
❖ Hardware: Physical Access to the parts of Computer
❖ Software: Set of Instruction for the Computer to perform the task.
Fundamentals of Computer Organization
❖ Computer Organization: This states the working of the internal parts of the computer
(e.g. Working of ALU (Arithmetic Logic Unit), Memory Unit )
Every Computer has 4 Units
1. Memory Unit
2. Input Unit CPU
3. Output Unit
4. Processing Unit (Central Processing Unit) CU
Output
Input ALU Unit
Unit
CU: Control Unit
CPU: Central Processing Unit Memory Unit
ALU: Arithmetic Logic Unit
Roles of Computer Units and Their Links
CPU: Coordinates with CU: Surveillances all
the memory unit to CPU the activities of CPU.
perform desired tasks.
CU ALU: Performs all the
logical operations and
Input gives output to output unit.
Unit ALU
Output
Input Unit: A path through
Unit
which a user interacts with
the Processing Unit.
Output Unit: A path
Memory Unit through which processing
Memory Unit: Storage
unit of the device. unit interacts with the user.
Block Diagram of Computer Units
Memory Hierarchy
CPU
Resisters
Cache
Memory
Storage Capacity
Speed Primary Memory (RAM) (Increases from
(Increases from Top to Down)
Down to Top) Secondary Memory (Hard-Disk)
• Secondary Memory: External Memory
• Primary Memory, Cache Memory, CPU Registers: Internal Memory
Memory Classification
Types of Memories
RAM ROM
SRAM DRAM PROM EPROM EEPROM
• PROM: Programmable Random Only Memory
• SRAM: Static Random Access Memory • EPROM: Erasable Programmable Random
Only Memory
• DRAM: Dynamic Random Access Memory
• EEPROM: Electrical Erasable Programmable
Random Only Memory
Software: Program: Instructions
Prgm. Prgm.
No. 1 No. 2
Soft-
Ins 1: Ins 2: Ware
a % b; a * b;
Prgm. Prgm.
No. 3 No. 4
Ins 4: Ins 3:
a - b; a / b;
Ins 1: Prgm.
a + b;
Instruction Cycle
For CPU Running any Software
Instruction 1. CPU takes data from Hard Disk (HD).
Execution 2. Data Transferred from HD to Main Memory (RAM).
3. CPU takes only necessary data which is required.
500 MB
CPU 10 MB RAM HD
100 MB
(Main Memory)
Block Diagram of Execution of any Instruction
Instruction Cycle
1. Fetch: Instruction 2. Decode: Instruction
is fetched from HD is converted into ML
to RAM and RAM or BL. (Machine
to CPU. 2. language, Binary
1. Fetch
Decoder Language)
3. Execution:
3. Execution Allotted function
Ex. 1 program : 12 Instructions.
Software has 10 programs is performed.
120 Instructions
After 120 Cycles desired output
would be obtained.
Performance Equation
𝑵 ×𝑺
𝑻=
𝑹
T: Time required by the processor to execute single program.
N: Number of Instructions.
S: Steps involved in every instructions.
R: Processor speed (Cycle/seconds)
Representing Information as Bit Patterns
What is a Bit?
A bit (short for binary digit) is the smallest unit of data in a computer.
It can have only two possible values:
❑ 0 (OFF/False)
❑ 1 (ON/True)
All digital data — numbers, text, images, audio — is ultimately represented using
combinations of 0s and 1s, known as bit patterns.
Why Bit Patterns?
❑ Computers use binary (base-2) number system.
❑ Electrical circuits in a computer can easily represent two states:
❑ High voltage (1)
❑ Low voltage (0)
❑ Therefore, all information must be converted into binary format.
Types of Data Represented Using Bit Patterns
❑Numbers (Integers and Floating-point)
❑Characters (Text)
❑Images (Pixels and Colors)
❑Sound (Audio Samples)
❑Video (Sequence of images + audio)
Number System
• In digital system, the number system is used for representing the
information.
• The number system has different bases.
• The base represent as the total number of digits used in the number
system.
• There are four types of number systems.
28
Number System
• Binary System: 0 and 1 (2 Numbers)
• Octal System: 0 to 7 (8 Numbers)
• Decimal System: 0 to 9 (10 Numbers)
• Hexadecimal System: 0 to 9, A,B,C,D,E and F (16 Numbers)
Where A: 10 , B:11 , C:12, D:13, E:14, F:15
Number Representation
Format of all types of Systems: 𝐚𝟑 𝐚𝟐 𝐚𝟏 𝐚𝟎 𝐛
e.g. (𝟐𝟒𝟓)𝟖 , (𝐂𝐀𝐂)𝐇 , (𝟏𝟎𝟎𝟏)𝟐 , etc.
“b”: Base (for Binary:2, Oct:8) (a:0,1 and a: 0 to 7)
• “b >a” Always and Always but “a cannot be > b”
MSB LSB MSB: Most Significant bit
LSB: Least Significant bit
𝐚𝟑 𝐚𝟐 𝐚 𝟏 𝐚𝟎 𝐛
How to expand this?
𝐚𝟑 𝐚𝟐 𝐚𝟏 𝐚𝟎 𝐛 = (𝐛𝟑 × 𝐚𝟑 ) + (𝐛𝟐 × 𝐚𝟐 ) + (𝐛𝟏 × 𝐚𝟏 ) + (𝐛𝟎 × 𝐚𝟎 )
(𝟖𝟐𝟒𝟓)𝟏𝟎 : (8000+200+40+5) = (8 × 𝟏𝟎𝟑 ) + (2 × 𝟏𝟎𝟐 ) + (4 × 𝟏𝟎𝟏 ) + (5 × 𝟏𝟎𝟎 )
Binary to Decimal Conversion
For Numbers After Decimals: 𝐚𝟎 . 𝐚−𝟏 𝐚−𝟐 𝐚−𝟑 𝐛
How to expand this 𝐚𝟎 . 𝐚−𝟏 𝐚−𝟐 𝐚−𝟑 𝐛 = (𝐛𝟎 × 𝐚𝟎 ) + (𝐛−𝟏 × 𝐚−𝟏 ) +
(𝐛−𝟐 × 𝐚−𝟐 ) + (𝐛−𝟑 × 𝐚−𝟑 )
• (𝟏𝟎𝟎𝟏)𝟐 = (𝟐𝟑 × 𝟏) + (𝟐𝟐 × 𝟎) + (𝟐𝟏 × 𝟎) + (𝟐𝟎 × 𝟏) = 𝟖 + 𝟏 = 𝟗
• (𝟏𝟏𝟏. 𝟏)𝟐 = (𝟐𝟐 × 𝟏) + (𝟐𝟏 × 𝟏) + (𝟐𝟎 × 𝟏) + (𝟐−𝟏 × 𝟏) = 𝟕 + 𝟎. 𝟓 = 𝟕. 𝟓
• (𝟏𝟏𝟎𝟎𝟎)𝟐 = ?
• (𝟎𝟏𝟎𝟏)𝟐 = ?
• (𝟏𝟏𝟏𝟎. 𝟎𝟏)𝟐 = ?
• (𝟏𝟎𝟎𝟏𝟎)𝟐 = ?
Decimal to Binary Conversion
Division Approach is Used for Performing the Conversion
(𝟏𝟓)𝟏𝟎 = (? )𝟐
2 15 1
(𝟏𝟏𝟏𝟏)𝟐
2 7 1
2 3 1
Bottommost: MSB
1 Topmost: LSB
• Step 1: Divide Number By 2 write quotient below and remainder aside
• Step 2: Perform the division until quotient is less than 2 is achieved
• Step 3: Write the Number from Bottom to Top
Decimal to Binary Conversion
(𝟐𝟒)𝟏𝟎 = (? )𝟐
2 24 0
2 12 0
2 6 0
2 3 1
Ans:(𝟏𝟏𝟎𝟎𝟎)𝟐
1
(𝟑𝟐)𝟏𝟎 = (? )𝟐 (𝟑𝟏)𝟏𝟎 = (? )𝟐
(𝟔𝟒)𝟏𝟎 = (? )𝟐 (𝟔𝟑)𝟏𝟎 = (? )𝟐
(𝟏𝟖)𝟏𝟎 = (? )𝟐 (𝟏𝟓)𝟏𝟎 = (? )𝟐
(𝟓𝟔)𝟏𝟎 = (? )𝟐
Things to Remember (Power’s of 2)
𝟏 2 𝟔 64
𝟐 𝟐
𝟕
𝟐𝟐 4 𝟐 128
𝟐𝟑 8 𝟐𝟖 256
𝟐𝟒 16 𝟐𝟗 512
𝟐𝟓 32 𝟐𝟏𝟎 1024
MSB LSB
0/1 0/1 0/1 0/1 0/1 0/1 0/1 0/1 0/1 0/1 0/1 0/1
𝟐𝟏𝟏 𝟐𝟏𝟎 𝟐𝟗 𝟐𝟖 𝟐𝟕 𝟐𝟔 𝟐𝟓 𝟐𝟒 𝟐𝟑 𝟐𝟐 𝟐𝟏 𝟐𝟎
Octal Number System
Numbering Sequence: 0,1,2,3,4,5,6,7,10,11,12,13,14,15,16,17,20,21,……….
(𝟏𝟎)𝟖 = 𝟖 × 𝟏 + 𝟎 × 𝟏 = 𝟖 Decimal to Octal Conversion
(𝟏𝟏)𝟖 = 𝟖 × 𝟏 + 𝟏 × 𝟏 = 𝟗 8 62 6
(𝟏𝟐)𝟖 = 𝟖 × 𝟏 + 𝟐 × 𝟏 = 𝟏𝟎 7 8 285 5
(𝟏𝟕)𝟖 = 𝟖 × 𝟏 + 𝟕 × 𝟏 = 𝟏𝟓
(𝟔𝟐)𝟏𝟎 = (𝟕𝟔)𝟖 8 35 3
(𝟐𝟕)𝟖 = 𝟖 × 𝟐 + 𝟕 × 𝟏 = 𝟐𝟑 4
(𝟒𝟕)𝟖 = 𝟖 × 𝟒 + 𝟕 × 𝟏 = 𝟑𝟗 8 600 0 (𝟐𝟖𝟓)𝟏𝟎 = (𝟒𝟑𝟓)𝟖
Octal to Decimal Conversion 8 75 3
8 9 1
𝐚𝟑 𝐚𝟐 𝐚𝟏 𝐚 𝟎 𝐛 1 (𝟔𝟎𝟎)𝟏𝟎 = (𝟏𝟏𝟑𝟎)𝟖
= (𝐛𝟑 × 𝐚𝟑 ) + (𝐛𝟐 × 𝐚𝟐 )
+ (𝐛𝟏 × 𝐚𝟏 ) + (𝐛𝟎 × 𝐚𝟎 )
Conversion of Binary to Octal Number System
Make group of 3 , 3 bits starting from LSB
(𝟏𝟏𝟏𝟎𝟎𝟏𝟏𝟏𝟎𝟎𝟎𝟎𝟎𝟏𝟏𝟏𝟏𝟏)𝟐
Grouping of 3 bits: (𝟏𝟏𝟏𝟎𝟎𝟏𝟏𝟏𝟎𝟎𝟎𝟎𝟎𝟏𝟏𝟏𝟏𝟏)𝟐
Corresponding Octal Value: 716 037
Final Answer: (𝟕𝟏𝟔𝟎𝟑𝟕)𝟖
Hexadecimal Number System
Numbering Sequence: 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,……….
(𝟏𝟎)𝐇 = 𝟏𝟔 × 𝟏 + 𝟎 × 𝟏 = 𝟏𝟔 Decimal to Hexadecimal Conversion
(𝟐𝟓)𝐇 = 𝟏𝟔 × 𝟐 + 𝟓 × 𝟏 = 𝟑𝟕 16 62 E
(𝐀𝐁)𝐇 = 𝟏𝟔 × 𝟏𝟎 + 𝟏𝟏 × 𝟏 = 𝟏𝟕𝟏 3 16 285 D
(𝐀𝟒)𝐇 = 𝟏𝟔 × 𝟏𝟎 + 𝟒 × 𝟏 = 𝟏𝟔𝟒 (𝟔𝟐)𝟏𝟎 = (𝟑𝐄)𝐇 16 17 1
(𝐃𝐂)𝐇 = 𝟏𝟔 × 𝟏𝟑 + 𝟏𝟐 × 𝟏 = 𝟐𝟐𝟎 1
16 600 8
Hexadecimal to Decimal (𝟐𝟖𝟓)𝟏𝟎 = (𝟏𝟏𝐃)𝐇
16 37 5
Conversion
2
(𝟔𝟎𝟎)𝟏𝟎 = (𝟐𝟓𝟖)𝐇
Conversion of Binary to Hexa. Number System
Make group of 4 , 4 bits starting from LSB
(𝟏𝟏𝟏𝟎𝟎𝟏𝟏𝟏𝟎𝟎𝟎𝟎𝟎𝟏𝟏𝟏𝟏𝟏)𝟐
Grouping of 4 bits: (𝟏𝟏𝟏𝟎𝟎𝟏𝟏𝟏𝟎𝟎𝟎𝟎𝟎𝟏𝟏𝟏𝟏𝟏)𝟐
Corresponding Hex. Value: 3 9 C1 F
Final Answer: (𝟑𝟗𝐂𝟏𝐅)𝐇