0% found this document useful (0 votes)
17 views59 pages

PPS IMPORTANT QUESTIONS and Solution

The document provides an overview of computer fundamentals, including definitions and functions of computers, operating systems, software, and hardware. It explains the components of a computer's architecture, types of software, and various types of computers, as well as algorithms and memory hierarchy. Additionally, it discusses operators in programming, specifically in the context of the C language.

Uploaded by

sus.sinha2004
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)
17 views59 pages

PPS IMPORTANT QUESTIONS and Solution

The document provides an overview of computer fundamentals, including definitions and functions of computers, operating systems, software, and hardware. It explains the components of a computer's architecture, types of software, and various types of computers, as well as algorithms and memory hierarchy. Additionally, it discusses operators in programming, specifically in the context of the C language.

Uploaded by

sus.sinha2004
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/ 59

IMPORTANT QUESTIONS

THEORY:

1. W hat is Computer? Explain the block diagram of the computer.

Ans: The computer is the


electronic data processing device which take data as a input, process it in a very high
speed and return a valuable information as output. It cannot do only arithmetic operation
but logical operation also.

Input Unit
Input unit consists of devices with the help of which we can enter the information to the
computer. This unit is linked between user and computer. Input devices translate user
input into the form of computer language.
Ex – Keyword, Mouse, OMR, OCR, BCR, etc.

The Central Processing Unit (CPU)


CPU is consider as the brain of the computer. CPU performs all types of data processing
operations. It store data, instructions and intermediate results. It control the operation of
all parts of the computer. CPU itself has following three components:-
(i) ALU
(ii) CU
(iii) MU
1. Arithmetic Logic Unit (ALU) :-
This unit consist of two subsections:-
(a) Arithmetic Section:- Function of arithmetic section is to perform arithmetic
operations like addition, subtraction, multiplication, division and all complex
operation also.
(b) Logical Section:- Function of logical section is to perform logic operations like
comparing, selecting, matching and merging of data.

2. Control Unit (CU) :-


This unit control the operations of all parts of computer. It does not carry out any actual
data processing operations.
Function of Control Unit are :-
(a) It is responsible for controlling the transfer of data and instruction among other
unit of the computer.
(b) It manages and coordinates all the unit of the computer.
(c) It obtains the instruction from the memory interpretant and direct the operations
of the computer.
(d) It communicates with input and output devices for transfer of data or result from
storage.
(e) It does not process or store data.

3. Memory Unit (MU) :-


This unit can store data, instructions and intermediate results. It is also known as internal
storage unit or main memory or primary memory or random access memory. It size effect
speed, power and capability.
Functions of Memory Unit are :-
(a) It store all the data to be processed and instructions required for processing.
(b) It store intermediate results of processing.
(c) It store final result of processing before these results are released to the output
device.
(d) All input and output are transmitted through main memory.
2. W hat is Operating System? W rite its characteristics, objectives
and types.
Ans:

Operating System
An operating system is a program that acts as an interface between the software
and the computer hardware.

It is an integration set of specialised programs that are used to manage overall


resources and operations of the computer.

It is a specialised software that controls and monitors the execution of all other
programs that reside in the computer, including application programs and other
system software.

Objectives of Operating System :-


Making a computer system convenient to use in an efficient manner.

To hide the details of the hardware resources from the users.

To provide users a convenient interface to use the computer system.

To act as an intermediary between the hardware and its users and making it easier
for the users to access and use other resources.

Manage the resources of a computer system.

Keep track of who is using which resource, granting resource requests, according
for resource using and mediating conflicting requests from different programs and
users.
The efficient and fair sharing of resources among users and
programs.

Characteristics of Operating System :-


Memory Management - - It keeps track of primary memory, i.e., what parts of it are
in use by whom, what parts are not in use, etc. Allocates the memory when the
process or program requests it.

Processor Management - - Allocates the processor (CPU) to a process. Deallocate


processor when processor is no longer required.

Device Management - - Keeps tracks of all devices. This is also called I/O controller.
Decides which process gets the device when and for how much time.

File Management - - Allocates the resources. Deallocates the resource. Decides who
gets the resources.

Security - - By means of passwords & similar other techniques, preventing


unauthorized access to programs & data.

Job accounting - - Keeping track of time & resources used by various jobs and/or
users.

Control over system performance - - Recording delays between requests for a


service & from the system.

Interaction with the operators - - The interaction may take place via the console of
the computer in the form of instructions. Operating System acknowledges the
same, do the corresponding action and inform the operation by a display screen.
Error- detecting aids - - Production of dumps, traces, error messages and other
debugging and error- detecting methods.

Coordination between other software and users - - Coordination and assignment of


compilers, interpreters, assemblers and other software to the various users of the
computer systems

3. W hat is Software and Hardware? Explain different types of


software.
Ans:
Software is a set of programs, which are designed to perform a well- defined function. A
program is a sequence of instructions written to solve a particular problem.
There are two types of softwares:
System Software

Application Software

System Software
The system software is a collection of programs designed to operate, control and
extend the processing capabilities of the computer itself. System software are
generally prepared by computer manufacturers.

These softwares comprise of programs written in low level languages which


interact with the hardware at a very basic level. System software serves as the
interface between hardware and the end users.

Some examples of system software are Operating System, Compilers, Interpreter,


Assemblers, etc.

Features of System Software are the following:

Close to system.

Fast in speed.

Difficult to design.

Difficult to understand.
Less interactive.

Smaller in size.

Difficult to manipulate.

Generally written in low- level language.

Application Software
Application softwares are the softwares that are designed to satisfy a particular
need of a particular environment. All softwares prepared by us in the computer lab
can come under the category of Application Software.

Application software may consist of a single program, such as a Microsoft's


notepad for writing and editing simple text. It may also consist of a collection of
programs, often called a software package, which work together to accomplish a
task, such as a spreadsheet package.

Examples of Application softwares are the following:

Payroll Software

Student Record Software

Inventory Management Software

Income Tax Software

Railways Reservation Software

Microsoft Office Suite Software

Microsoft Word

Microsoft Excel

Microsoft Powerpoint

4. Explain different types of computer.


Ans:

