0% found this document useful (0 votes)
12 views28 pages

Cse Theory by Nuha

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views28 pages

Cse Theory by Nuha

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

1. What do you mean by operating system?

[19][13] Describe the functionality


of an operating system. [19] Write down the purpose of an operating system.
[13] 4. Write the name of five operating systems. [14]
ANSWER:

Operating system is basically a system program which is a medium


between all the computer hardware components and the application
softwares.

Through operating system, user/programmer can use their application


program to communicate with computer components to perform their
required computing tasks.

Without an operating system, application programs and computer hardware


is nothing and users can't do anything their computing tasks.

Operating systems perform the following two functions:

 operating system as extending the machine


 operating system as managing resources

Operating System as Extending the Machine

The operating system's function is to present the user with the equivalent of
an extended or virtual machine that is easier to program than the underlying
hardware.

Operating system basically provides a variety of services that programs can


obtain using special instructions called system calls.

Operating System as Managing Resources

Let's imagine that three programs present on some computers. All tied to print
their output simultaneously on the same printer. Then the first few lines might
be from the first program, second program, or third program and the next few
lines might also be from the first program, second program, or the third
program and so on.

1
In this way, the printout result of all the three programs would be chaos.
Therefore, here in this case operating system can bring order by buffering all
the output destined for the printer on the disk. When one program is finished,
then operating system copy its output from the disk file where it has been
stored to the printer, while at the same time the other program can continue
generating more output.

In this manner, operating system can perform for all the programs to print their
actual output in a correct way.

List of Popular Operating Systems

Here are the list of some popular windows based personal computer operating
systems:

 Windows XP
 Windows 7
 Windows 8
 Windows 8.1
 Windows Vista
 Windows 10
 and many more

And the following are the list of some popular linux based operating system,
mostly used by advance computer user:

 Ubuntu
 Backtrack
 Kali Linux
 Fedora
 Linux Mint
 and many more

2
2.Draw and explain the internal connection of a digital computer.

Block diagram of a computer gives you the pictorial representation of a


computer that how it works inside. Or you can say that, in computer's block
diagram, we will see how computer works from feeding the data to getting the
result.

Here is the block diagram of a computer system:

In the above diagram, both control (control unit or CU) and arithmetic &
logic unit (ALU) combinely called as Central Processing Unit (CPU).

Let's describe about all the parts as included in the above diagram one by
one.

The Processor Unit (CPU)

It is the brain of a computer system.

All major calculation and comparisons are made inside the CPU and it is also
responsible for activation and controlling the operation of other unit.

This unit consists of two major components that are arithmetic logic unit (ALU)
and control unit (CU).

3
Arithmetic Logic Unit (ALU)

Here arithmetic logic unit performs all arithmetic operations such as addition,
subtraction, multiplication and division. It also uses logic operation for
comparison.

Control Unit (CU)

And the control unit of a CPU controls the entire operation of a computer. It
also controls all devices such as memory, input/output devices connected to
the CPU.

CU fetches instructions from memory, decodes the instruction, interprets the


instruction to know what the task are to be performed and sends suitable
control signals to the other components to perform for the necessary steps to
execute the instruction.

Input /Output Unit

The input/output unit consists of devices used to transmit information between


the external world and computer memory.

The information fed through the input unit is stored in computer's memory for
processing and the final result stored in memory can be recorded or display
on the output medium.

Memory Unit

Memory unit is an essential component of a digital computer. It is where all


data intermediate and final results are stored.

The data read from the main storage or an input unit are transferred to
the computer's memory where they are available for processing.

This memory unit is used to hold the instructions to be executed and data to
be processed.

Disk Storage Unit

4
Data and instruction enters into a computer system through input device have
to stored inside the computer before actual processing start.

Two types of storage unit are primary and secondary storage unit.

Primary Storage Unit

Primary memory has direct link with input unit and output unit. It stores the
input data, calculation result.

Secondary Storage Unit

The primary storage is not able to store data permanently for future use. So
some other types of storage technology is required to store the data
permanently for long time, it is called secondary or auxiliary storage.

