Cse Theory by Nuha
Cse Theory by Nuha
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.
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.
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.
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.
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.
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.
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
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.
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 memory has direct link with input unit and output unit. It stores the
input data, calculation result.
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.
Google Chrome
Safari
Mozilla Firefox
Microsoft Word
Microsoft PowerPoint
Skype
Notepad etc.
5
1. System Software
2. Application Software
Let's briefly define both the types of computer software one by one.
System Software
MS-DOS
Windows 7
Windows 10
Ubuntu
OS/2 etc.
Application Software
Calculator
Opera
6
Adobe Reader
WhatsApp
MX Player
VLC Media Player etc.
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 memory has direct link with input unit and output unit. It stores the
input data, calculation result.
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
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.
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
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
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
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-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.
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 −
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.
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
Function definition
Function definition contains the block of code to perform a specific task. In our
example, adding two numbers and returning it.
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();
... .. ...
#include <stdio.h>
17
int sum(int n);
int main() {
int number, result;
result = sum(number);
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};
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
Initialization of a 2d array
20
Any changes to array elements within the function affect the ``original’’ array
elements
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.
⚫ An Array
⚫ Functions
⚫ Structures, and
⚫ Unions
Advantages of Pointers
22
Declaring a Pointer Variable
⚫ In previous example, ptr points to the variable x. We say that ptr is an integer
pointer.
⚫ & -- "address operator" which gives or produces the memory address of a data
variable
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
⚫ 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
⚫ 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.
⚫ 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?
⚫ The size of the character array containing the string is one more than the
number of 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.
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