Computer – Types
Computers can be broadly classified by their speed and computing power.

Sr.
Type Specifications
No.

PC (Personal Single user computer system. Moderately


1
Computer) pow erful microprocessor.

Single user computer system. Similar to Personal


2 WorkStation Computer but have more pow erful
microprocessor.

Multi-user computer system. Capable of


3 Mini Computer
supporting hundreds of users simultaneously.

Multi-user computer system. Capable of


supporting hundreds of users simultaneously.
4 Main Frame
Softw are technology is different from
minicomputer.

An extremely fast computer, w hich can perform


5 Supercomputer
hundreds of millions of instructions per second.

PC (Personal Computer)
A PC can be defined as a small, relatively inexpensive computer designed for an individual
user. PCs are based on the microprocessor technology that enables manufacturers to put
an entire CPU on one chip. Businesses use personal computers for word processing,
accounting, desktop publishing, and for running spreadsheet and database management
applications. At home, the most popular use for personal computers is for playing games
and surfing the Internet.

Although personal computers are designed as single- user systems, these systems are
normally linked together to form a network. In terms of power, nowadays high- end models
of the Macintosh and PC offer the same computing power and graphics capability as low-
end workstations by Sun Microsystems, Hewlett- Packard, and DELL.

WorkStation
Workstation is a computer used for engineering applications (CAD/CAM), desktop
publishing, software development, and other such types of applications, which require a
moderate amount of computing power and relatively high quality graphics capabilities.
Workstations generally come with a large, high- resolution graphics screen, large amount
of RAM, inbuilt network support, and a graphical user interface. Most workstations also
have a mass storage device such as a disk drive, but a special type of workstation, called a
diskless workstation, comes without a disk drive.
Common operating systems for workstations are UNIX and W indows NT. Like PC,
Workstations are also single- user computers. However, workstations are typically linked
together to form a local- area network, although they can also be used as stand- alone
systems.
Minicomputer
It is a midsize computer. A minicomputer is a multi- processing system capable of
supporting from up to 250 users simultaneously
Mainframe
Mainframe is a very large in size and an expensive computer capable of supporting
hundreds, or even thousands, of users simultaneously. Mainframe executes many
programs concurrently. Mainframes support many simultaneous programs execution
Supercomputer
Supercomputers are one of the fastest computers currently available. Supercomputers are
very expensive and are employed for specialized applications that require immense
amounts of mathematical calculations (number crunching). For example, weather
forecasting, scientific simulations, (animated) graphics, fluid dynamic calculations, nuclear
energy research, electronic design, and analysis of geological data (e.g. in petrochemical
prospecting).

5. W hat is algorithm? W rite the characteristics of algorithm.


Ans:
The word Algorithm means a set of finite rules or instructions to be followed in calculations
or other problem-solving operations 
Input: An algorithm has zero or more inputs. Each that contains a fundamental
operator must accept zero or more inputs.

Output: An algorithm produces at least one output. Every instruction that contains
a fundamental operator must accept zero or more inputs.

Definiteness: All instructions in an algorithm must be unambiguous, precise, and


easy to interpret. By referring to any of the instructions in an algorithm one can
clearly understand what is to be done. Every fundamental operator in instruction
must be defined without any ambiguity.

Finiteness: An algorithm must terminate after a finite number of steps in all test
cases. Every instruction which contains a fundamental operator must be terminated
within a finite amount of time. Infinite loops or recursive functions without base
conditions do not possess finiteness.

Effectiveness: An algorithm must be developed by using very basic, simple, and


feasible operations so that one can trace it out by using just paper and pencil.

6. W hat is memory hierarchy of the computer?


Ans:
The Computer memory hierarchy looks like a pyramid structure which is used to describe
the differences among memory types. It separates the computer storage based on
hierarchy.
Level 0: CPU registers
Level 1: Cache memory
Level 2: Main memory or primary memory
Level 3: Magnetic disks or secondary memory

Level 4: Optical disks or magnetic


types or tertiary Memory

In Memory Hierarchy the cost of memory, capacity is inversely proportional to speed. Here
the devices are arranged in a manner Fast to slow, that is form register to Tertiary
memory.
Let us discuss each level in detail:
Level- 0 − Registers
The registers are present inside the CPU. As they are present inside the CPU, they have
least access time. Registers are most expensive and smallest in size generally in kilobytes.
They are implemented by using Flip- Flops.
Level- 1− Cache
Cache Memory is used to store the segments of a program that are frequently accessed
by the processor. It is expensive and smaller in size generally in Megabytes and is
implemented by using Static RAM.
Level- 2 − Primary or Main Memory
It directly communicates with the CPU and with auxiliary memory devices through an I/O
processor. Main memory is less expensive than cache memory and larger in size generally
in Gigabytes. This memory is implemented by using Dynamic RAM.
Level- 3 − Secondary storage
Secondary Storage Devices like Magnetic Disk are present at level 3. They are used as
backup storage. They are cheaper than main memory and larger in size generally in a few
TB.
Level- 4 − Tertiary storage
Tertiary storage devices like magnetic tape are present at level 4. They are used to store
removable files and are the cheapest and largest in size (1- 20 TB).

7. W hat is operator? Define 5 operators with examples.(Bitwise,


cast, unary, logical, assignment)
Ans: An operator is a symbol that tells the compiler to perform specific mathematical or
logical functions. C language is rich in built- in operators and provides the following types
of operators −
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators
Misc Operators
We will, in this chapter, look into the way each operator works.
Arithmetic O perators
The following table shows all the arithmetic operators supported by the C language.
Assume variable A holds 10 and variable B holds 20 then −
Examples :-
Exampl
Operator Description
e
A+B=
+ Adds tw o operands.
30
A−B=
− Subtracts second operand from the first.
-10
A *B=
* Multiplies both operands.
200
B/A =
/ Divides numerator by de-numerator.
2
Modulus Operator and remainder of after B% A=
%
an integer division. 0
Increment operator increases the integer
++ A++ = 11
value by one.
Decrement operator decreases the
- - A- - = 9
integer value by one.