4. What is software? Describe the types of software with example.

A computer software is a set or collection of programs, procedures, and


related or associated documentation to perform specific task inside a
computer system.

Here are the list of some popular computer software:

 Google Chrome
 Safari
 Mozilla Firefox
 Microsoft Word
 Microsoft PowerPoint
 Skype
 Notepad etc.

Types of Computer Software

Basically there are two types of computer software as given below:

5
1. System Software
2. Application Software

Let's briefly define both the types of computer software one by one.

System Software

System software is a collection of programs that are created or designed to


control the operation of a computer system.

System software are basically designed to:

 run other computer software (application software)


 communicate with peripheral devices such as PenDrives, Memory
Cards, Printers, Mouses, Keyboards etc.
 develop some other type of software
 monitor the use of various computer hardware (devices) such as
memory, CPU etc.

Here are some of the popular examples of system software:

 MS-DOS
 Windows 7
 Windows 10
 Ubuntu
 OS/2 etc.

Application Software

Application software is also a collection of programs, but the collection of


programs are created or designed to carry out or perform operations for a
specified applications or tasks.

Here are the list of some popular application software-

 Calculator
 Opera

6
 Adobe Reader
 WhatsApp
 MX Player
 VLC Media Player etc.

5. What is the storage unit of a computer? Differentiate between primary and


secondary unit of a computer.

Data and instruction enters into a computer system through input device have
to stored inside the computer before actual processing start.

Two types of storage unit are primary and secondary storage unit.

Primary Storage Unit

Primary memory has direct link with input unit and output unit. It stores the
input data, calculation result.

Secondary Storage Unit

The primary storage is not able to store data permanently for future use. So
some other types of storage technology is required to store the data
permanently for long time, it is called secondary or auxiliary storage.

Identifiers

A C identifier is a name used to identify a variable. An identifier starts with a letter


A to Z or a to z or an underscore _ followed by zero or more letters, underscores,
and digits (0 to 9).

C does not allow punctuation characters such as @, $, and % within identifiers. C


is a case sensitive programming language. Thus, Manpower and manpower are
two different identifiers in C. Here are some examples of acceptable identifiers:
Mohd zara abc move_name a_123 myname50 _temp

J a23b9 retVa
7
Data-types :

• The data-type of an object in memory determines the set of values it can have
and what operations that can be performed on it.

• C allows implicit conversions as well as forced (potentially dangerous) casting.

• Generally, the type of a variable determines how much space it occupies in


storage and how the bit pattern stored is interpreted

The datatypes in C can be classified as follows: S.N. Types and Description

1. Basic Types: They are arithmetic types and consists of the two types: (a) integer
types and (b) floating- point types.

2 .Enumerated types: They are again arithmetic types and they are used to define
variables that can only be assigned certain discrete integer values throughout the
program.

3 .The type void: The type specifier void indicates that no value is available.

4. Derived types: They include (a) Pointer types, (b) Array types, (c) Structure
types, (d) Union types and (e) Function types. Integer Type

Integer Types

1. char - 1 byte -128 to 127 or 0 to 255


2. Unsigned char - 1 byte 0 to 255 signed char 1 byte -128 to 127
3. int - 2 or 4 bytes -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647
4. unsigned int - 2 or 4 bytes 0 to 65,535 or 0 to 4,294,967,295
5. short - 2 bytes -32,768 to 32,767
6. unsigned short - 2 bytes 0 to 65,535
7. long - 4 byte -2,147,483,648 to 2,147,483,647
8. unsigned long - 4 bytes 0 to 4,294,967,295

8
Floating-Point Types
Standard integer types with its storage sizes and value ranges:
Type Storage Size Value Range Precision
float 4 byte 1.2E-38 to 3.4E+38 6 decimalplaces
double 8 byte 2.3E-308 to 1.7E+308 15
long double 10 bytes 3.4E-4932 to 1.1E+4932 19

sizeof operator
• To get the exact size of a type or a variable on a particular platform, you can use
the sizeof operator.
• The expressions sizeof(type) yields the storage size of the object or type in
bytes.
• Following is an example to get the size of int type on any machine:
#include
int main()
{
printf("Storage size for int : %d \n", sizeof(int));
return 0;
}

