0% found this document useful (0 votes)
6 views

Unit 1

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Unit 1

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Unit-I

Computer systems consist of three components as shown in below


image: Central Processing Unit, Input devices and Output devices.
Input devices provide data input to processor, which processes data and
generates useful information that’s displayed to the user through output
devices. This is stored in computer’s memory.

Central Processing Unit

The Central Processing Unit (CPU) is called "the brain of computer" as it


controls operation of all parts of computer. It consists of two components:
Arithmetic Logic Unit (ALU), and Control Unit
Arithmetic Logic Unit (ALU)

Data entered into computer is sent to RAM, from where it is then sent to
ALU, where rest of data processing takes place. All types of processing,
such as comparisons, decision-making and processing of non-numeric
information takes place here and once again data is moved to RAM.

Control Unit

As name indicates, this part of CPU extracts instructions, performs


execution, maintains and directs operations of entire system.
Functions of Control Unit
Control unit performs following functions −

 It controls all activities of computer


 Supervises flow of data within CPU
 Directs flow of data within CPU
 Transfers data to Arithmetic and Logic Unit
 Transfers results to memory
 Fetches results from memory to output devices

Memory Unit
This is unit in which data and instructions given to computer as well as
results given by computer are stored. Unit of memory is "Byte".
1 Byte = 8 Bits

What is a Computer?

A computer is an electronic device that accepts data from the user,


processes it, produces results, displays them to the users, and stores the
results for future usage.
Data is a collection of unorganized facts & figures and does not provide
any further information regarding patterns, context, etc. Hence data means
"unstructured facts and figures".
Information is a structured data i.e. organized meaningful and processed
data. To process the data and convert into information, a computer is
used.

Functions of Computers

A computer performs the following functions −


Receiving Input
Data is fed into computer through various input devices like keyboard,
mouse, digital pens, etc. Input can also be fed through devices like CD-
ROM, pen drive, scanner, etc.
Processing the information
Operations on the input data are carried out based on the instructions
provided in the programs.
Storing the information
After processing, the information gets stored in the primary or secondary
storage area.

Sr.No. Computer Concepts & Description

1 History of Computers
The history of the computer dates back to several years.
There are five prominent generations of computers. Each
generation has witnessed several technological advances
which change the functionality of the computers.

2 Characteristics of Computer System


Characteristics of Computer System involve Speed,
Accuracy, Diligence, Versatility, Reliability, Automation,
Memory.

3 Basic Applications of Computer


Computers play a role in every field of life. They are used
in homes, business, educational institutions, research
organizations, medical field, government offices,
entertainment, etc.

4 Components of Computer System


Computer systems consist of three components as shown
in below image: Central Processing Unit, Input devices
and Output devices.

5 Input Devices – Keyboard and Mouse


Input devices help to get input or data from user.

6 Other Input Devices


There are few other input devices which help to feed data
to the computer.

7 Output Devices
Output devices help to display output to user

8 Computer Memory
Computer memory refers to storage area where data is
stored. It is of two types Primary Memory & Secondary
Memory.

9 Concept of Hardware and Software


The term hardware refers to mechanical device that
makes up computer.
Software can be categorized into two types - System
software & Application software

10 Programming Languages
The languages that are used to write a program or set of
instructions are called "Programming languages".
Programming languages are broadly categorized into
three types - Machine level language, Assembly level
language, High-level language.

11 Representation of Data/Information
Computer does not understand human language. Any
data, viz., letters, symbols, pictures, audio, videos, etc.,
fed to computer should be converted to machine language
first. Computers represent that data into different forms.

12 Data Processing & Data Processing Stages


Data processing is a process of converting raw facts or
data into a meaningful information.

13 Applications of IECT
IECT stands for Information Electronics and
Communication Technology.

Producing output
The processed information and other details are communicated to the
outside world through output devices like monitor, printer, etc.

Basics of Operating System

Operating System
Operating system is a software that controls system’s hardware and
interacts with user and application software.
In short, an operating system is computer’s chief control program.
Functions of Operating System
The operating system performs the following functions −
 It offers a user interface.
 Loads program into computer’s memory.
 Coordinates how program works with hardware and other software.
 Manages how information is stored and retrieved from the disk.
 Saves contents of file on to disk.
 Reads contents of file from disk to memory.
 Sends document to the printer and activates the printer.
 Provides resources that copy or move data from one document to
another, or from one program to another.
 Allocates RAM among the running programs.
 Recognizes keystrokes or mouse clicks and displayes characters or
graphics on the screen.

Sr.No. Operating System Concepts & Description

1 Types of Operating System

There are four types of operating systems.

2 Basics of Popular Operating Systems


Windows Operating System is developed by Microsoft
Corporation, Linux is a multitasking operating system that
supports various users and numerous tasks. It is open
source, i.e., code for Linux is available for free of cost