Relational O perators
The following table shows all the relational operators supported by C. Assume
variable A holds 10 and variable B holds 20 then –
Examples :-
Exampl
Operator Description
e
(A ==
Checks if the values of tw o operands are
B) is
== equal or not. If yes, then the condition
not
becomes true.
true.
Checks if the values of tw o operands are
(A != B)
!= equal or not. If the values are not equal,
is true.
then the condition becomes true.
Checks if the value of left operand is (A > B)
> greater than the value of right operand. is not
If yes, then the condition becomes true. true.
Checks if the value of left operand is less
(A < B)
< than the value of right operand. If yes,
is true.
then the condition becomes true.
Checks if the value of left operand is (A >=
greater than or equal to the value of right B) is
>=
operand. If yes, then the condition not
becomes true. true.
Checks if the value of left operand is less
(A <=
than or equal to the value of right
<= B) is
operand. If yes, then the condition
true.
becomes true.

Logical O perators
Following table shows all the logical operators supported by C language. Assume
variable A holds 1and variable B holds 0, then −
Show Examples
Exampl
Operator Description
e
Called Logical AND operator. If both the (A &&
&& operands are non-zero, then the B) is
condition becomes true. false.
Called Logical OR Operator. If any of the
(A || B)
|| tw o operands is non-zero, then the
is true.
condition becomes true.
Called Logical NOT Operator. It is used to
!(A &&
reverse the logical state of its operand. If
! B) is
a condition is true, then Logical NOT
true.
operator w ill make it false.

Bitwise O perators
Bitwise operator works on bits and perform bit- by- bit operation. The truth tables for &, |,
and ^ is as follows −
p q p & q p | q p ^ q
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1
Assume A = 60 and B = 13 in binary format, they will be as follows −
A = 00111100
B = 0000 1101
- - - - - - - - - - - - - - - - -
A&B = 0000 1100
A|B = 00111101
A^ B = 00110001
~A = 1100 0011
The following table lists the bitwise operators supported by C. Assume variable 'A' holds
60 and variable 'B' holds 13, then −
Examples :-
Exampl
Operator Description
e
(A & B)
= 12,
Binary AND Operator copies a bit to the
& i.e.,
result if it exists in both operands.
0000
1100
(A | B)
Binary OR Operator copies a bit if it = 61,
|
exists in either operand. i.e.,
00111101
(A ^ B)
= 4 9,
Binary XOR Operator copies the bit if it is
^ i.e.,
set in one operand but not both.
0011
0001
(~A ) =
Binary One's Complement Operator is ~(60),
~
unary and has the effect of 'flipping' bits. i.e,.
- 0111101
Binary Left Shift Operator. The left A << 2
operands value is moved left by the = 24 0
<<
number of bits specified by the right i.e., 1111
operand. 0000
>> Binary Right Shift Operator. The left A >> 2
= 15
operands value is moved right by the i.e.,
number of bits specified by the right 0000
operand. 1111

Assignment O perators
The following table lists the assignment operators supported by the C language −
Examples :-
Operator Description Example
C=A+
B w ill
Simple assignment operator. Assigns assign
= values from right side operands to left the
side operand value of
A + B to
C
C += A
Add AND assignment operator. It adds is
+= the right operand to the left operand equivale
and assign the result to the left operand. nt to C =
C + A
Subtract AND assignment operator. It C -= A is
subtracts the right operand from the equivale
-=
left operand and assigns the result to nt to C =
the left operand. C - A
Multiply AND assignment operator. It C *= A is
multiplies the right operand w ith the equivale
*=
left operand and assigns the result to nt to C =
the left operand. C * A
Divide AND assignment operator. It C /= A is
divides the left operand w ith the right equivale
/=
operand and assigns the result to the nt to C =
left operand. C / A
C %= A
Modulus AND assignment operator. It is
%= takes modulus using tw o operands and equivale
assigns the result to the left operand. nt to C =
C % A
C <<= 2
is same
<<= Left shift AND assignment operator.
as C = C
<< 2
C >>= 2
is same
>>= Right shift AND assignment operator.
as C = C
>> 2
&= Bitw ise AND assignment operator. C &= 2
is same
as C = C
& 2
C ^ = 2 is
Bitw ise exclusive OR and assignment
^ = same as
operator.
C = C ^ 2
C |= 2 is
Bitw ise inclusive OR and assignment
|= same as
operator.
C = C | 2

Misc O perators sizeof & ternary


Besides the operators discussed above, there are a few other important operators
including sizeof and ? : supported by the C Language.
Examples :-
Operator Description Example
sizeof(a), w here a is
Returns the size of a
sizeof() integer, w ill return
variable.
4 .
&a; returns the
Returns the address of a
& actual address of
variable.
the variable.
* Pointer to a variable. *a;
If Condition is
? : Conditional Expression. true ? then value X :
otherw ise value Y
8. Define all:

a) Keyword
Ans:

A keyw ord is a reserved w ord. You cannot use it as a variable name, constant name, etc. There are
only 32 reserved w ords (keyw ords) in the C language.
A list of 32 keyw ords in the c language is given below :
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 sw itch typedef union unsigned void volatile w hile
We w ill learn about all the C language keyw ords later.

b) Comment

c) Data type

d) Statement
9. W hat is storage class? Define all storage class w ith example.

Ans:

C Storage Classes are used to describe the features of a variable/function. These features
basically include the scope, visibility, and lifetime w hich help us to trace the existence of a
particular variable during the runtime of a program.

10. W hat is control statement? Define all control statements with


examples.

(if- else, switch)


Ans:

if statement in C

In the C programming language, w e use the if statement for conditional branching. It allow s a
program to evaluate if a given condition is true or false and execute a block of code accordingly.
Syntax:
if (condition)
{
// statements to execute if condition is true
}

if- else statement in C


The if- else statement in C is a fundamental control flow structure that allow s for conditional
execution of code. It tests a condition: if the condition is true, one block of code is executed, and if
the condition is false, another block (or none at all) is executed.
Syntax :
if (condition)
{
// statements to execute if condition is true
}
else
{
// statements to execute if condition is false
}
Nested else-if statement in C :
Nested else-if statements in C are a w ay to create more decision-making structures in your code.
They involve using if statements w ithin the branches of other if or else statements.
Syntax :
if (outer condition) {
if (inner condition1) {
// Code for inner condition1
} else if (inner condition2) {
// Code for inner condition2
} else {
// Code for other cases w ithin outer condition
}
} else if (another outer condition) {
// Code for another outer condition
} else {
// Code for cases not covered by any condition
}

