C Programming Introduction-1
C Programming Introduction-1
Joseph University In
Tanzania (SJUIT)
P. O. Box 11007 Dar es Salaam
Storage Unit: The data & instruction that are entered have to be stored in the computer.
Similarly, the end results & the intermediate results also have to be stored somewhere before
being passed to the output unit. The storage unit provides solution to all these issues. This
storage unit is designed to save the initial data, the intermediate result & the final result. This
storage unit has 2 units: Primary storage & Secondary storage.
Central Processing Unit: Together the Control Unit & the Arithmetic Logic Unit are called as
the Central Processing Unit (CPU). The CPU is the brain of the computer. Like in humans, the
major decisions are taken by the brain itself & other body parts function as directed by the
brain. Similarly in a computer system, all the major calculations & comparisons are made
inside the CPU. The CPU is responsible for activating & controlling the operation of other
units of the computer system
Output Unit: The job of an output unit is just the opposite of an input unit. It accepts the
results produced by the computer in coded form. It converts these coded results to human
readable form. Finally, it displays the converted results to the outside world with the help of
output devices ( Eg :monitors, printers, projectors etc..).
Central Processing Unit: Together the Control Unit & the Arithmetic Logic Unit are called as
the Central Processing Unit (CPU). The CPU is the brain of the computer. Like in humans, the
major decisions are taken by the brain itself & other body parts function as directed by the
brain. Similarly in a computer system, all the major calculations & comparisons are made
inside the CPU. The CPU is responsible for activating & controlling the operation of other
units of the computer system
System Software- System software are a set of programs, responsible for running the
computer, controlling various operations of computer systems and management of computer
resources. They act as an interface between the hardware of the computer & the application
software. E.g.: Operating System
ii. Mainframe Computers: are powerful, large-scale machines designed to handle and
process massive amounts of data and transactions for large organizations. They are known
for their high reliability, scalability, and ability to support hundreds or thousands of users
simultaneously. Are particularly suited for mission-critical applications that require large-
scale data processing, such as in banking, insurance, government, and large enterprises.
iv. Microcomputers : Also called Personal Computers, are versatile, single-user systems
designed for individual tasks. They range from desktop PCs and laptops to smartphones
and tablets,
Working Scenarios of Microcomputers:
Education and Learning,
Graphic Design and Video Editing, Gaming,
Scientific Experimentation and Prototyping
Classification of Computers
B. Working principle and data handling abilities
Analog Computers
Analog computers work with continuous data or physical quantities such as voltage,
temperature, speed, or pressure. They measure continuous changes and represent data
using a continuous range of values, rather than discrete steps, Examples: Thermostats,
Speedometers
Digital Computers
work with discrete data, processing information in binary format (0s and 1s). They are
highly accurate and perform tasks by handling discrete signals, performing arithmetic and
logical operations at high speed, Example, Personal Computers (PCs), Digital Watches.
Hybrid Computers
combine the features of both analog and digital computers. They process both continuous
(analog) and discrete (digital) data. These computers convert analog signals to digital form
and vice versa for processing, takes analog signals and transforms them into digital form
before processing them.
Classification of Computers
C. Purpose
General Purpose
General computers are designed to perform various everyday tasks such as;
Document preparation, Financial analysis, Printing documents, Basic Input/Output
functions, Creating databases, Data Saving on a smaller scale, Calculations with
accuracy and consistency.
Special Purpose
When a computer is outlined specifically to perform a certain function, such type of
computer is identified as a Special Purpose computer.
Examples of special-purpose computers
Data representation in computer is about how a computer interprets and functions with
different types of information. Different information types require different representation
techniques. For instance, a video will be represented differently than a text document.
➢ Bit (Binary Digit): The smallest unit of data in a computer, which can be either a 0 or a 1.
➢ Byte: A group of 8 bits. Commonly, a byte is used to represent a single character in text,
such as a letter or symbol.
➢ Word: A unit of data that typically consists of multiple bytes (e.g., 16-bit, 32-bit, or 64-bit
words). The word size depends on the architecture of the computer and affects how much
data the CPU can process at once.
Data are representation inside the computer
1. Binary Representation
➢ Here's a simplified representation of how bits are grouped:
1 bit = Binary Digit
8 bits = 1 byte
1024 bytes = 1 kilobyte (KB)
1024 KB = 1 megabyte (MB)
1024 MB = 1 gigabyte (GB)
1024 GB = 1 terabyte (TB)
2. Representation of Numbers
Computers represent numbers in binary form using bits. There are several ways to represent
numbers:
Unsigned Integers: Binary Decimal
0000 0
Signed Integers:
0001 1
Floating-point Numbers: 0010 2
0011 3
Data are representation inside the computer
3. Representation of Characters
Characters (letters, digits, symbols) are typically represented using character encoding standards,
where each character is assigned a unique binary code. Common standards is ASCII (American
Standard Code for Information Interchange): Represents 128 characters. For example:
The letter 'A' is represented as 01000001 in binary.
The letter 'a' is represented as 01100001
4. Representation of Images
Images are represented as a collection of pixels, where each pixel has a specific color value. Color
values are typically represented using the RGB (Red, Green, Blue) model, with 8 bits for each
color channel, giving 24 bits per pixel (16.7 million possible colors).
For example:
A white pixel is represented as 11111111 11111111 11111111 (all color channels at full intensity).
A black pixel is represented as 00000000 00000000 00000000 (no intensity).
Images are stored in various formats such as JPEG, PNG, and BMP, which may involve
compression techniques to reduce file size.
Data are representation inside the computer
5. Representation of Sound
Sound is represented as a series of digital samples taken from an analog sound wave. These
samples are converted into binary form, representing the amplitude of the sound at a given
moment.
Common formats include:
WAV(Waveform Audio File Format): Uncompressed audio data.
MP3(MPEG Audio Layer 3): Compressed audio using lossy compression to reduce file size.
A sound sample might be represented as a sequence of 16-bit binary values corresponding to the
amplitude at each point in time.
6. Representation of Video
Video is essentially a sequence of images (frames) displayed in rapid succession, along with
synchronized audio. Each frame is represented as a collection of pixels, just like images, and the
video file may include compressed versions of both image and audio data.
Common video formats include MP4(MP4 stands MPEG-4 part 14), AVI (Audio Video
Interleave)
Data are representation inside the computer
Representation of Instructions
Machine Language: The binary representation of instructions that the CPU can execute directly.
For example, an instruction like "ADD" (to add two numbers) might be represented as a binary
code like 1010 0001 1100.
Summary
Numbers: Represented as binary integers or floating-point values.
Characters: Represented using encoding standards like ASCII or Unicode.
Images: Represented as pixel grids, with color information stored in binary.
Sound: Represented as digital samples of sound waves.
Video: Represented as sequences of image frames along with audio.
Instructions: Represented in binary as machine code for the CPU to execute.
INTRODUCTION TO PROGRAMMING
Languages are a means of communication. Normally people interact with each other through a
language. On the same pattern, communication with computers is carried out through a language.
This language is understood both by the user and the machine. Just as every language like
Swahili, English has its own grammatical rules; every computer language is also bounded by rules
known as syntax of that language. The user is bound by that syntax while communicating with the
computer system
INTRODUCTION TO PROGRAMMING
• Machine Language: This is the language (in the form of 0’s and 1’s, called binary numbers)
understood directly by the computer. It is machine dependent. It is difficult to learn and even
more difficult to write programs.
• Assembly Language: This is the language where the machine codes comprising of 0’sand 1’s
are substituted by symbolic codes (called mnemonics) to improve their understanding. It is the
first step to improve programming structure. Assembly language programming is simpler and
less time consuming than machine level programming, it is easier to locate and correct errors in
assembly language than in machine language programs.
INTRODUCTION TO PROGRAMMING
What is Algorithm?
Why C programming?
✓ If you know C, you will have no problem learning other popular programming languages
such as Java, Python, C++, C#, etc, as the syntax is similar,
✓ C is very fast, compared to other programming languages, like Java and Python,
1. Simplicity: C is a straightforward language with clean syntax. It doesn't have a lot of built-in
features, which makes it easy to learn.
2. Portability: C programs can be run on different machines with little to no modification,
making it highly portable.
3. Efficiency: C is known for its efficiency, allowing developers to write programs that execute
quickly.
4. Rich Library: The standard C library provides numerous built-in functions for performing
common tasks.
5. Extensibility: You can add your own functions to the C library to extend the capabilities of the
language.
6. Structured Language: C supports structured programming with its clean structure and
modularity.
7. Memory Management: C provides dynamic memory allocation, allowing programs to handle
memory efficiently.
8. Fast Execution: Since C is a compiled language and provides low-level access to system
resources, C programs are generally fast and efficient in terms of execution speed.
Features and structure of C program
10. Extendable: C can be extended by creating libraries and linking them to other C programs.
This feature allows developers to build upon the existing codebase, adding new functionalities
without starting from scratch.
Structure of a C Program
5. Executable Expressions & Statements: These are the lines of code that
perform the operations of the program. Statements are the executable
instructions in a C program, such as assignments, conditional statements,
loops, and function calls. Expressions perform computations, evaluations, and
logical operations, often involving operators like +, -, *, /, and &&.
7. Return Statement: Indicates the end of the main function. In the main()
function, a return statement is used to indicate the program’s exit status.
Typically, return 0; indicates that the program has executed successfully.
Structure of a C Program
Example C Program.
#include <stdio.h> // Preprocessor Command
int main() // Main Function
{
int num1, num2, sum; // Variable Declarations
// Executable Statements
printf("Enter two integers: ");
scanf("%d %d", &num1, &num2);
sum = num1 + num2;
printf("Sum: %d\n", sum);
return 0; // Return Statement
}
This program takes two integers as input, calculates their sum, and provide the
result
Diagrammatic representation of C program execution process
The execution process of a C program involves several stages, from writing the
code to running the executable.
Source Code This is the initial file written by the programmer, typically saved with a .c
(.c file) extension. It contains the program's code in C language syntax.
The compiler translates the preprocessed code into assembly language,
Compilation generating an assembly file.(It checks for syntax errors and converts
high-level C code into a lower-level language.)
Preprocessor The preprocessor takes the source code and handles directives, like
(Expanded #include and #define, expanding macros and including the contents of any
source code) header files.
The compiler translates the preprocessed code into assembly language,
Compilation generating an assembly file.(It checks for syntax errors and converts
high-level C code into a lower-level language.)
Compiler
(Assembly
Code)
Diagrammatic representation of C program execution process
Compiler
(Assembly
Code)
Assembly
Assembler The assembler takes the assembly code and converts it into machine code,
(Object producing an object file (.o or .obj file).(This object file contains machine-
Code) readable code but may not be executable yet)
Linking
Linker The linker combines all object files, as well as libraries and external
(Executable functions, into a single executable file.(It resolves function calls and
File) variable references, connecting any necessary code from libraries.)
Diagrammatic representation of C program execution process
Linker
(Executable
File)
The operating system loads the executable file into memory for execution
Loading (This is where the operating system allocates memory and sets up the
runtime environment)
Execution
The CPU executes the instructions in the executable file, starting from the
(Program is main function.
Run)
C Programming Language Concepts
Character Set
C uses a set of characters for writing programs:
1. Letters: A-Z and a-z
2. Digits: 0-9
3. Special Characters: + - * / = % & ^ ! ~ | . , ; ' " : ? # _
4. Whitespace Characters: space, tab, newline, etc.
Tokens
Tokens are the smallest elements of a program that are meaningful to the compiler.
They include:
1. Keywords: Reserved words with special meanings (e.g., int, return, if).
2. Identifiers: Names given to variables, functions, arrays, etc. (e.g., age,
calculateSum).
3. Constants: Fixed values that do not change during program execution (e.g., 10,
3.14, 'A’).
4. Operators: Symbols representing operations (e.g., +, -, *, &&).
5. Punctuators (Separators): Characters that separate statements (e.g., {}, [], ;, ,).
C Programming Language Concepts
Constants
Constants are values that do not change during the execution of a program.
Types of constants include::
1. Integer Constants: Whole numbers without decimal points (e.g., 10, -25)
Syntax of Integer
We use int keyword to declare the integer variable: int var_name;
Example
int number;
C Programming Language Concepts
Data Types &
Storage
B. Double Data Type:
Double-precision floating-point data type is used for larger decimal
numbers and provides more precision than float.
Range : 1.7E-308 to 1.7E+308
Size : 8 bytes
Format Specifier : %lf
Syntax of Double
The variable can be declared as double precision floating point using the
double keyword:
double var_name;
Example
double bigSize = 123456.79;
C Programming Language Concepts
Data Types &
Storage
C. Character Data Type:
Character data type is used to store a single character.
Range : (-128 to 127) or (0 to 255)
Size : 1 byte
Format Specifier : %c
Syntax of char
The char keyword is used to declare the variable of character type:
char var_name;
Example
char grade = 'A';
C Programming Language Concepts
Data Types &
Storage
E. Bool:
Boolean data type is used to represent true or false values (0 or 1).
Example:
bool a = true;
C Programming Language Concepts
Data Types &
Storage
Derived Data Type
1) Arrays: Arrays allow you to store multiple values of the same data type
in a contiguous memory block.
Example
int numbers[5] = {1, 2, 3, 4, 5};
C Programming Language Concepts
Data Types &
Storage
Derived Data Type
2) Pointers:
Pointers store the memory address of a variable.
They allow dynamic memory allocation and manipulation.
Example:
int *ptr = &number;
3) Structures:
Structures allow you to group variables of different data types under
a single name.
Example
struct Person {
char name[20]; int age;
};
C Programming Language Concepts
Data Types &
Storage
Derived Data Type
4) Unions: Unions are similar to structures, but they share the same
memory location for all their members.
Example
union Value {
int intValue;
float floatValue;
};
C Programming Language Concepts
Data Types &
Storage
Derived Data Type
5) Enumeration Data Type
✓ A set of named constants or enumerators that represent a collection of
connected values
✓ Enumeration allows you to define a set of named integer constants.
✓ It’s declared using enum keyword
✓ Example
enum Days {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday,
Sunday};
• Unsigned:
The unsigned keyword modifies an integer type to only represent non-
negative values (0 and positive numbers). This effectively doubles the
range of positive values it can represent compared to its signed
counterpart.
Example:
unsigned int b = 10; // Can only store zero and positive values
C Programming Language Concepts
Type Size Qualifiers
• Short:
The short keyword specifies a shorter version of an integer, which
typically uses less storage and has a smaller range compared to the
standard int type.
Example:
short int c = 32123; // Range is typically -32,768 to 32,767
• Long:
The long keyword specifies a longer version of an integer, which typically
uses more storage and has a larger range compared to the standard int
type.
Example:
long int d = 1000000L;
Larger range of integer
C Programming Language Concepts
Type Size Qualifiers
(Summary)