3 User Interface
While working with a computer, we use a set of items on
screen called "user interface". In simple terms, it acts as
an interface between user and software application or
program

4 Running an Application
The operating system offers an interface between
programs and user, as well as programs and other
computer resources such as memory, printer and other
programs.

5 Operating System Simple Setting


We will learn different settings in Operating System such
as changing system date and time, changing display
properties, etc.

6 File and Directory Management


File is nothing but a collection of information. The
information can be of numbers, characters, graphs,
images, etc. Directory is a place/area/location where a set
of file(s) will be stored.

7 File Management System


The file management system is a software which is used
to create, delete, modify and control access and save files.

8 Types of Files
There are five types of files such as Ordinary files,
Directory files, Device files, FIFO files

Introduction of Compiler Design


The compiler is software that converts a program written in a high-
level language (Source Language) to low-level language
(Object/Target/Machine Language).
Introduction to Algorithms
What is Algorithm? Algorithm Basics

The word Algorithm means “a process or set of rules to be followed in


calculations or other problem-solving operations”. Therefore Algorithm
refers to a set of rules/instructions that step-by-step define how a work is
to be executed upon in order to get the expected results.

It can be understood by taking an example of cooking a new recipe. To


cook a new recipe, one reads the instructions and steps and execute them
one by one, in the given sequence. The result thus obtained is the new
dish cooked perfectly. Similarly, algorithms help to do a task in
programming to get the expected output.
The Algorithm designed are language-independent, i.e. they are just plain
instructions that can be implemented in any language, and yet the output
will be the same, as expected.
What are the Characteristics of an Algorithm?

 Clear and Unambiguous: Algorithm should be clear and unambiguous.


Each of its steps should be clear in all aspects and must lead to only
one meaning.
 Well-Defined Inputs: If an algorithm says to take inputs, it should be
well-defined inputs.
 Well-Defined Outputs: The algorithm must clearly define what output
will be yielded and it should be well-defined as well.
 Finite-ness: The algorithm must be finite, i.e. it should not end up in an
infinite loops or similar.
 Feasible: The algorithm must be simple, generic and practical, such
that it can be executed upon with the available resources. It must not
contain some future technology, or anything.
 Language Independent: The Algorithm designed must be language-
independent, i.e. it must be just plain instructions that can be
implemented in any language, and yet the output will be same, as
expected.
Advantages of Algorithms:

 It is easy to understand.
 Algorithm is a step-wise representation of a solution to a given problem.
 In Algorithm the problem is broken down into smaller pieces or steps
hence, it is easier for the programmer to convert it into an actual
program.

Disadvantages of Algorithms:

 Writing an algorithm takes a long time so it is time-consuming.


 Branching and Looping statements are difficult to show in Algorithms.

How to Design an Algorithm?

In order to write an algorithm, following things are needed as a pre-


requisite:

1. The problem that is to be solved by this algorithm.


2. The constraints of the problem that must be considered while solving
the problem.
3. The input to be taken to solve the problem.
4. The output to be expected when the problem the is solved.
5. The solution to this problem, in the given constraints.

Algorithm 1: Add two numbers entered by the user

Step 1: Start
Step 2: Declare variables num1, num2 and sum.
Step 3: Read values num1 and num2.
Step 4: Add num1 and num2 and assign the result to sum.
sum←num1+num2
Step 5: Display sum
Step 6: Stop
Algorithm 2: Find the largest number among three numbers

Step 1: Start
Step 2: Declare variables a,b and c.
Step 3: Read variables a,b and c.
Step 4: If a > b
If a > c
Display a is the largest number.
Else
Display c is the largest number.
Else
If b > c
Display b is the largest number.
Else
Display c is the greatest number.
Step 5: Stop

Representation of algorithm

FLOW CHART
Diagrammatic representation of an algorithm is called flow chart.
Symbols used in flowchart are mentioned below –

Name Symbol Purpose


Name Symbol Purpose

Terminal start/stop/begin/end

Oval

Input/output Input/output of data

Parallelogram

Process Any processing to


be performaed can
be represented

Rectangle

Decision Decision operation


box that determine
which of the
alternative paths to
be followed

Diamon
Name Symbol Purpose

Connector Used to connect


different parts of
flowchart

Circle

Flow Join 2 symbols and


also represents
flow of execution

Arrows

Pre defined Module (or)


process subroutines
specified else
where
Double Sided Rectangle

Page Used to connect


connector flowchart in 2
different pages

Pentagon

For loop shows initialization,


symbol condition and
incrementation of
loop variable
Name Symbol Purpose

Hexagon

Document Shows the data


that is read

Printout