Sw itch statement in C
The sw itch statement is a control flow statement in C (and many other programming languages)
that allow s you to choose one of several possible code blocks to execute based on the value of an
expression. It’s often used as a more concise alternative to a series of if- else statements w hen
you need to compare a single value against multiple possible values.
Syntax :
sw itch (expression) {
case value1:
// code w ill be executed if expression equals value1
break;
case value2:
// code w ill be executed if expression equals value2
break;
// additional cases...
default:
// code w ill be executed if none of the above cases match
}
11. W hat is function and recursive function? Define w ith syntax.

Ans:

12. W hat is file handling? Write some function using file handling.

Ans:

File handing in C is the process in w hich w e create, open, read, w rite, and close operations
on a file. C language provides different functions such as fopen(), fw rite(), fread(), fseek(),
fprintf(), etc. to perform input, output, and many different C file operations in our program.

Types of Files in C
A file can be classified into tw o types based on the w ay the file stores the data. They are as
follow s:
Text Files
Binary Files

Text Files
A text file contains data in the form of ASCII characters and is generally used to store a stream of
characters.
Each line in a text file ends w ith a new line character (‘ \n’).
It can be read or w ritten by any text editor.
They are generally stored w ith .txt file extension.
Text files can also be used to store the source code.
2. Binary Files
A binary file contains data in binary form (i.e. 0’s and 1’s) instead of ASCII characters. They contain
data that is stored in a similar manner to how it is stored in the main memory.
The binary files can be created only from w ithin a program and their contents can only be
read by a program.
More secure as they are not easily readable.
They are generally stored w ith .bin file extension.

C File O perations
C file operations refer to the different possible operations that w e can perform on a file in C such
as:
1. Creating a new file – fopen() w ith attributes as “a” or “a+” or “ w ” or “ w +”
2. Opening an existing file – fopen()
3. Reading from file – fscanf() or fgets()
4. Writing to a file – fprintf() or fputs()
5. Moving to a specific location in a file – fseek(), rew ind()
6. Closing a file – fclose()
File Pointer in C
A file pointer is a reference to a particular position in the opened file. It is used in file handling to
perform all file operations such as read, w rite, close, etc. We use the FILE macro to declare the file
pointer variable. The FILE macro is defined inside <stdio.h> header file.
Syntax of File Pointer
FILE* pointer_name;
File Pointer is used in almost all the file operations in C.
O pen a File in C
For opening a file in C, the fopen() function is used w ith the filename or file path along w ith the
required access modes.
Syntax of fopen()
File Pointer in C
A file pointer is a reference to a particular position in the opened file. It is used in file handling to
perform all file operations such as read, w rite, close, etc. We use the FILE macro to declare the file
pointer variable. The FILE macro is defined inside <stdio.h> header file.
Syntax of File Pointer
FILE* pointer_name;
File Pointer is used in almost all the file operations in C.
O pen a File in C
For opening a file in C, the fopen() function is used w ith the filename or file path along w ith the
required access modes.
Syntax of fopen()
Opening
Modes Description
Searches file. If the file is opened successfully fopen( ) loads it into memory and sets
r up a pointer that points to the first character in it. If the file cannot be opened
fopen( ) returns NULL.
rb Open for reading in binary mode. If the file does not exist, fopen( ) returns NULL.
Open for w riting in text mode. If the file exists, its contents are overw ritten. If the
w
file doesn’t exist, a new file is created. Returns NULL, if unable to open the file.
Open for w riting in binary mode. If the file exists, its contents are overw ritten. If the
w b
file does not exist, it w ill be created.
Searches file. If the file is opened successfully fopen( ) loads it into memory and sets
a up a pointer that points to the last character in it. It opens only in the append mode.
If the file doesn’t exist, a new file is created. Returns NULL, if unable to open the file.
Open for append in binary mode. Data is added to the end of the file. If the file
ab
does not exist, it w ill be created.
Searches file. It is opened successfully fopen( ) loads it into memory and sets up a
r+
pointer that points to the first character in it. Returns NULL, if unable to open the file.
Open for both reading and w riting in binary mode. If the file does not exist, fopen( )
rb+
returns NULL.
Searches file. If the file exists, its contents are overw ritten. If the file doesn’t exist a
w +
new file is created. Returns NULL, if unable to open the file.
Open for both reading and w riting in binary mode. If the file exists, its contents are
w b+
overw ritten. If the file does not exist, it w ill be created.
Searches file. If the file is opened successfully fopen( ) loads it into memory and sets
a+
up a pointer that points to the last character in it. It opens the file in both reading
and append mode. If the file doesn’t exist, a new file is created. Returns NULL, if
unable to open the file.
Open for both reading and appending in binary mode. If the file does not exist, it
ab+
w ill be created.

Reading From a File