C Operators
Operator performs mathematical or logical operations on the variables
 Arithmetic Operators
 Relational Operators
 Logical Operators
 Bitwise Operators
 Assignment Operators
 Misc Operators

9
Types of Constants in C

Type of Constants Data Example of Data type


type
Floating-point or Real constants double
float 20.987654
500.987654321

Example: 013 /*starts with


Octal constant int
0 */

Example: 0x90 /*starts


Hexadecimal constant int
with 0x*/

character constants char Example: 'X', 'Y', 'Z'

Constant type data type (Example)


int (53, 762, -478 etc )
unsigned int (5000u, 1000U etc)
long int, long long int
Integer constants (483,647 2,147,483,680)
float (10.456789)
Real or Floating point constants doule (600.123456789)
Octal constant int (Example: 013 /*starts with 0 */)
Hexadecimal constant int (Example: 0x90 /*starts with 0x*/)
character constants char (Example: ‘A’, ‘B’, ‘C’)
string constants char (Example: “ABCD”, “Hai”)

Unary Operator in C
These are the type of operators that act upon just a single operand for producing
a new value. All the unary operators have equal precedence, and their
associativity is from right to left. When we combine the unary operator with an
operand, we get the unary expression.

10
Infinite Loop
Infinite Loop is a loop that never terminates or ends and repeats indefinitely. Or
in other words, an infinite loop is a loop in which the test condition does not
evaluate to false and the loop continues forever until an external force is used to
end it. You can create an infinite loop:
 Using for loop
 Using while loop

*The below table consists of some valid and invalid Arithmetic


Expressions in C, with the explanation for all the invalid expressions.
Arithmetic Valid or
Explanation
Expression Invalid

6.4%5.0 Invalid modulus operator only works with integer values.

a- Invalid If the minus is a unary operator it should be before the operand, If the minus is a
binary operator, then the second operand is missing. So, either way, the
Expression is invalid.

-a Valid The unary minus operator is followed by an operand

a-b Valid The binary minus operator has two valid operands, so this is a valid Arithmetic
Expression.
5%2 Valid The modulous operator has two integer operands.

Difference between getc(), getchar(), getch() and getche()


getc():
It reads a single character from a given input stream and returns the
corresponding integer value (typically ASCII value of read character) on
success. It returns EOF on failure.
Syntax:
int getc(FILE *stream);