Example
Given below is the flowchart for finding an average of three numbers −
Data Types in C
A data type specifies the type of data that a variable can store such as
integer, floating, character, etc.
There are the following data types in C language.

Types Data Types

Basic Data Type int, char, float, double

Derived Data Type array, pointer, structure, union

Enumeration Data Type enum

Void Data Type void

Basic Data Types

The basic data types are integer-based and floating-point based. C


language supports both signed and unsigned literals.

The memory size of the basic data types may change according to 32 or
64-bit operating system.

Data Types Memory Size Range

char 1 byte −128 to 127

signed char 1 byte −128 to 127

unsigned char 1 byte 0 to 255


short 2 byte −32,768 to 32,767

signed short 2 byte −32,768 to 32,767

unsigned short 2 byte 0 to 65,535

int 2 byte −32,768 to 32,767

signed int 2 byte −32,768 to 32,767

unsigned int 2 byte 0 to 65,535

short int 2 byte −32,768 to 32,767

signed short int 2 byte −32,768 to 32,767

unsigned short int 2 byte 0 to 65,535

long int 4 byte -2,147,483,648 to 2,147,483,647

signed long int 4 byte -2,147,483,648 to 2,147,483,647

unsigned long int 4 byte 0 to 4,294,967,295

float 4 byte

double 8 byte

long double 10 byte

1. int main()
2. {
3. int a=10;
4. int A=20;
5. printf("Value of a is : %d",a);
6. printf("\nValue of A is :%d",A);
7. return 0;
8. }

Output
Value of a is : 10
Value of A is :20
HTML Tutorial

Comments in C

Comments in C language are used to provide information about lines of


code. It is widely used for documenting code. There are 2 types of
comments in the C language.

1. Single Line Comments


2. Multi-Line Comments

Single Line Comments

1. #include<stdio.h>
2. int main(){
3. //printing information
4. printf("Hello C");
5. return 0;
6. }

Mult Line Comments

Multi-Line comments are represented by slash asterisk \* ... *\. It can occupy
many lines of code, but it can't be nested. Syntax:

1. /*
2. code
3. to be commented
4. */
5.
6. #include<stdio.h>
7. int main(){
8. /*printing information
9. Multi-Line Comment*/
10. printf("Hello C");
11. return 0;
12. }
Programming Errors in C
Errors are the problems or the faults that occur in the program, which
makes the behavior of the program abnormal, and experienced developers
can also make these faults. Programming errors are also known as the bugs
or faults, and the process of removing these bugs is known as debugging.

These errors are detected either during the time of compilation or


execution. Thus, the errors must be removed from the program for the
successful execution of the program.

There are mainly five types of errors exist in C programming:

o Syntax error
o Run-time error
o Linker error
o Logical error
o Semantic error

Syntax error

Syntax errors are also known as the compilation errors as they occurred at the
compilation time, or we can say that the syntax errors are thrown by the compilers.
These errors are mainly occurred due to the mistakes while typing or do not follow
the syntax of the specified programming language. These mistakes are generally
made by beginners only because they are new to the language. These errors can be
easily debugged or corrected.

For example:

1. If we want to declare the variable of type integer,


2. int a; // this is the correct form
3. Int a; // this is an incorrect form.

Commonly occurred syntax errors are:

o If we miss the parenthesis (}) while writing the code.


o Displaying the value of a variable without its declaration.
o If we miss the semicolon (;) at the end of the statement.

Let's understand through an example.

1. #include <stdio.h>
2. int main()
3. {
4. a = 10;
5. printf("The value of a is : %d", a);
6. return 0;
7. }
Logical error

The logical error is an error that leads to an undesired output. These errors
produce the incorrect output, but they are error-free, known as logical
errors. These types of mistakes are mainly done by beginners. The
occurrence of these errors mainly depends upon the logical thinking of the
developer. If the programmers sound logically good, then there will be
fewer chances of these errors.

Let's understand through an example.

1. #include <stdio.h>
2. int main()
3. {
4. int sum=0; // variable initialization
5. int k=1;
6. for(int i=1;i<=10;i++); // logical error, as we put the semicolon after loop

7. {
8. sum=sum+k;
9. k++;
10. }
11. printf("The value of sum is %d", sum);
12. return 0;
13. }
Data Types in C Language
Data type defines the type of data being used.

Whenever we define a variable or use any data in the C programming, we have


to specify the type of the data, so that the compiler knows what type of data to
expect.

For example, you may want to use a number like 1, 2, 100, or a decimal
points number like 99.95, 10.5, or a text, like "Studytonight", all these values
are handled differently by the C compiler, hence, we use data types to define
the type of data used in any program.

 Each data type occupies some memory.


 Each data type has its own valid range of values.
 There are a set of operations that are allowed for every data type.

Data Types in C

 Broadly, there are 5 different categories of data types in the C language,