The file read operation in C can be performed using functions fscanf() or fgets(). Both the
functions performed the same operations as that of scanf and gets but w ith an additional
parameter, the file pointer. There are also other functions w e can use to read from a file. Such
functions are listed below :
Function Description
fscanf() Use formatted string and variable arguments list to take input from a file. 
fgets() Input the w hole line from the file.
fgetc() Reads a single character from the file.
fgetw () Reads a number from a file.
fread() Reads the specified by tes of data from a binary file.
W rite to a File
The file w rite operations can be performed by the functions fprintf() and fputs() w ith similarities
to read operations. C programming also provides some other functions that can be used to w rite
data to a file such as:
Function Description
Similar to printf(), this function use formatted string and varible arguments list to
fprintf()
print output to the file.
fputs() Prints the w hole line in the file and a new line at the end.
fputc() Prints a single character into the file.
fputw () Prints a number to the file.
fw rite() This functions w rite the specified amount of by tes to the binary file.
Closing a File
The fclose() function is used to close the file. After successful file operations, you must alw ays
close a file to remove it from the memory.
Syntax of fclose()
fclose(file_pointer);
fseek() in C
If w e have multiple records inside a file and need to access a particular record that is at a specific
position, so w e need to loop through all the records before it to get the record. Doing this w ill
w aste a lot of memory and operational time. To reduce memory consumption and operational
time w e can use fseek() w hich provides an easier w ay to get to the required data. fseek() function
in C seeks the cursor to the given record in the file.
Syntax for fseek()
int fseek(FILE *ptr, long int offset, int pos);
rewind() in C
The rew ind() function is used to bring the file pointer to the beginning of the file. It can be used in
place of fseek() w hen you w ant the file pointer at the start.
Syntax of rew ind()
rew ind (file_pointer);
Function Description
s
fopen() It is used to create a file or to open a file.
fclose() It is used to close a file.
fgets() It is used to read a file.
fprintf() It is used to w rite blocks of data into a file.
fscanf() It is used to read blocks of data from a file.
getc() It is used to read a single character to a file.
putc() It is used to w rite a single character to a file.
fseek() It is used to set the position of a file pointer to a mentioned location.
ftell() It is used to return the current position of a file pointer.
rew ind() It is used to set the file pointer to the beginning of a file.
putw () It is used to w rite an integer to a file.
getw () It is used to read an integer from a file.

13. W hat are macros? Define conditional and null conditional macros.

Ans:

C Preprocessor and Macros

Working of C Preprocessor

The C preprocessor is a macro preprocessor (allow s you to define macros) that transforms your
program before it is compiled. These transformations can be the inclusion of header files, macro
expansions, etc.
All preprocessing directives begin w ith a # symbol. For example,
#define PI 3.14
Some of the common uses of C preprocessors are:

Including Header Files: #include


The #include preprocessor is used to include header files to C programs. For example,
#include <stdio.h>
Here, stdio.h is a header file. The #include preprocessor directive replaces the above line w ith the
contents of stdio.h header file.
That's the reason w hy you need to use #include <stdio.h> before you can use functions
like scanf() and printf().
You can also create your ow n header file containing function declaration and include it in your
program using this preprocessor directive.

#include "my_header.h"
Visit this page to learn more about using header files.

Macros using #define


A macro is a fragment of code that is given a name. You can define a macro in C using
the #define preprocessor directive.
Here's an example.

#define c 2997924 58 // speed of light


Here, w hen w e use c in our program, it is replaced w ith 2997924 58.

Example 1: #define preprocessor

#include <stdio.h>
#define PI 3.14 15

int main()
{
float radius, area;
printf("Enter the radius: ");
scanf("%f", &radius);

// Notice, the use of PI


area = PI*radius*radius;

printf("Area=% .2f",area);
return 0;
}

Function like Macros


You can also define macros that w ork in a similar w ay as a function call. This is know n as function-
like macros. For example,

#define circleArea(r) (3.14 15*(r)*(r))


Every time the program encounters circleArea(argument), it is replaced
by (3.14 15*(argument)*(argument)).
Suppose, w e passed 5 as an argument then, it expands as below :

circleArea(5) expands to (3.14 15*5*5)

Example 2: Using #define preprocessor

#include <stdio.h>
#define PI 3.14 15
#define circleArea(r) (PI*r*r)

int main() {
float radius, area;

printf("Enter the radius: ");


scanf("%f", &radius);
area = circleArea(radius);
printf("Area = % .2f", area);

return 0;
}
Visit this page to learn more about macros and #define preprocessor.
Conditional Compilation
In C programming, you can instruct the preprocessor w hether to include a block of code or not. To
do so, conditional directives can be used.

It's similar to a if statement w ith one major difference.


The if statement is tested during the execution time to check w hether a block of code should be
executed or not w hereas, the conditionals are used to include (or skip) a block of code in your
program before execution.

Uses of Conditional

use different code depending on the machine, operating system

compile the same source file in tw o different programs

to exclude certain code from the program but to keep it as a reference for future purposes

How to use conditional?

To use conditional, #ifdef, #if, #defined, #else and #elif directives are used.

#ifdef Directive

#ifdef MACRO
// conditional codes
#endif
Here, the conditional codes are included in the program only if MACRO is defined.
#if, #elif and #else Directive

#if expression
// conditional codes
#endif
Here, expression is an expression of integer type (can be integers, characters, arithmetic
expression, macros, and so on).
The conditional codes are included in the program only if the expression is evaluated to a non-zero
value.
The optional #else directive can be used w ith #if directive.
#if expression
conditional codes if expression is non-zero
#else
conditional if expression is 0
#endif
You can also add nested conditional to your #if...#else using #elif
#if expression
// conditional codes if expression is non-zero
#elif expression1
// conditional codes if expression is non-zero
#elif expression2
// conditional codes if expression is non-zero
#else
// conditional if all expressions are 0
#endif

#defined

The special operator #defined is used to test w hether a certain macro is defined or not. It's often
used w ith #if directive.
#if defined BUFFER_SIZE && BUFFER_SIZE >= 204 8
// codes

Predefined Macros
Here are some predefined macros in C programming.

Macro Value
__DATE__ A string containing the current date.

__FILE__ A string containing the file name.

__LINE__ An integer representing the current line number.

__STDC__ If follow s ANSI standard C, then the value is a nonzero integer.

__TIME__ A string containing the current time.

14. W hat is dynamic memory allocation? Define:

a) Calloc()

b) Malloc()

c) Alloc()

d) Free()

Ans:

15. W hat is precedence and associatively?


Ans:
O perator Precedence and Associativity Table
The following tables list the C operator precedence from highest to lowest and the
associativity for each of the operators:
Operator
Precedence Associativit
  Description y 
() Parentheses (function call)

[] Array Subscript (Square Brackets)


Left- to-
1 . Dot Operator
Right
-> Structure Pointer Operator

++ , — Postfix increment, decrement

++ / — Prefix increment, decrement

+ / – Unary plus, minus


Right- to-
2 ! , ~ Logical NOT, Bitw ise complement
Left
(type) Cast Operator

* Dereference Operator
& Addressof Operator

