C Programming
C Programming
May , 2024
1. Introduction to
Programming
a. Introduction to Components
of a Computer System:
■ Memory
■ Processor
Contents ■ I/O Devices
■ Storage Devices
Chapter 1 b. Operating System
c. Assembler
d. Compiler
e. Interpreter
f. Loader
g. Linker
1. Introduction to
Programming
h. Representation of
Algorithm
Contents 1. Flowchart
2. Pseudocode
i. Basics of C Programming
+
● Computer: Computational device used to process the data
under the control of a program.
Program
Program
Data
■ CPU [Processing]
■ Memory [Storage]
■ Input-Output [external
communication]
● The Bus which is used to provide the communication between the major
components of the system (CPU, Memory, IO ) is called a System Bus.
CPU Mem
System Bus
IO
● The CPU is often referred to as the brain of
+ CPU
●
the computer
Control Unit
○ Controls the operation of the CPU and
Major structural hence the computer
components: ○ Arithmetic and Logic Unit (ALU)
○ Performs the computer’s data processing
function
○ Registers
○ Provide storage internal to the CPU
○ CPU Interconnection
○ Some mechanism that provides for
communication among the control unit,
ALU, and registers
+ Input and Output
Devices ● Input devices allow users to provide
data and instructions to the computer
● Common input devices include
Major structural keyboards, mouse, touchscreens, and
components: microphones
● Output devices display information
processed by the computer
● monitors, printers, speakers, and
projectors are examples of output
devices
● Computer memory is used to store data
+ Memory
and instructions temporarily or
permanently.
Random Access Memory (RAM):
Major structural ● RAM is volatile memory
● store data and instructions that the
components: CPU needs to access quickly.
● faster but more expensive compared to
secondary storage.
Read-Only Memory (ROM):
● ROM is non-volatile memory
● stores instructions required to start the
computer.
● contains firmware that initializes the
computer’s hardware components
during the boot process
● Secondary Storage Devices
● Optical Drives:
● Optical drives (e.g., CD/DVD/Blu-ray) use laser
technology.
● Less common due to digital downloads and
streaming.
● Blu-ray discs offer higher storage capacity than
DVDs.
● Secondary Storage Devices
+Storage Devices Cont. ● Cloud Storage:
○ Remote storage on servers (e.g.,
Google Drive, Dropbox).
○ Accessible from any device with an
internet connection.
Major structural ○ Consider security and privacy
implications.
components: ● Network Attached Storage (NAS):
○ Specialized storage servers connected
to a network.
○ Provide centralized storage for multiple
users or devices.
○ Used for backups, media streaming,
and file sharing.
+
● It reads each line of code, translates it into machine code, and executes it immediately.
Functions:
1. Parsing:
Analyzes the syntax of the source code to identify the sequence of commands.
2. Execution:
Interprets and executes each command or statement directly.
3. Error Handling:
Detects and reports syntax errors and runtime errors as they occur.
Example:
○ Python Interpreter (CPython) for the Python programming language.
○ Ruby Interpreter (MRI) for the Ruby programming language.
+ Linker
■ A linker is a program that combines multiple object files and libraries into a single executable or
shared library.
■ It resolves external references, merges code and data sections, and generates the final
executable file.
○ Functions:
■ Symbol Resolution:
● Resolves references to symbols defined in other object files or libraries.
■ Code and Data Merging:
● Combines code and data sections from multiple object files into a single executable.
■ Relocation:
● Adjusts memory addresses and references to account for the final layout of the
executable in memory.
○ Example:
■ GNU ld (GNU Linker) for linking object files and libraries in the GNU Compiler Collection (GCC).
■ Microsoft Linker (link.exe) for linking object files on the Windows platform.
○
+ Loader
○ A loader is a program that loads executable files into memory for execution by the CPU.
○ It prepares the program for execution by resolving references, allocating memory, and initializing data
structures.
● Functions:
○ Loading:
■ Reads the executable file from storage into memory.
○ Address Resolution:
■ Resolves symbolic references and relocates addresses based on the program's memory layout.
○ Memory Allocation:
■ Allocates memory space for the program's code, data, and stack segments.
● Example:
○ Windows Loader for loading executable files on the Windows operating system.
○ ELF Loader for loading executable files on Unix-like operating systems.
●
+ Putting it Altogether
Source:https://www.techtarget.com/searchdatacenter/definition/assembler
+ Introduction to Algorithms
❖ Algorithms are step-by-step procedures or methods used for solving
problems
❖ An algorithm is a finite sequence of well-defined instructions used to solve a
problem or perform a computation
Characteristics
➢ Input: Data provided to the algorithm.
➢ Output: Result produced by the algorithm.
➢ Definiteness: Each step of the algorithm must be precisely defined.
➢ Correctness: The algorithm must produce the correct output for all
valid inputs.
➢ Finiteness: The algorithm must terminate after a finite number of steps.
➢ Effectiveness: Each step must be executable using basic operations.
Representation of Algorithm
+ Flowchart Representation
● Flowcharts are graphical representations of algorithms, consisting of
various symbols connected by arrows to indicate the flow of control.
● Common flowchart symbols include:
● Start/End: Indicates the beginning
or end of the algorithm.
● Process: Represents a computational
step or operation.
● Decision: Represents a conditional
statement or branching.
● Input/Output: Represents data input or output.
+ Representation of Algorithm
Pseudocode Representation
● Pseudocode is a high-level description of algorithms using structured
English-like syntax, allowing for easy understanding and translation into
programming languages.
● Advantages of pseudocode include language independence, readability,
and aiding in algorithm design.
+ Problem Solving
5. Stop
Stop
+ Problem Solving
Algorithm: Finding Largest of two numbers SStart
1. Start Nnum1,
num2
2. Input: num1, num2,
3. If num1 > num2
then: num1 > num2
4. Stop Stop
+ Problem Solving
Algorithm: Print first 10 natural numbers
Algorithm: Checking whether number is even or odd
Algorithm: Checking whether number is a Prime Number
Algorithm: Calculate sum of digits of a number
Algorithm: Checking whether number is palindrome
Algorithm: Steps involved in C program development
+ History of C
● num
● total_amount
● _value
● counter1
● myFunction
Defining Constants:
● Constants can be defined using the const keyword to declare variables with fixed values.
● Example: const int MAX_VALUE = 100;
+ Components of C Language
Variables in C
● Variables are named memory locations used to store data
during program execution.
● They provide a way to manipulate and process data in a
program.
● Before using a variable, it must be declared with a specific
data type.
Declaration of Variables:
● The size of each data type depends on the compiler and the
system architecture.
● For example:
● int: Typically 4 bytes on most systems, range from
-2,147,483,648 to 2,147,483,647.
● float: Typically 4 bytes, range from approximately
1.2e-38 to 3.4e38 with 6 decimal places of precision.
● char: Typically 1 byte, representing ASCII characters.
Integrated Development Environment (IDE)
+
❖ provides comprehensive facilities to programmers for software
development
❖ consists of several components such as a source code editor,
build automation tools, and a debugger, all integrated into a
single user interface
❖ Code::Blocks: Code::Blocks is a free, open-source IDE specifically designed for C,
C++, and Fortran development. It is customizable, lightweight, and supports multiple
compilers, making it suitable for beginners and advanced users alike.
❖ Visual Studio Code (VS Code): extensions like C/C++ and C/C++ IntelliSense
❖ Dev-C++: Dev-C++ is a free, open-source IDE for Windows that is tailored for C and
C++ development. It offers a simple interface, integrated debugging, and support for
multiple compilers.
Storage Classes in C
+
❖ Storage classes define the scope, lifetime, and visibility of variables in C
programs.
❖ They play a crucial role in memory management and program optimization
void function() {
// File1.c
// File2.c
void function() {
num_calls++;
}
Storage Classes in C
+
4. register Storage Class:
● Requests the compiler to store the variable in a CPU register for faster
access.
● Variables declared as register are accessed more quickly than those stored in
memory.
● The use of register is optional, and the compiler may ignore the request.
register int i;
for (i = 0; i < 10; i++) {
// Loop body
}