11
getch():
getch() is a nonstandard function and is present in conio.h header file which is
mostly used by MS-DOS compilers like Turbo C. It is not part of the C standard
library or ISO C, nor is it defined by POSIX (Source:
http://en.wikipedia.org/wiki/Conio.h)
Like above functions, it reads also a single character from keyboard. But it does
not use any buffer, so the entered character is immediately returned without
waiting for the enter key.
Syntax:
int getch();

getche()
Like getch(), this is also a non-standard function present in conio.h. It reads a
single character from the keyboard and displays immediately on output screen
without waiting for enter key.
Syntax:
int getche(void);

Function
Function is a group of statements that together perform a task.
• A function declaration tells the compiler about a function's name, return type,
and parameters. A function definition provides the actual body of the function.

Defining a function
The general form of a function definition in C programming language is as follows:
return_type function_name( parameter list )
{
body of the function
}
Return Type: A function may return a value. The return_type is the data type of
the value the function returns. Some functions perform the desired operations
without returning a value. In this case, the return_type is the keyword void.
12
Function Name: This is the actual name of the function. The function name and
the parameter list together constitute the function signature.
Parameters: A parameter is like a placeholder. When a function is invoked, you
pass a value to the parameter. This value is referred to as actual parameter or
argument. The parameter list refers to the type, order, and number of the
parameters of a function. Parameters are optional; that is, a function may contain
no parameters.
Function Body: The function body contains a collection of statements that define
what the function does.
Example
/* function returning the max between two numbers */
int max(int num1, int num2)
{ /* local variable declaration */
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}

Function Declaration
• A function declaration tells the compiler about a function name and how to call
the function. The actual body of the function can be defined separately.
return_type function_name( parameter list );
int max(int num1, int num2);

13
or int max(int, int);

Calling a Function
• When a program calls a function, program control is transferred to the called
function. A called function performs defined task, and when its return statement
is executed or when its function-ending closing brace is reached, it returns
program control back to the main program.
• To call a function, you simply need to pass the required parameters along with
function name, and if function returns a value, then you can store returned value.

Can function be called from more than one place within a program? Explain
with example.
Yes any function (user-defined or from a library) can be called any number
of times from any function including itself(in case of user-defined, called
recursion) except for the main function. But a function can only be defined
once.
Example : factorial code

Function call
A function call is an important part of the C programming language. It is
called inside a program whenever it is required to call a function. It is
only called by its name in the main() function of a program. We can pass
the parameters to a function calling in the main() function.
If a function is to use arguments, it must declare variables that accept the
values of the arguments. These variables are called the formal
parameters of the function.Formal parameters behave like other local
variables inside the function and are created upon entry into the function and
destroyed upon exit.
14
While calling a function, there are two ways in which arguments can be
passed to a function −

Sr.No. Call Type & Description

1 Call by value
This method copies the actual value of an argument into the formal
parameter of the function. In this case, changes made to the parameter
inside the function have no effect on the argument.

2 Call by reference
This method copies the address of an argument into the formal parameter.
Inside the function, the address is used to access the actual argument
used in the call. This means that changes made to the parameter affect
the argument.

Function prototype
A function prototype is simply the declaration of a function that specifies
function's name, parameters and return type. It doesn't contain function body.

A function prototype gives information to the compiler that the function may
later be used in the program.

Syntax of function prototype

returnType functionName(type1 argument1, type2 argument2, ...);

In the above example, int addNumbers(int a, int b); is the function prototype
which provides the following information to the compiler:
1. name of the function is addNumbers()

15
2. return type of the function is int

3. two arguments of type int are passed to the function


The function prototype is not needed if the user-defined function is defined
before the main() function.

Function definition
Function definition contains the block of code to perform a specific task. In our
example, adding two numbers and returning it.

Syntax of function definition

returnType functionName(type1 argument1, type2 argument2, ...)

//body of the function

When a function is called, the control of the program is transferred to the


function definition. And, the compiler starts executing the codes inside the
body of a function.

Recursion
A function that calls itself is known as a recursive function. And, this technique
is known as recursion.

16
How recursion works?

void recurse()

... .. ...

recurse();

... .. ...

int main()

... .. ...

recurse();

... .. ...

The recursion continues until some condition is met to prevent it.

To prevent infinite recursion, if...else statement (or similar approach) can be


used where one branch makes the recursive call, and other doesn't.

Example: Sum of Natural Numbers Using Recursion

#include <stdio.h>

17
int sum(int n);

int main() {
int number, result;

printf("Enter a positive integer: ");


scanf("%d", &number);

result = sum(number);

printf("sum = %d", result);


return 0;
}

Advantages and Disadvantages of Recursion

Recursion makes program elegant. However, if performance is vital, use loops


instead as recursion is usually much slower.

That being said, recursion is an important concept. It is frequently used in data


structure and algorithms. For example, it is common to use recursion in
problems such as tree traversal.

Arrays
• A group of contiguous memory locations used to store a series of related values
• The array name is a pointer to the first element
• All values have the same type
• Individual elements of an array are accessed via an integer index: array[index]
• Element indices start at 0, array[0] is the first element

18
Declaring Array
Data_type arrayName [ arraySize ];
• This is called a single-dimensional array.
• The arraySize must be an integer constant greater than zero.
• Data_type can be any valid C data type.
For example, to declare a 10- element array called balance of type double, use
this statement:
double balance[10];
Now balance is a variable array which is sufficient to hold up-to 10 double
numbers.

Initializing Arrays
• Arrays may be initialized with a list of suitable values
• No need to specify the number of elements for a 1D (1-dimensional) array
double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};
double balance[] = {1000.0, 2.0, 3.4, 17.0, 50.0};