sizeof Determine size in by tes


Left- to-
3 *, /,% Multiplication, division, modulus
Right
Left- to-
4 +/- Addition, subtraction
Right
Left- to-
5 << , >> Bitw ise shift left, Bitw ise shift right
Right

< , <= Relational less than, less than or equal to


Left- to-
6 Relational greater than, greater than or Right
> , >= equal to
Left- to-
7 == , != Relational is equal to, is not equal to
Right
Left- to-
8 & Bitw ise AND
Right
Left- to-
9 ^ Bitw ise exclusive OR
Right
Left- to-
10 | Bitw ise inclusive OR
Right
Left- to-
11 && Logical AND
Right
Left- to-
12 || Logical OR
Right
Right- to-
13 ?: Ternary conditional
Left

= Assignment

+= , -= Addition, subtraction assignment

*= , /= Multiplication, division assignment Right- to-


14
Modulus, bitw ise AND assignment Left
%= , &=
^ = , |= Bitw ise exclusive, inclusive OR assignment

<<=, >>= Bitw ise shift left, right assignment


Left- to-
15 , comma (expression separator)
Right
O perator Precedence in C
Operator precedence determines w hich operation is performed first in an expression w ith more
than one operator w ith different precedence.
16. W rite the difference between:

a) Compiler & interpreter


Ans:
Difference Between Compiler and Interpreter
Compiler Interpreter
 
Steps of Programming:
Program Creation.
Analysis of language by the compiler and
Steps of Programming:
throw s errors in case of any incorrect
Program Creation.
statement.
Linking of files or generation of Machine Cod
In case of no error, the Compiler converts
Interpreter.
the source code to Machine Code.
Execution of source statements one by one.
Linking of various code files into a runnable
program.
Finally runs a Program.
The compiler saves the Machine Language in form
The Interpreter does not save the Machine Languag
of Machine Code on disks.
Compiled codes run faster than Interpreter. Interpreted codes run slow er than Compiler.
Linking-Loading Model is the basic w orking model
The Interpretation Model is the basic w orking mode
of the Compiler.
The compiler generates an output in the form of
The interpreter does not generate any output.
(.exe).
Any change in the source program after the Any change in the source program during the transl
compilation requires recompiling the entire code. retranslation of the entire code.
Errors are displayed in Compiler after Compiling
Errors are displayed in every single line.
together at the current time.
The compiler can see code upfront w hich helps in
The Interpreter w orks by line w orking of Code, that’
running the code faster because of performing
slow er compared to Compilers.
Optimization.
It does not require source code for later execution. It requires source code for later execution.

Execution of the program takes place only after


Execution of the program happens after every line is
the w hole program is compiled.

Compilers more often take a large amount of time


In comparison, Interpreters take less time for analyzi
for analyzing the source code.

CPU utilization is more in the case of a Compiler. CPU utilization is less in the case of a Interpreter.
The use of Compilers mostly happens in The use of Interpreters is mostly in Programming an
Production Environment. Environments.

Object code is permanently saved for future use. No object code is saved for future use.

C, C++, C#, etc are programming languages that Py thon, Ruby, Perl, SNOBOL, MATLAB, etc are progra
are compiler-based. interpreter-based.

b) while & do- while

Ans:
Difference Between while and do - while
while do-while
Entry Control Loop Exit Control Loop
First checking the condition, if true then Without checking the condition, In first time it
do the block w ork. must do the block w ork.
Not terminated by “ ; ” Must terminated by “ ; ”
Not use “ do ” Use “ do ”
Syntax: Syntax:
w hile (condition) { do{
statement 1; statement 1;
statement 2; statement 2;
...... ......
. . . . . . . . . . . .
Statement n; Statement n;
Increment or decrement or update Increment or decrement or update
} }w hile(condition);

c) RAM & RO M

Ans:

Difference Between RAM and RO M


RAM ROM
Random Access Memory Read Only Memory
It is temporary memory It is permanent memory
It is volatile memory It is non –volatile memory
Data can be change or deleted in Instructions w ritten in ROM cannot be
RAM changed
Large size w ith higher capacity Small size w ith less capacity
It is expensive It is cheaper than RAM

d) if & switch
Ans:
Difference Between If and Switch
If Switch
It can check only one case, not multiple
We can check multiple cases in one time
case
We can pass condition also We pass only expression, not condition
Use “else” Use “default”
It do not use “case” It use “case”
Syntax: Syntax:
if (condition) sw itch (expression) {
{ case value1:
// statements to execute statement 1;
if condition is true break;
} case value2:
else statement 2;
{ break;
// statements to execute . . . . . . . . .
if condition is false . . . . . . . . .
} case n;
statement n;
break;
default:
// code w ill be executed
if none of the above cases match
}

e) Struct & Union

f) Array & structure

17. W hat is pointer? Define with example.


18. W hat is loop? Define all loops with syntax and example.(while,do-
while,for)

Ans. Loop –It means rotating any block till the condition is true.
There are three types of loops:-

(i) W hile loop : - It is a loop which is used for rotating any block. In this loop
first check the condition, If the condition is true then go to the block
and do all the statement. In the last statement do the increment or
decrement or update. After that again checking the condition. If true
then again do all the above work otherwise comeback from the loop.

Syntax:
w hile (condition) {
statement 1;
statement 2;
......
. . . . . .
Statement n;
Increment or decrement or update
}

Example: To find the factorial of any number.


#include <stdio.h>

#include <conio.h>

void main(){

int n, f= 1;

clrscr();

printf("enter any number : ");

scanf("% d", &n);

w hile(n > 0){

f *= n;

n- -;

}
printf("Factorial is : % d", f);

getch();

}

(ii) For loop : - It is a loop which is used for rotating any block. In this loo, it
has three parts:-

(a) Initialization

(b) Condition

(c) Increment or decrement

All these parts are separated by “ ; ”. In this loop first initialize the variable
after that checking the condition. If the condition is true then go to the
block and do all the statement after that do increment or decrement
and checking the condition, if the condition is true, then again do the
block work otherwise comeback from the loop

