Unit -1 Notes
WHAT IS A COMPUTER?
Computer is a device that transforms data into meaningful information. Data can be anything
like marks obtained by you in various subjects. It can also be name, age, gender, weight,
height, etc. of all the students in a class.
Computer can also be defined in terms of functions it can perform. A computer can i) accept
data, ii) store data, iii) process data as desired, and iv) retrieve the stored data as and when
required and v) print the result in desired format.
The major characteristics of a computer are high speed, accuracy, diligence, versatility and
storage.
Computer Organisation
The computer performs basically five major operations of functions irrespective of their size
and make. These are 1) it accepts data or instruction by way of input, 2) it stores data, 3) it
can process data as required by the user, 4) it gives results in the form of output, and 5) it
controls all operations inside a computer. We discuss below each of these operations.
1. Input: this is the process of entering data and programs into the computer system.
2. Control Unit (CU): The process of input, output, processing and storage is performed
under the supervision of a unit called 'Control Unit'. It decides when to start receiving
data, when to stop it, where to store data, etc. It takes care of step -by-step processing
of all operations inside the computer.
3. Memory Unit: Computer is used to store data and instructions.
4. Arithmetic Logic Unit (ALU): The major operations performed by the ALU are
addition, subtraction, multiplication, division, logic and comparison.
5. Output: This is the process of producing results from the data for getting useful
information. The ALU and the CU of a computer system are jointly known as the
central processing unit (CPU). You may call CPU as the brain of any computer
system.
SOFTWARE
As you are aware, computer cannot do anything on its own. It is the user who instructs
computer; what to do, how to do and when to do. In order to perform any task, you have
to give a set of instructions in a particular sequence to the computer. These sets of
instructions are called Programs. Software refers to a set of programs that makes the
hardware perform a particular set of tasks in particular order. Software can be classified
mainly into following categories and sub-categories.
System Software
System software helps run the computer and manage its hardware. It acts as a bridge
between the hardware and the user.
Operating System (OS):
Controls the entire computer system and allows other programs to run.
Examples: Windows, macOS, Linux, Android.
Utilities:
Special programs for maintenance and performance improvement.
Examples: Antivirus software, disk cleanup tools, file management tools.
Application Software
Application software is designed to help users perform specific tasks.
Generalized Packages:
Ready-made software for common needs.
Examples: MS Office, Photoshop, web browsers, media players.
Customized Packages:
Tailor-made software developed for a particular organization or purpose.
Examples: School management systems, banking software, inventory systems.
Algorithm
An algorithm is a step-by-step procedure to solve a problem or perform a task.
It is written in simple English (not in any specific programming language) so that anyone
can understand it.
Characteristics of a Good Algorithm
Clear and Simple – Easy to understand.
Finite Steps – Must complete in a limited number of steps.
Unambiguous – No confusion in the instructions.
Effective – Solves the problem efficiently.
Example: Algorithm to Add Two Numbers
Problem: Add two numbers and display the result.
Steps:
1. Start
2. Read first number (A)
3. Read second number (B)
4. Add A and B → store in SUM
5. Display SUM
6. Stop
Another Example: Check if a Number is Even or Odd
Algorithm
1. Start
2. Read a number N
3. If (N % 2 == 0), then print "Even"
4. Else print "Odd"
5. Stop
Flowchart
A flowchart is the graphical or pictorial representation of an algorithm with the help of
different symbols, shapes, and arrows to demonstrate a process or a program. With
algorithms, we can easily understand a program. The main purpose of using a flowchart is
to analyze different methods. Several standard symbols are applied in a flowchart:
Common Abbreviations Used in flowchart
Terminal Box - Start / End
Input / Output
Process / Instruction
Decision
Connector / Arrow
The symbols above represent different parts of a flowchart. The process in a flowchart
can be expressed through boxes and arrows with different sizes and colors. In a flowchart,
we can easily highlight certain elements and the relationships between each part.
Addition of two
Checking number is
number
even or odd
COMPUTER LANGUAGES
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 user and the machine. Just as
every language like English, Hindi has its grammatical rules; every computer language is
bound by rules known as SYNTAX of that language. The user is bound by that syntax
while communicating with the computer system.
1. Low-Level Languages (LLL)
Low-level languages are closer to machine code (binary) and are harder for humans to
read or write.
Types of Low-Level Languages
a) Machine Language
o Written in binary (0s and 1s).
o Directly executed by the CPU.
o Example: 10110000 01100001
b) Assembly Language
o Uses mnemonics (symbols) instead of binary.
o Needs an assembler to convert it into machine code.
Example:
MOV A, 5
ADD B, A
Advantages
Fast Execution: Code runs very efficiently because it directly interacts with
hardware.
Hardware Control: Gives complete control over system resources like memory and
CPU.
Less Translation Overhead: Machine code executes directly without heavy
compilation.
Disadvantages
Difficult to Learn: Requires knowledge of hardware and binary/assembly codes.
Time-Consuming: Writing and debugging code takes longer.
Not Portable: Code is specific to one type of processor or machine.
Poor Readability: Hard for humans to understand and maintain.
2. High-Level Languages (HLL)
High-level languages are closer to human language and easier to learn and use.
Examples
C, C++, Java, Python, JavaScript, FORTRAN, COBOL, BASIC, etc.
Advantages
Easy to Learn and Use: Syntax is similar to human language, making it beginner-
friendly.
Portable: Code can run on different machines with minimal changes.
Faster Development: Easier to write, debug, and maintain programs.
Rich Libraries: Provides pre-built functions and tools to speed up development.
Disadvantages
Slower Execution: Needs to be translated into machine code, which adds overhead.
Less Hardware Control: Cannot directly access or manage hardware components
efficiently.
Resource-Heavy: May use more memory and processing power.
Differences between high-level and low-level languages
Parameters High-Level Language Low-Level Language
Abstraction High abstraction, closer to Low abstraction, closer to
Level human language machine code
More complex and harder to
Easier to learn and use
Ease of Use learn
Highly portable across Less portable, often system-
Portability different systems specific
Development
Faster development time Slower development time
Speed
Python, Java, C++, Assembly language, Machine
Examples JavaScript code
Memory Automatic memory
Manual memory management
Management management
Parameters High-Level Language Low-Level Language
Built-in error handling Limited error handling,
Error Handling features requires manual checks
Performance Generally slower execution Generally faster execution
System programming,
Application development,
embedded systems, device
scripting, web development
Use Cases drivers
Compiler and Interpreter
The Compiler and Interpreter, both have similar works to perform. Interpreters and
Compilers convert the Source Code (HLL) to Machine Code (understandable by
Computer). In general, computer programs exist in High-Level Language that a human
being can easily understand. But computers cannot understand the same high-level
language, so for computers, we have to convert them into machine language and make
them readable.
What is a Compiler?
The Compiler is a translator that takes input i.e., High-Level Language, and produces an
output of low-level language i.e. machine or assembly language. The work of a Compiler
is to transform the codes written in the programming language into machine code (format
of 0s and 1s) so that computers can understand.
A compiler is more intelligent than an assembler it checks all kinds of limits, ranges,
errors, etc.
But its program run time is longer and occupies a larger part of memory. It has a slow
speed because a compiler goes through the entire program and then translates the
entire program into machine codes.
Role of a Compiler
For Converting the code written in a high-level language into machine-level language so
that computers can easily understand, we use a compiler. Converts basically convert high-
level language to intermediate assembly language by a compiler and then assembled into
machine code by an assembler.
Advantages of Compiler
Compiled code runs faster in comparison to Interpreted code.
Compilers help improve the security of Applications.
Compilers give Debugging tools, which help in fixing errors easily.
Disadvantages of Compiler
The compiler can catch only syntax errors and some semantic errors .
Compilation can take more time in the case of bulky code.
What is an Interpreter?
An Interpreter is a program that translates a programming language into a comprehensible
language. The interpreter converts high-level language to an intermediate language. It
contains pre-compiled code, source code, etc.
It translates only one statement of the program at a time.
Interpreters, more often than not are smaller than compilers.
Role of an Interpreter
The simple role of an interpreter is to translate the material into a target language. An
Interpreter works line by line on a code. It also converts high-level language to machine
language.
Advantages of Interpreter
Programs written in an Interpreted language are easier to debug.
Interpreters allow the management of memory automatically, which reduces memory
error risks.
Interpreted Language is more flexible than a Compiled language.
Disadvantages of Interpreter
The interpreter can run only the corresponding Interpreted program.
Interpreted code runs slower in comparison to Compiled code.
History of CProgramming Language
C programming is a general-purpose, procedure-oriented programming language. It is both
machine-independent and structured. C is a high-level programming language developed by
Dennis Ritchie in the early 1970s. It is now one of the most popular and influential
programming languages worldwide.
C is popular for its simplicity, efficiency, and versatility. It has powerful features including
low-level memory access, a rich set of operators, and a modular framework.
Apart from its importance with respect to the evolution of computer programming
technologies, the design of C language has a profound influence on most of the other
programming languages that are in use today. The languages that are influenced by C
include Java, PHP, JavaScript, C#, Python and many more. These languages have designed
their syntax, control structures and other basic features from C.
To learn about the history of C language, let's first start with its root and early developments.
The root of all modern languages is ALGOL (Algorithmic Language). ALGOL was the first
computer programming language to use a block structure, and it was introduced in 1960.
In 1967, Martin Richards developed a language called BCPL (Basic Combined Programming
Language). BCPL was derived from ALGOL. In 1970, Ken Thompson created a language
using BCPL called B. Both BCPL and B programming languages were typeless. After that, C
was developed using BCPL and B by Dennis Ritchie at the Bell lab in 1972. So, in terms of
history of C language, it was used in mainly academic environments, but at long last with the
release of many C compilers for commercial use and the increasing popularity of UNIX, it
began to gain extensive support among professionals.
Features of C Programming Language
1. Procedural Language
In a procedural language like C step by step, predefined instructions are carried out. C
program may contain more than one function to perform a particular task. New people to
programming will think that this is the only way a particular programming language works.
There are other programming paradigms as well in the programming world. Most of the
commonly used paradigm is an object-oriented programming language.
2. Fast and Efficient
Newer languages like Java, python offer more features than c programming language but due
to additional processing in these languages, their performance rate gets down effectively. C
programming language as the middle-level language provides programmers access to direct
manipulation with the computer hardware but higher-level languages do not allow this. That’s
one of the reasons C language is considered the first choice to start learning programming
languages. It’s fast because statically typed languages are faster than dynamically typed
languages.
3. Modularity
The concept of storing C programming language code in the form of libraries for further
future uses is known as modularity. This programming language can do very little on its own
most of its power is held by its libraries. C language has its own library to solve common
problems.
4. General-Purpose Language
From system programming to photo editing software, the C programming language is used in
various applications. Some of the common applications where it’s used are as follows:
Operating systems: Windows, Linux, iOS, Android, macOS
Databases: PostgreSQL, Oracle, MySQL, MS SQL Server, etc.
5. Rich set of built-in Operators
It is a diversified language with a rich set of built-in operators which are used in writing
complex or simplified C programs.
6. Libraries with Rich Functions
Robust libraries and functions in C help even a beginner coder to code with ease.
7. Middle-Level Language
C programming is called a middle-level language because it combines the features of both
low-level (machine-level) and high-level (user-friendly) programming languages.
Direct hardware access: C allows direct manipulation of memory addresses using pointers.
Readable syntax: C uses English-like keywords (if, while, for, etc.), making it easier to
understand.
8. Portable
C language is lavishly portable as programs that are written in C language can run and
compile on any system with either no or small changes.
9. Easy to Extend
Programs written in C language can be extended means when a program is already written in
it then some more features and operations can be added to it.
Structure of the C Program
There are 6 basic sections responsible for the proper execution of a program. Sections are
mentioned below:
1. Documentation
2. Preprocessor Section
3. Definition
4. Global Declaration
5. Main() Function
6. Sub Programs
1. Documentation
This section consists of the description of the program, the name of the program, and the
creation date and time of the program. It is specified at the start of the program in the form of
comments. Documentation can be represented as:
// description, name of the program, programmer name, date, time etc.
or
/*
description, name of the program, programmer name, date, time etc.
*/
Anything written as comments will be treated as documentation of the program and this will
not interfere with the given code. Basically, it gives an overview to the reader of the program.
2. Preprocessor Section
All the header files of the program will be declared in the preprocessor section of the
program. Header files help us to access other's improved code into our code. A copy of these
multiple files is inserted into our program before the process of compilation.
Example:
#include<stdio.h>
#include<math.h>
3. Definition
Preprocessors are the programs that process our source code before the process of
compilation. There are multiple steps which are involved in the writing and execution of the
program. Preprocessor directives start with the '#' symbol. The #define preprocessor is used
to create a constant throughout the program. Whenever this name is encountered by the
compiler, it is replaced by the actual piece of defined code.
Example:
#define PI 3.14159
4. Global Declaration
The global declaration section contains global variables, function declaration, and static
variables. Variables and functions which are declared in this scope can be used anywhere in
the program.
Example:
int num = 18;
5. Main() Function
Every C program must have a main function. The main() function of the program is written in
this section. Operations like declaration and execution are performed inside the curly braces
of the main program. The return type of the main() function can be int as well as void too.
void() main tells the compiler that the program will not return any value. The int main() tells
the compiler that the program will return an integer value.
Example:
void main()
or
int main()
6. Sub Programs
User-defined functions are called in this section of the program. The control of the program is
shifted to the called function whenever they are called from the main or outside the main()
function. These are specified as per the requirements of the programmer.
Example:
int sum(int x, int y)
{
return x+y;
}
Structure of C Program with example
Example: Below C program to find the sum of 2 numbers:
// Documentation
/**
* file: sum.c
* description: program to find sum.
*/
// Link
#include <stdio.h>
// Definition
#define X 20
// Global Declaration
int sum(int y);
// Main() Function
int main(void)
{
int y = 55;
printf("Sum: %d", sum(y));
return 0;
}
// Subprogram
int sum(int y)
{
return y + X;
}
Output
Sum: 75
Explanation of the above Program
Below is the explanation of the above program. With a description explaining the program's
meaning and use.
Sections Description
/**
* file: sum.c
It is the comment section and is part of the description section of
* description: program
the code.
to find sum.
*/
Header file which is used for standard input-output. This is the
#include<stdio.h> preprocessor section.
Sections Description
This is the definition section. It allows the use of constant X in
#define X 20 the code.
This is the Global declaration section includes the function
int sum(int y) declaration that can be used anywhere in the program.
int main() main() is the first function that is executed in the C program.
These curly braces mark the beginning and end of the main
{...} function.
printf("Sum: %d",
printf() function is used to print the sum on the screen.
sum(y));
We have used int as the return type so we have to return 0 which
states that the given program is free from the error and it can be
return 0; exited successfully.
int sum(int y)
{ This is the subprogram section. It includes the user-defined
return y + X; functions that are called in the main() function.
}
Keyword
Keywords are reserved words in C that have special meaning to the compiler.
You cannot use them as variable names, function names, or identifiers.
Always written in lowercase.
Cannot be redefined or used for other purposes.
auto break case char const continue default do
double else enum extern float for goto if
int long register return short signed sizeof static
struct switch typedef union unsigned void volatile while
Identifiers
Identifiers are names used to identify variables, functions, arrays, structures, etc. created
by the programmer.
Examples:
sum, totalMarks, area, MyFunction, _temp
Rules for Identifiers:
1. Can contain letters (A-Z, a-z), digits (0-9), and underscore (_).
2. Must start with a letter or underscore (not a digit).
3. Cannot be a keyword.
4. Case-sensitive (e.g., Age and age are different).
5. Should be meaningful for better readability.
Example in Code:
int marks; // 'marks' is an identifier
float student_avg; // 'student_avg' is an identifier
Constant
A constant is a value that does not change during the execution of the program.
Types of Constants:
Integer constants → 10, -25
Floating-point constants → 3.14, -0.5
Character constants → 'A', '9'
String constants → "Hello", "123"
Example:
#include <stdio.h>
#define PI 3.14159 // constant using preprocessor
int main() {
const int age = 18; // constant using 'const' keyword
printf("PI = %.2f\n", PI);
printf("Age = %d\n", age);
return 0;
}
Output
PI = 3.14
Age = 18
Variables
In programming, a variable is a container (storage area) to hold data.
To indicate the storage area, each variable should be given a unique name (identifier).
Variable names are just the symbolic representation of a memory location. For example:
Rules for naming variables:
Must start with a letter or underscore (_).
Can contain letters, digits, and underscores.
Cannot use keywords as variable names.
Case-sensitive (e.g., Age and age are different).
Example:
#include <stdio.h>
int main() {
int number = 10; // integer variable
printf("Number = %d\n", number);
return 0;
}
Output
Number = 10
Data Types
In C programming, data types are declarations for variables. This determines the type and
size of data associated with variables.
int is typically 4 bytes (not 2 as in Turbo C++).
long double is usually 16 bytes on many 64-bit systems.
Range of values also changes due to larger sizes.
Formula for Range (signed)
Formula for Range (unsigned)
Where n = total number of bits.
Format
Size Specifier
Data Type (bytes) Range
short int 2 -32,768 to 32,767 %hd
unsigned short int 2 0 to 65,535 %hu
unsigned int 4 0 to 4,294,967,295 %u
int 4 -2,147,483,648 to 2,147,483,647 %d
long int 4 -2,147,483,648 to 2,147,483,647 %ld
unsigned long int 4 0 to 4,294,967,295 %lu
Format
Size Specifier
Data Type (bytes) Range
long long int 8 -(2^63) to (2^63)-1 %lld
unsigned long long 0 to
8 %llu
int 18,446,744,073,709,551,615
signed char 1 -128 to 127 %c
unsigned char 1 0 to 255 %c
float 4 1.2E-38 to 3.4E+38 %f
double 8 1.7E-308 to 1.7E+308 %lf
long double 16 3.4E-4932 to 1.1E+4932 %Lf
Format specifiers are the symbols that are used for printing and scanning values of given data
types.
Example Program to Check Sizes
#include <stdio.h>
int main() {
printf("Size of int: %zu bytes\n", sizeof(int));
printf("Size of char: %zu byte\n", sizeof(char));
printf("Size of float: %zu bytes\n", sizeof(float));
printf("Size of double: %zu bytes\n", sizeof(double));
printf("Size of long int: %zu bytes\n", sizeof(long int));
printf("Size of long long int: %zu bytes\n", sizeof(long long int));
printf("Size of unsigned int: %zu bytes\n", sizeof(unsigned int));
printf("Size of short int: %zu bytes\n", sizeof(short int));
printf("Size of long double: %zu bytes\n", sizeof(long double));
return 0;
}