Passing Arrays to Functions

• The array is passed – as an array of unspecified size (int array[]) OR – as a


pointer (int *array)
• Changes to the array within the function affect the “original” array
to pass an entire array to a function, only the name of the array is passed
as an argument.

19
Two-dimensional Arrays
A multi-dimensional array can be termed as an array of arrays that stores
homogeneous data in tabular form. Data in multidimensional arrays are
stored in row-major order.
int a [4] [3] ;
defines a two dimensional array

Initializing a multidimensional array


Here is how you can initialize two-dimensional and three-dimensional arrays:

Initialization of a 2d array

// Different ways to initialize two-dimensional array

int c[2][3] = {{1, 3, 0}, {-1, 5, 9}};

int c[][3] = {{1, 3, 0}, {-1, 5, 9}};

int c[2][3] = {1, 3, 0, -1, 5, 9};

Passing Arrays to Functions


 To pass multidimensional arrays to a function, only the name of the array is
passed to the function (similar to one-dimensional arrays).

 The function takes a two-dimensional array as an argument. We can also pass


arrays with more than 2 dimensions as a function argument.

 When passing two-dimensional arrays, it is not mandatory to specify the number


of rows in the array. However, the number of columns should always be
specified.

 In the function definition, the size of the array needs to be specified

20
 Any changes to array elements within the function affect the ``original’’ array
elements

Arrays as Function Parameters


 In this program, the array addresses (i.e., the values of the array names),
are passed to the function inc_array().
 This does not conflict with the rule that “parameters are passed by values”

Advantages of 2D array
 It is used to represent multiple data items of same type by using only single
name.
 It can be used to implement other data structures like linked lists, stacks,
queues, trees, graphs etc.
 Multidimensional arrays are used to represent matrices.

Disadvantages:
 We must know in advance that how many elements are to be stored in
array.
 Array is static structure. It means that array is of fixed size. The memory
which is allocated to array cannot be increased or reduced.
 Since array is of fixed size, if we allocate more memory than requirement
then the memory space will be wasted. And if we allocate less memory
than requirement, then it will create problem.
 The elements of array are stored in consecutive memory locations. So
insertions and deletions are very difficult and time consuming.

21
Pointer
What is pointer?
 Pointers are variables that contain memory addresses as their values.
 A variable name directly references a value.
 A pointer indirectly references a value. Referencing a value through a
pointer is called indirection.
 A pointer variable must be declared before it can be used.

Concept of Address and Pointers


• Memory can be conceptualized as a linear set of data locations.
• Variables reference the contents of a locations
• Pointers have a value of the address of a given location
Pointers are used in following variables
⚫ Variables of any basic data type

⚫ An Array

⚫ Functions

⚫ Structures, and

⚫ Unions

Advantages of Pointers

⚫ To point to different data structures

⚫ To achieve clarity and simplicity

⚫ More compact and efficient coding

⚫ To return multiple values via functions

⚫ Dynamic memory Allocation

22
Declaring a Pointer Variable

⚫ In previous example, ptr points to the variable x. We say that ptr is an integer
pointer.

⚫ Similarly, we have character pointer, floating pointer, long pointer, … .

⚫ To declare ptr as an integer pointer: int *ptr;

⚫ To declare ptr as a character pointer: char *ptr;


From the previous example: int x=547, *ptr;
ptr=&x;

Use of & and *

⚫ When is & used? ⚫ When is * used?

⚫ & -- "address operator" which gives or produces the memory address of a data
variable

⚫ * -- "dereferencing operator" which provides the contents in the memory


location specified by a pointer.

Arithmetic and Logical Operations on Pointers


⚫ A pointer may be incremented or decremented

⚫ An integer may be added to or subtracted from a pointer.

⚫ Pointer variables may be subtracted from one another.

⚫ Pointer variables can be used in comparisons, but usually only in a comparison


to NULL.

⚫ When an integer is added to or subtracted from a pointer, the new pointer


value is changed by the integer times the number of bytes in the data variable the
pointer is pointing to.