Syntax:
for (initialization; condition; increment or decrement) {
statement 1;
statement 2;
......
. . . . . .
Statement n;
}
Example: To find the factorial of any number.
#include <stdio.h>

#include <conio.h>

void main(){

int n, f= 1;

clrscr();

printf("enter any number : ");

scanf("% d", &n);

for(int i = 1;i<=n;i++){

f*=i;

}

printf("Factorial is : % d", f);

getch();

}

(iii) do - W hile loop : - It is a loop which is used for rotating any block. In this
loop first do the statement of the block. In the second time, it
checking the condition, if true then again do the block work,
otherwise comeback from the loop.

NO TE:- In the first time, it must do the block work

Syntax:
do{
statement 1;
statement 2;
......
. . . . . .
Statement n;
Increment or decrement or update
} w hile (condition);

Example: To find the factorial of any number.


#include <stdio.h>

#include <conio.h>

void main(){

int n, f= 1;

clrscr();

printf("enter any number : ");

scanf("% d", &n);

do{

f *= n;

n- -;

}w hile(n>0);

printf("Factorial is : % d", f);

getch();

}
19. W hat is link list? Define all type of link list.

20. W hat is string? Define four string functions with example.

21. Draw a flowchart for:

a) Armstrong

b) Prime number

c) Greater number

PRO GRAMS:

1. W rite a program to find the greatest between 5 numbers by using


conditional operator.

Ans:
#include <stdio.h>

#include <conio.h>

void main(){

int a,b,c,d,e;
clrscr();

printf("enter five numbers : ");

scanf("%d% d% d% d% d", &a, &b, &c, &d, &e);

a = (a>b) ? a : b;

a = (a>c) ? a : c;

a = (a>d) ? a : d;

a = (a>e) ? a : e;

printf("Greater number is % d", a);

getch();

return 0;

}

2. W rite a program to check between 3 numbers which number is greater


by using nested if.

Ans:
#include <stdio.h>

#include <conio.h>

void main(){

int x, y, z;

printf("enter any three numbers : ");

scanf("%d% d% d", &x, &y, &z);

if(x>y){

if(x>z){

printf("% d is greater", x);

}
else{

printf("% d is greater", z);

}

}

else{

if(y>z){

printf("% d is greater", y);

}

else{

printf("% d is greater", z);

}

}

getch();

return 0;

}

3. W rite a program to check whether entered character is vowel or not.

Ans:

# include <stdio.h>
#include <conio.h>

void main(){

char ch;

clrscr();

printf("enter any character : ");

scanf("% c", &ch);

sw itch (ch){
case 'a':

case 'A':

case 'e':

case 'E':

case 'i':

case 'I':

case 'o':

case 'O':

case 'u':

case 'U':

printf("Vow el");

break;

default:

printf("Not Vow el");

}

getch();

return 0;

}

4. W rite a program to find the area of square, circle, rectangle and triangle.
Ans :
#include <stdio.h>
#include <conio.h>

void main(){

int ch;

float a,b;
clrscr();

printf("[1]- - - -> rectangle\n");

printf("[2]- - - -> circle\n");

printf("[3]- - - -> triangle\n");

scanf("% d", &ch);

printf("Enter the tw o number : ");

scanf("%f %f", &a, &b);

sw itch (ch){

case 1:

a = a * b;

printf("Area = % .1f", a);

break;

case 2:

a = (22 / 7) * a * a;

printf("Area = % .1f", a);

break;

case 3:

a = (a * b) / 2;

printf("Area = % .1f", a);

break;

default:

printf("Invalid! Try Again");

}

getch();

}

5. W hat is call by value or call by reference? Define with swapping program.

6. W rite a program to enter any digit number and check it is Armstrong or


not.

Ans:
#include <stdio.h>
#include <conio.h>

#include <math.h>

void main(){

int n, m, t, k, c = 0, s = 0;

clrscr();

printf("Enter any digit number : ");

scanf("% d", &n);

t = m = n;

w hile(t > 0){

c++;

t = t / 10;

}

w hile(m > 0){

k = m%10;

s = s + pow(k,c);

k = k / 10;

}

(s == n) ? printf("Armstrong!") : printf("Not Armstrong!");

getch();
}

7. W rite a program to enter 4 digit numbers and reverse it.


Ans:
#include <stdio.h>
#include <conio.h>

void main(){

int n, k, s = 0;

clrscr();

printf("Enter any 4 - digit number : ");

scanf("% d", &n);

w hile(n > 0){

k = n % 10;

s = s * 10 + k;

n = n / 10;

}

printf("Reversed Number is % d", s);

getch();

}

8. W rite a program to count all prime numbers from 1- 100.

Ans:
#include <stdio.h>
#include <conio.h>

void main(){

int n = 100, i, j, k = 0, c = 0;

clrscr();
for(i = 1;i<=n;i++){

c = 0;

for(j = 1;j<=i;j++){

if(i % j == 0){

c++;

}

if(c == 2){

k++;

}

}

printf("Total prime numbers betw een 1to 100 is % d", k);

getch();

}

9. W rite a program to find sum of series:

a)

b)

c) ……

(i)
#include <stdio.h>
#include <conio.h>

#include <math.h>
void main()

{

int n, i, f = 1, p;

float s = 0;

clrscr();

printf("Enter any number: ");

scanf("% d", &n);

for (i = 0; i <= n; i++)

{

p = pow(i, i);

f *= i;

s = s + (float)p / f;

}

printf("Sum = % .1f", s);

getch();

}

(iii)
#include <stdio.h>
#include <conio.h>

void main(){

int n, k = 0, s = 0;

clrscr();

printf("Enter any number: ");

scanf("% d", &n);


w hile (n > 0){

k = k * 10 + 1;

s += k;

n- -;

}

printf("Sum = % d", s);

getch();

}

10. W rite a program to print the pyramid:

a) 1

2 3

4 5 6

7 8 9 10

1112 13 14 15
#include <stdio.h>
#include <conio.h>

void main(){

int n = 5, i, j, k = 1;

clrscr();

for (i = 1; i <= n; i++){

for (j = 1; j <= i; j++){

printf("% d ", k);


k++;

}

printf("\n");

}

getch();

}

Ans:
b) 5