they are:

Type Example

Basic character, integer, floating-point, double.

Derived Array, structure, union, etc.

Enumeration enums
Type Example

Bool type true or false

void Empty value

Primary Data Types in C Programming


The C language has 5 basic (primary or primitive) data types, they are:

Character (char):

1. We use the keyword char for the character data type.


2. It is used to store single-bit characters and occupies 1 byte of memory.
3. You can store alphabets from A-Z(and a-z) and 0-9 digits using char data
type.
4. For example,
char b = 'A'
5. For char data type, it is necessary to enclose the data within single
quotes.
6. You can perform addition and subtraction operations on char data type
values.
7. The ASCII value of a char data type value should not exceed 127.

Integer (int):

1. We use the keyword int for the integer data type.


2. The int data type is used to store non-fractional numbers which include
positive, negative, and zero values.
3. The range of int is -2,147,483,648 to 2,147,483,647 and it occupies 2 or 4
bytes of memory, depending on the system you’re using.
For example
int a = 5550; int b = -90; int c = 0;

4. We can perform addition, subtraction, division, multiplication, bitwise,


and modulo operations on int data type.

Floating-point (float):

1. We use the keyword float for a floating-point data type.


2. The keyword float is used to store decimal numbers.
3. It occupies 4 bytes of memory and ranges from 1e-37 to 1e+37.

For example: float a = 0.05; float b = -0.005;

4. We can perform addition, subtraction, division, and multiplication


operations on float data type.

Double (double):
1. We use the keyword double for the double data type.
2. The double data type is used to store decimal numbers.
3. It occupies 8 bytes of memory and ranges from 1e-37 to 1e+37.
4. Here is how we use it in code,

double a = 10.09;

double b = -67.9;

5. The double data type has more precision than float so double gives more
accurate results as compared to float.
6. We can perform addition, subtraction, division, and multiplication
operations on double data type.

Void (void):

1. This means no value.


2. This data type is mostly used when we define functions.
3. The void data type is used when a function does not return any result.
4. It occupies 0 bytes of memory.
5. We use the void keyword for void data type.
6. Here is how we use the void type with functions,
void function()

//your code goes here

Each data type has a size defined in bits/bytes and has a range for the values
that these data types can hold.

Size of different Data types in C


 The size of different data types depends on the compiler and processor
types.
 In short, it depends on the Computer on which you are running the C
language and the version of the C compiler that you have installed.
1. char is 1 byte

 The char datatype is 1 byte in size or 8 bits.


 This is mostly the same and is not affected by the processor or the
compiler used.

2. int can be 2 bytes/4 bytes

 There is a very easy way to remember the size of int datatype.


 The size of int datatype is usually equal to the word length of the
execution environment of the program.
 In simpler words, for a 16-bit environment, int is 16 bits or 2 bytes, and
for a 32-bit environment, int is 32 bits or 4 bytes.

3. float is 4 bytes

 The float datatype is 4 bytes or 32 bits in size.


 It is a single-precision data type that is used to hold decimal values.
 It is used for storing large values.
 float is a faster data type compared to double, because double data type
works with very large values, hence it is slow.

4. double is 8 bytes

 The double data type is 8 bytes or 64 bits in size.


 It can store values that are double the size of what a float data type can
store, hence it is called double.
 In the 64 bits, 1 bit is for sign representation, 11 bits for the exponent,
and the rest 52 bits are used for the mantissa.
 The double data type can hold approximately 15 to 17 digits, before the
decimal and after the decimal.

5. void is 0 bytes

The void data type means nothing, hence it doesn't have a size.

……………………………………………………………………………………
Variables in C (with Examples)
 A variable is like a container (storage space) with a name in which you
can store data.
 When you create a variable in C programming a new memory space gets
assigned to the variable.
 While creating a new variable you have to provide it a name and specify
datatype for the variable.
 Once you have created the variable, you cannot change its datatype.
 When we create a variable in a C program, the C compiler allocates a
storage space, depending upon the data type of the variable(8
bits for char, 16/32 bits for int, etc.)
 Then that storage space is given a name which is the variable name.

 Once a variable is created, we can store value in it.


 We can change the value stored in a variable, but we cannot change the
datatype of the value stored in it.

Syntax for using Variable:

Here is how you can create or declare a new variable in the C language,

data_type var_name;
Where, data_type is a valid data type (along with datatype modifiers, if
required) and var_name is the name of the variable.

For example,
int marks;
Here, marks is the name of the variable, and it can store values of int type.

Assign value to Variable


Once we have declared or created the variable, then we can assign a value to it.
This is called variable definition.
// variable declaration
int marks;
// variable definition
marks = 10;
We can do declaration and definition in a single step too, like this
(recommended).
int marks = 10;
……………………………………………………………………..

You might also like