23
⚫ For example, if the pointer valptr contains the address of a double precision
variable and that address is 234567870, then the statement: valptr = valptr + 2;
would change valptr to 234567886

Pointers and One-Dimensional Array


⚫ Array name is a pointer to the first element. ◦ Address of first array element is
&x[0] or x. ◦ Address of 2 nd element is &x[1] or (x+1). ◦ So, address for i element
is &x[i] or (x+i).

⚫ Then to retrieve the content of i element is x[i] or *(x+i).

Pointer and Multidimensional Arrays


⚫ Since one dimensional array can be represented by pointer (the array name)

⚫ Pointer can also used in representing multidimensional array.

⚫ A multidimensional array can be declare as below:


Datatype (*ptr)[expression1] [expression2]…[expression i]

⚫ Noted that for int x[ ][20];

⚫ It become int (*x)[20]; Datatype (*ptr)[expression1] [expression2]…[expression


i] 20

⚫ To retrieve content of one dimensional array for array declared using pointer, it
will be *(arrayname + i)

⚫ Example: ◦ to retrieve x[2][5] in array declared using pointer, the code will be
*(*(x+2)+5) 21

24
Array and String
⚫ Since array name is the pointer to the first element,

⚫ When dealing with strings, the retrieval of the strings can be done by calling
the content pointer to the array * arrayname

Dynamic Memory Allocation


⚫ Previously, the number of elements already been defined when declaring the
array.

⚫ But since an array name is actually a pointer to the first element, it is possible
to define array as a pointer variable rather than as a conventional array.

⚫ In other meaning, you may no need to fixed the number of elements within an
array.

⚫ This is called Dynamic Memory Allocation

⚫ Generally it is done using a C function called malloc (under stdlib.h)


arrayname = (datatype *) malloc (quantity * sizeof(datatype));

Reordering the content of Array


⚫ The content of the array is entered by user.

⚫ Sometime an ordered list / content are required for further operation or


output.

⚫ The reordering can be done by comparing two elements in the array and swap
their position if needed.

⚫ The process need to carry out to compare and swap all the element to be in
ordered.

25
String
What is string?

⚫ Strings are actually one-dimensional array of characters terminated by a null


character '\0'.

⚫ The following declaration and initialization create a string consisting of the


word "Hello".

⚫ The size of the character array containing the string is one more than the
number of characters.

A. Fundamentals of Strings and Characters

⚫ Characters
◦ Building blocks of programs
● Every program is a sequence of meaningfully grouped characters
◦ Character constant
● An int value represented as a character in single quotes
● 'z' represents the integer value of z

⚫ Strings
◦ Series of characters treated as a single unit
● Can include letters, digits and special characters (* , /, $)
◦ String literal (string constant) – written in double quotes "Hello"
◦ Strings are arrays of characters
● String a pointer to first character
● Value of string is the address of first character

26
strcpy() & strncpy()
⚫ strcpy ◦ Copies its second argument ( a string ) into its first argument
( a char array that must be large enough to store the string and its
terminating null char )
⚫ strncpy ◦ Equivalent to strcpy, except that strncpy specifies the
number of char to be copied from the string into the array.

strcat() & strncat()


⚫ strcat ◦ Appends its second argument ( a string) to its first argument (
a char array containing a string).
⚫ strncat ◦ Appends a specified number of characters from the second
string to the first string.

strcmp() & strncmp()


⚫ strcmp ◦ Compares its first string argument with its second string
argument, character by char
◦ Return 0 if string are equal
◦ Return –ve value if the first string is < second string and +ve value if
the first string > second string
⚫ strncmp ◦ Equivalent to strcmp, except compares up to a specified
numbers of char.

27
strchr
◦ Searches for the first occurrence of a character in a string. If the char
is found, strchr returns a pointer to the char in the string, otherwise,
return NULL

strcspn
◦ Determines the length of the initial part of the string in its first
argument that does not contain any character from the string in its
second arguments.

strpbrk
◦ Searches its first string argument for the first occurrence of any char
in its second string argument
◦ If a char from the second argument is found, strpbrk returns a pointer
to the char in the first argument, otherwise return NULL

28

You might also like