5 4
5 4 3

5 4 3 2
5 4 3 2 1

Ans:
#include <stdio.h>
#include <conio.h>

void main(){

int n = 5, i, j;

clrscr();

for (i = n; i >= 1; i- -){

for (j = n; j >= i; j- -){

printf("% d ", j);

}

printf("\n");

}

getch();

}

c) 1

10

10 1
10 10

10 10 1

Ans:
#include <stdio.h>
#include <conio.h>

void main(){

int n = 5, i, j;

clrscr();

for (i = 1; i <= n; i++){

for (j = 1; j <= i; j++){

if (j % 2 == 0)

printf("% d ", 0);

else

printf("% d ", 1);

}

printf("\n");

}

getch();

}

d) 5 5 5 5 5

4 4 4 4

3 3 3

2 2

1

Ans:
#include <stdio.h>
#include <conio.h>

void main(){
int n = 5, i, j;

clrscr();

for (i = n; i >= 1; i- - ){


for (j = 1; j <= i; j++){

printf("% d ", i);

}

printf("\n");

}

getch();

}

e) +

+ -

+ - +

+ - + -

+ - + - +

Ans:
#include <stdio.h>
#include <conio.h>

void main(){

int n = 5, i, j;

clrscr();

for (i = 1; i <= n; i++){

for (j = 1; j <= i; j++){

if (j % 2 == 0)

printf("- ");

else

printf("+ ");

}

printf("\n");

}

getch();

}

f) I

I I

I I M

I I M T

Ans:
#include <stdio.h>
#include <conio.h>

#include <string.h>

void main(){

int i, j;
char n[] = "IIMT";

clrscr();

for (i = 0; i < strlen(n); i++){

for (j = 0; j <= i; j++){

printf("%c", n[j]);

}

printf("\n");

}

getch();

}

11. W rite a program to sort the list in ascending order by:

i. Selection

ii. Bubble

iii. Insertion

Ans: (i) Selection Sort


#include <stdio.h>
#include <conio.h>

void main()

{

int i, j, temp;

int arr[] = {10, 9, 5, 1, 2, 3, 6};

int n = sizeof(arr) / sizeof(arr[0]);

clrscr();

for (i = 0; i < n; i++)

{

for (j = i + 1; j < n; j++)

{

if (arr[i] > arr[j])

{

temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

}

}

}

printf("Sorted array is\n");

for (i = 0; i < n; i++)

{

printf("%d ", arr[i]);


}

getch();

}

(ii) Bubble Sort

#include <stdio.h>
#include <conio.h>

void main()

{

int i, j, temp;

int arr[] = {10, 9, 5, 1, 2, 3, 6};

int n = sizeof(arr) / sizeof(arr[0]);

clrscr();
for (i = 0; i < n; i++)

{

for (j = 0; j < n - i - 1; j++)


{

if (arr[j] > arr[j + 1])


{

temp = arr[j];

arr[j] = arr[j + 1];

arr[j + 1] = temp;

}

}

}

printf("Sorted array is\n");

for (i = 0; i < n; i++)

{

printf("% d ", arr[i]);

}

getch();

}

(iii) Insertion Sort


#include <stdio.h>
#include <conio.h>

void main() {

int arr[] = {10, 11, 16, 17, 2, 7, 1, 8, 5, 19, 3};

int len = sizeof(arr) / sizeof(arr[0]);

int i,j, key;

clrscr();

for(i = 0;i<len;i++){

key = arr[i];

j = i-1;

while(j>=0 && key < arr[j]){

arr[j+1] = arr[j];

j- -;

}

arr[j+1] = key;

}

printf("Sorted array : \n");

for(i = 0;i<len;i++){

printf("%d ", arr[i]);

}

getch();

}
rd
12. W rite a program to enter two 3x3 matrix and multiply in 3 matrix.

13. W rite a program to enter any 3x3 matrix and transpose it.
14. W rite a program to search any value from the list. linear search ,binary
search

Ans:

Linear Search
#include <stdio.h>
#include <conio.h>

void main()

{

int arr[] = {10, 5, 6, 2, 1, 15, 8, 9, 7};

int len = sizeof(arr) / sizeof(arr[0]);

int n, i, x = 0;

clrscr();

printf("Enter the number to find in the array : ");

scanf("%d", &n);

for (i = 0; i < len; i++)

{

if (arr[i] == n)

{
x = 1;

break;

}

}

if (x == 1)

printf("Present");

else

printf("Not Present");

getch();

}

Binary Search
#include <stdio.h>
#include <conio.h>

void main()

{

int arr[] = {10, 11, 16, 17, 2, 7, 1, 8, 5, 15, 12, 18, 4 , 20, 19, 3};

int len = sizeof(arr) / sizeof(arr[0]);

int i, j, v, l, h, x = 0;

int n = printf("enter the number : ");

scanf("% d", &n);


clrscr();

for (i = 0; i < len; i++){

for (j = i + 1; j < len; j++){

if (arr[i] > arr[j]){

v = arr[i];

arr[i] = arr[j];

arr[j] = v;

}

}

}

l = 0;

h = len - 1;

w hile (l <= h){

v = (l + h) / 2;

if (arr[v] == n){

x = 1;

break;

}

else if (n < arr[v]){

h = v - 1;

}

else{

l = v + 1;

}

}

if (x == 1)
printf("Present");

else

printf("Not Present");

getch();

}

15. W rite a program to convert decimal to binary.

Ans:
#include <stdio.h>
#include <conio.h>

void main()

{

int n, i = 0, k, arr[10];

clrscr();

printf("Enter any number : ");

scanf("%d", &n);

while (n > 0)

{

k = n % 2;

arr[i] = k;

i++;

}

i- -;

printf("Binary Number = ");


for (k = i; k >= 0; k- -)

{

printf("%d", arr[k]);

}

getch();

}

16. W rite a program to check if entered string is palindrome or not.

17. W rite a program to copy the content of abc.txt to xyz.txt.

18. W rite a program to write all squares between 1to 10 inside the file and
read it.

19. W rite a program to find Fibonacci series by using recursive function.

20. W rite a program to find factorial by using recursive function.

You might also like