Documente Academic
Documente Profesional
Documente Cultură
2013)
Q January 14 May 14 January 15 May 15
1. Adv - 5th generation computers (110100111101)2 = (?)8 What is Flow chart? What is super computer?
2. What is pseudo code? Flowchart for biggest of 2 number Define algorithm Define pseudo code
3. What are operators in C? What are data types in C? What is a variable? Define keywords
4. Print from 10 to 1 Program for factorial What is ternary operator? Write any 4 I/O statements
5. What is array? What is 2D array? Declare float array with 5values Define array
6. Write 4 string handling functions Write 4 string handling functions What is string array? What is character string
7. Define functions Define recursion Define functions Actual and formal parameter?
8. Define pointer call by value/call by reference What is address operator? Define pointer
9. Define pre-processor What is the Purpose of union? What is static storage class? Define structure
10. Difference b/w struct & union What are pre-processor directives? What is #define pre-processor? Define pre-processor
12a Structure of c program ex: calculator(8) Explain operators in C (16) Explain operators in C (10)
Short notes about Branching(8) Datatypes in c(6)
12b Looping in C with examples(16) Branching in C(16) Input and output statements in c(10)
Cprog- sum of n numbers
13a Matrix multiplication (8) C program- maximum and smallest C prog-count vowels(6)
Determinant of matrix(8) element in the array(16) Matrix multiplication (10)
15a Structure datatype and problems of using Explain structure with example(16) C prog-Structure –student details
structure(8)
Structure declaration(8)
15b Explain storage class (16) Explain static storage class(4) Explain storage class(8)
Explain union(4), pre-processor(8)
pre-processor(8)
**GE6151 – COMPUTER PROGRAMMING**
UNIT 1 – INTRODUCTION TO COMPUTERS
1. What is a computer?
The computer is an electronic machine that takes input from the user and processes it to generate the output in the
form of useful information.
2. What is a program?
A program is a set of instructions that can be executed one-by-one in the computer.
Each program performs certain task that is created by the user.
3. What is Hardware and Software?
Hardware of the computer means, all the visible components of a computer that are needed for proper functioning.
It is the framework. Software is applied on the hardware of the computer. E.x: CPU, monitor, etc
Software of the computer means, the set of programs that can be executed one by one in a computer.
It controls all the operations of a computer. Ex: OS, games softwares, etc
4. What is the difference between application software and system software?
System softwares are inbuilt softwares that are required to start the computer.
Without system software, a computer cannot work properly. Ex : Windows OS, Linux OS, DOS, etc
Application softwares are optional softwares that are needed to perform certain task needed for user.
Without application software, a system can work. Ex: Tally, Games softwares, etc
5. What are the advantages and disadvantages of first generation of computers?
Advantages: Disadvantages:-
Based on vacuum tube technology Large space is needed.
During that time, these type of computers It takes more power
were the fastest. Very costly
Calculations are done in milliseconds. No reliability
Programming is difficult
PART – B
Page 3 of 11
“There is no shortcut for success; Work hard and get success” – Sachin Tendulkar
**GE6151 – COMPUTER PROGRAMMING**
Education:-
It is used in education, online classrooms, online library, etc
Marketing:-
It is used in preparing advertisements
Banking:-
It is used in banking to store the account details of customers
It is used in ATM, internet banking
Video games:-
It is used in playing video games, online games, etc
Training:-
Computers are used to give training to the workers
Hospitals:-
It is used in hospitals to help the doctors to find the disease and give treatment
It stores the record of patients Ex: Scan, blood test, ECG, etc
Railways:-
It is used in online ticket reservation, see the schedule of trains, etc
Science:-
It is used in research by scientists
It helps in finding earth quake, Tsunami, etc
3. Explain the generation of Digital computers in detail. (or) Explain the Generation of computers in detail.
First Generation:-
The first generation of computers were used only for certain task.
Each computer has a machine language ( 0’s and 1’s)
They are difficult to process
They are not versatile, they have limited speed.
They used VACUUM TUBES and MAGNETIC DRUMS for data storage.
It consumed 100kW power, weight of many 1000kilos.
It performed 5000calculations per second.
Ex: EDSAC, UNIVAC, etc
Advantages: Disadvantages:-
Based on vacuum tube technology Large space is needed.
During that time, these type of computers It takes more power
were the fastest. Very costly
Calculations are done in milliseconds. No reliability
Programming is difficult
They are used in business, university, government, etc because of its success.
They used TRANSISTORS for memory storage
They are small and consume less power
Thermionic valves are used instead of heated tubes
They also contained components such as printers, tape, disk storage, memory, OS, etc
Ex: IBM 1401, 1620, UNIVAC 1108
Advantages: Disadvantages:-
Based on transistor technology Very costly
Small in size It needed AC
Less heat produced Production was difficult
Calculations are done in microseconds
Better reliability
Page 4 of 11
“There is no shortcut for success; Work hard and get success” – Sachin Tendulkar
**GE6151 – COMPUTER PROGRAMMING**
Third generation of computers:-
Integrated Chip (IC) Technology is used for memory storage
Quartz rock is used to remove the heat.
IC is a combination of three components on a small disc
Semiconductor was also introduced.
Because ICs are small in size, the size of the computers became small
In this generation of computers, many operating systems can be installed.
Ex: IBM 360 series, ICL 1900 series, CDC 1700
Advantages: Disadvantages:-
Based on IC (Integrated Chip) technology It needed AC
Calculations are done in nano seconds Production was difficult
Less power needed
Less maintenance cost
More reliability
Page 5 of 11
“There is no shortcut for success; Work hard and get success” – Sachin Tendulkar
**GE6151 – COMPUTER PROGRAMMING**
Characteristics of analog computers:-
Low speed
Limited applications
Less memory space
Not suitable for business
Output is not accurate
Digital Computers:-
They process the information digitally.
It stored and processes the data in digital form
Data is stored as 0’s and 1’s
Output produced by digital computers is in Digital form.
Ex: Palmtop, Laptop, Super computer, etc
Characteristics of a digital comouter:-
Large memory space
Suitable for business applications
Processor speed is high
Versatile in nature
Super Computers:-
They are most powerful computers than mainframe computers
The simplified form of mainframe computer is called as super computer
It is the top level computer used for high processing capacity
It can process Trillions of instructions per second ( 10,00,000 = 1million, 10,00,00,000=1trillion)
Applications of super computers:-
This computer cannot be used at home
It is used in heavy jobs such as Government use, etc
They are useful in weather forecasting.
Advantages – super computers Disadvantages – Super computers
Processing speed is : trillions per second Its size is large
Many processors can work at the same time It is also very costly
It has more main memory
Page 6 of 11
“There is no shortcut for success; Work hard and get success” – Sachin Tendulkar
**GE6151 – COMPUTER PROGRAMMING**
Cache memory
Registers
CPU
ALU
Control Unit
Basic operations of a computer:-
It ACCEPTS the data or instruction through INPUT
It STORES the data or information
It can PROCESS data as instructed by the user
It GIVES results through OUTPUT
It CONTROLS all the operations
Input Unit:-
Input devices get the data from the user and converts to the machine understandable form.
Ex: Keyboard, Mouse, Scanner, Joystick, Lightpen, Card reader, Webcam, Microphone
Keyboard:-
It is a standard input device attached to all type of computers
It contains keys arranged in the form of QWERTY
It contains many keys such as TAB, CAPSLOCK, SPACE BAR, ALT, CTRL, ENTER, HOME, END, etc
It contains 101 to 104 keys
If we press the keys in the keyboard, electrical signals are sent to the computer.
Mouse:-
It is used with personal computer
Old type of mouses have magnetic ball at the back.
Nowadays, Infrared mouses are used, that works on infrared light at the back of the mouse
We can move cursor to any direction with the help of a mouse
It is used for easy and fast operations.
Scanner:-
Keyboard can give input only the characters
Scanners can give a picture as input to the computer
Scanner is an optical device that takes a picture and gives as input to the computer
Ex: MICR, OMR, OCR
MICR – Magnetic Ink Character Recognition
They are used in bank cheques
Magnetic ink is printed on the cheque. If the cheque is passed through magnetic field, the information is read
OMR – Optical Mark reader
They are used in objective type tests
Students have to shade the rounds for the correct answer
OCR – Optical Character Recognition
Reads a paper and takes the information from it, gives as input to the computer
They are costly than MICR
Joystick:-
It is an online device
We can move the cursor fastly with the help of joystick
They are used to play games, etc
Page 7 of 11
“There is no shortcut for success; Work hard and get success” – Sachin Tendulkar
**GE6151 – COMPUTER PROGRAMMING**
Light Pen:-
It is a light-sensitive device
It is used with CRT monitor
It is used in CAD
Using light, it moves the cursor
Photo-sensor is used
Card Reader:-
It was punched cards to give information to the computer
The computer program which is to be fetched, transformed into punched cards by the process known as
card punching
After punching all cards, they are given to card reader, computer reads one by one
Webcam:-
It is a video camera that gives image or video to the computer that can be transferred online
It is connected to the system with USB cable
It us used for video chatting, video conferencing, etc using WWW
Microphone:-
It is an acoustic-to-electric transducer or sensor that converts sound signals to electric signals
They are used in Telephones, hearing aids, recording audio engineering, Radio and TV broadcasting, etc
It uses electromagnetic inductance capacitance change to produce electric signal from mechanical
vibration
CPU:-
It is called as brain of the computer
It performs taks such as arithmetic and logical operations
CPU is divided into three parts: ALU, Control Unit, Registers
ALU:-
After the system gets input data, it is stored in primary storage.
The actual processing of data takes place at Arithmetic Logic Unit (ALU)
It performs addition, subtraction, multiplication, division, logical comparison, etc
It also performs AND, OR, NOT, XOR, etc operations
Control Unit:-
It acts like the supervisor of a computer
It controls the overall activities of a computer components
It checks all the operations of a computer are going correctly or not
It determines how the instructions are executed one by one
It controls all the input and output operations of a computer
For executing an instruction, it performs the following steps:-
o Address of the instruction is placed in address bus
o Instruction is read from the memory
o Instruction is sent for decoding
o Data from that address is read from the memory
o These data and address are sent to the memory
o Again the next instruction is taken from the memory
Registers:-
They are high speed memory units for storing temporary data
they are small in size
it stores data, instruction, address, etc
ALU is also a register
Types: Accumulator, GPR, SPR( PC, MAR, MBR, IR)
Accumulator: to store the operands before execution. It receives the result of ALU operation
GPR: General Purpose Registers are used to store data and intermediate results
SPR:Special Purpose Registers are used for a certain purpose.
PC: Program counter, MAR: Memory Address Bus, MBR: Memory Buffer Register
Output Unit:-
It is a medium between computer and the human
After the CPU finishes operation, the output is displayed in the output unit
They take the input from CPU and converts to human readable form
Types of output:- Hardcopy, softcopy
Hardcopy: The output that can be seen physically using a printer is called as hardcopy
Page 8 of 11
“There is no shortcut for success; Work hard and get success” – Sachin Tendulkar
**GE6151 – COMPUTER PROGRAMMING**
Softcopy: The electric version of output that is stored in a computer or a memory card, or a hard disk
Ex: Monitor, Printer, Plotter
Monitor:-
It is the most popular output device
It is also called as Visual Display Unit (VDU)
It is connected to a computer through a cable called Video cord
It can be monochrome or color monitor
LCD: Liquid Crystal Display monitors
o They are flat type of screen. Liquid crystals are used for display in the screen
CRT: Cathode Ray Tube monitors
o They are old-fashioned TV set like monitor
Printer:-
The output of a computer can be printed using Printer, to get the hardcopy
Laser printer, Inkjet printer: Impact printers. They give fast printouts with good quality using LASER
Dot matrix printer: non-impact printers. Their quality is poor, they are used for billing purpose
Plotter:-
They are used for printing graphics
They are used in CAD/CAM
Pen plotters take printout by moving a pen across the surface of a piece of paper
Memory Unit:-
memory
Primary Secondary
memory memory
RAM ROM
Page 9 of 11
“There is no shortcut for success; Work hard and get success” – Sachin Tendulkar
**GE6151 – COMPUTER PROGRAMMING**
o We can store the important data into the ROM
o Ex: Hard disk
PROM:
Programmable Read Only Memory
They contain 1000 of fuses burnt, applying 12V to the memory boxes
These fuses say 0 or 1
EPROM:
Erasable Programmable Read Only Memory
They can be deleted
This chips have glass panel, UV rays are passed
Fuses are rearranged in UV rays (all bits become 1)
EEPROM:-
Electrically Erasable Programmable Read Only Memory
They can be erased using electric current
RAM:-
o They are used for storing programs and data that are executed
o It is different from ROM
o Random Access Memory
o It can be read and written
o It is volatile
o When the power is turned off, its contents are erased
o It is also called as RWM (Read Write Memory)
o It is faster than ROM
o Static RAM (SRAM) and Dynamic RAM (DRAM) are its types
o Its cost is high
o Its processing speed is also high
Cache memory:-
o It is a very small memory used to store intermediate results and data
o It stores the data that are more frequently called for processing
o It is present inside the CPU, near the processor
o It is used for the faster execution
Secondary Storage:-
o The speed of primary memory is fast, but secondary memory is slow
o But the memory capacity of primary memory is low. So, secondary memory is used
o It contains large memory space
o It is also called as additional memory or auxiliary memory
o Data is stored in it permanently
o Ex: Magnetic tape, Hard disk, Floppy, Optical Disc, USB drives
o Magnetic tape:-
It is used for large computers like mainframe computers
Large volume of data is stored for a long time
It is like a old tape-recorder cassette
They are cheap
They store data permanently
Its diameter is 12.5mm to 25mm, length is 500M to 1200M
It is compact, low cost, portable, unlimited storage, easy to handle
o Hard disk:-
Hard disks are disks that store more data and works faster
It can store 10GB to 2TB
It consists of platters
Two heads are there for read and write
It is attached to single arm
Information in hard disk is stored in tracks
A track location that cuts across all platters is called as a cylinder
All platters have same no.of tracks
o Floppy Disk
They can store 1.4MB of data
They are 5.25 to 3.5 inch in diameter
They are cheap, portable
Page 10 of 11
“There is no shortcut for success; Work hard and get success” – Sachin Tendulkar
**GE6151 – COMPUTER PROGRAMMING**
o Optical Disk:-
CD-ROM:
Compact Disk
They are made of reflective material
High power laser beam is passed to store data onto CD
Cost is low, storage capacity is 700MB
Its size is 4.5inches in diameter
It can only be READ, cant be written
Only a single side can be used for storage
DVD:
Digital Video Disk
It is the improved version of CD
It can store 4.7GB of data
Both the sides are used for storage
They cannot be scratched or damaged like CD
USB drives:-
They are commonly called as PEN DRIVES
They are removable storage
They are connected to the USB port of a computer
They are fast and portable
They store larger data when compared to CD, DVD (1GB to 64GB pendrives)
Page 11 of 11
“There is no shortcut for success; Work hard and get success” – Sachin Tendulkar
UNIT – 2 ‘C’ Programming Basics
Part – A (2mark questions) 8. What is a variable?
1. What are the advantages and disadvantages of A variable is an identifier for memory
C language? location
Advantages:- Data can be stored in it and taken when
Suitable for creating application software and needed.
system software 9. What is user-defined datatype? What are its types?
Easy, simple and fast. User can define a new kind of datatype to declare
It can be used for general purpose variables. It is called as user-defined datatype
Powerful language Types: Typedef, Enumerated
Flexible and portable 10. What is the difference between local variable and
Disadvantages:- global variable?
LOCAL VARIABLE GLOBAL VARIABLE
It is a weakly typed language
Defined at starting of a Declared before the main
It needs extra memory space
block function
It is not object oriented language It can be used only within It can be used anywhere in
2. What are the characteristics of C language? that block the program
It has many in-built functions ex:- ex:-
Pointers can be used int a=10; void main()
It is a structured language void main() {
Small and easy to understand { int i=10;
Efficient programming language printf(“%d”,i); printf(“%d”,i);
It can be used in many types of computers } }
3. Give the structure of a C program o/p:- 10 o/p:- 10
Documentation section
Preprocessor section 11. What ate the data types available in C?
Definition section In-built data type (int, char, float, …)
Global declaration section User defined data type( typedef, enum,struct)
Main function() Derived data type (array, pointer, function)
{ No data type (void)
Declaration part; 12. What are the basic (or) fundamental datatypes?
Executable statements; Int – to store numbers without decimal point
} Float – to store numbers with decimal point
User defined function() Char – to store a single character
{ Double – To store a big decimal point number
Executable statements; 13. Write any 6 escape sequences in C.
} \n – move to next line
4. What are the steps to execute a C program? \t – move one space horizontally
Create a C program \v- move in space vertically
Compile the program (Alt+F9) \f – move to next page
Link that program to library \r – move to starting of that line
Execute the program(Ctrl+F9) \b – backspace
5. What is a token? What are the types of Tokens? 14. What is an operator? What are its types?
The smallest individual unit of a program An operator is a symbol that tells computer to
is called as Token perform certain task.
Identifiers, keywords, constants, strings, Types:-
operators Arithmetic operators (+, -, *, /, % )
6. Mention some keywords in C language Relational operators ( <, >, <=, >=, ==, != )
If, else, while, do, for, continue, break, include, Logical operator ( &&, ||, !)
main, int, float, char, void Assignment operator ( +=, -=, *=, /=, %=, = )
7. What is a constant? Increment/decrement operator (i++, ++i, i--, --i)
A constant is a fixed value that cannot be changed Conditional operator ( ? : )
in the C program execution. Bitwise operator ( &, |, ^, <<, >>, ~)
Special operator ( sizeof() )
“Weak people revenge; Strong people forgive; Wise people ignore” – Dr.APJ Abdul Kalam
Part – B ( 8 and 16 mark questions) 2. Write a C program for addition of two matrices
1. Write a C program to arrange numbers in #include<stdio.h>
ascending order and descending order. #include<conio.h>
Ascending order:- void main()
#include<stdio.h> {
#include<conio.h> clrscr();
void main() int i, j;
{ int a[3][3], b[3][3], c[3][3];
clrscr(); printf(“enter matrix a in 3 x 3”);
int i, j, temp;
int a[10] = { 7, 2, 9, 5, 3, 4, 8, 1, 6, 0 }; for( i=0; i<3; i++)
for( i=0; i<10; i++) {
{ for(j=0; j<3; j++)
O/p:-
for(j=i+1; j<10; j++) {
Answer = scanf(“%d”,&a[i][j]);
{
0 }
if ( a[i] > a[j] )
1 }
{
2
temp = a[i];
a[i] = a[j]; 3 printf(“enter matrix b in 3 x 3”);
a[j] = temp; 4 for( i=0; i<3; i++)
} 5 {
} 6 for(j=0; j<3; j++)
} 7 {
printf(“answer=”); 8 scanf(“%d”,&b[i][j]);
for( i=0; i<10; i++) 9 }
{ }
printf(“%d”, a[i]);
} for( i=0; i<3; i++)
getch(); {
} for(j=0; j<3; j++)
Descending order:- {
#include<stdio.h> c[i][j] = a[i][j] + b[i][j];
#include<conio.h> }
void main() }
{
clrscr(); printf(“answer = ”);
int i, j, temp; for( i=0; i<3; i++)
int a[10] = { 7, 2, 9, 5, 3, 4, 8, 1, 6, 0 }; {
for( i=0; i<10; i++) for(j=0; j<3; j++)
{ {
for(j=i+1; j<10; j++) printf(“%d”,c[i][j]);
{ O/p:- }
if ( a[i] < a[j] ) Answer = }
{ 9 getch();
temp = a[i]; 8 }
a[i] = a[j]; 7 O/p:-
a[j] = temp; 6 Enter matrix Enter matrix Answer =
} 5 A in 3 X 3 B in 3 X 3 2
} 4 1 1 4
} 3 2 2 3
printf(“answer=”); 2 3 3 8
for( i=0; i<10; i++) 1 4 4 10
{ 0 5 5 12
printf(“%d”, a[i]);
6 6 14
}
getch();
7 7 16
} 8 8 18
9 9
“Weak people revenge; Strong people forgive; Wise people ignore” – Dr.APJ Abdul Kalam
3. Write a C program for subtraction of two matrices 4.Write a program for multiplication of two matrices
#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
void main() void main()
{ {
clrscr(); clrscr();
int i, j; int i, j, k;
int a[3][3], b[3][3], c[3][3]; int a[3][3], b[3][3], c[3][3];
printf(“enter matrix a in 3 x 3”); printf(“enter matrix a in 3 x 3”);
for( i=0; i<3; i++)
for( i=0; i<3; i++) {
{ for(j=0; j<3; j++)
for(j=0; j<3; j++) {
{ scanf(“%d”,&a[i][j]);
scanf(“%d”,&a[i][j]); }
} }
} printf(“enter matrix b in 3 x 3”);
for( i=0; i<3; i++)
printf(“enter matrix b in 3 x 3”); {
for( i=0; i<3; i++) for(j=0; j<3; j++)
{ {
for(j=0; j<3; j++) for(k=0; k<=3; k++)
{ {
scanf(“%d”,&b[i][j]); c[i][j] = c[i][j] + a[i][j] * b[i][j];
} }
} }
}
for( i=0; i<3; i++) for( i=0; i<3; i++)
{ {
for(j=0; j<3; j++) for(j=0; j<3; j++)
{ {
c[i][j] = a[i][j] - b[i][j]; c[i][j] = a[i][j] - b[i][j];
} }
} }
printf(“answer = ”);
printf(“answer = ”); for( i=0; i<3; i++)
for( i=0; i<3; i++) {
{ for(j=0; j<3; j++)
for(j=0; j<3; j++) {
{ printf(“%d”,c[i][j]);
printf(“%d”,c[i][j]); }
} }
} getch();
getch(); }
} O/p:-
O/p:- Enter matrix Enter matrix
Enter matrix Enter matrix A in 3 X 3 B in 3 X 3 Answer =
A in 3 X 3 B in 3 X 3 Answer = 1 1 30
2 1 1 2 2 36
4 2 2 3 3 42
3 3 3 4 4 66
8 4 4 5 5 81
10 5 5 6 6 96
12 6 6 7 7 102
14 7 7 8 8 126
16 8 8 9 9 150
18 9 9
“Weak people revenge; Strong people forgive; Wise people ignore” – Dr.APJ Abdul Kalam
5. Write a C program for transpose of a matrix for( i=0; i<3; i++)
#include<stdio.h> {
#include<conio.h> for(j=0; j<3; j++)
void main() {
{ if(i==j) sum = sum + a[i][j];
clrscr(); }
int i, j; }
int a[3][3];
printf(“enter matrix A in 3 x 3”); printf(“answer = %d”, sum);
getch();
for( i=0; i<3; i++) }
{ O/p:-
for(j=0; j<3; j++) Enter matrix
{ in 3 X 3 Answer =
scanf(“%d”,&a[i][j]); 1 15
} 2
} 3
4
printf(“answer = ”);
5
for( j=0; j<3; i++)
{
6
for(i=0; i<3; j++) 7
{ 8
printf(“%d”,a[i][j]); 9
}
}
getch(); 6. Write a C program to find determinant of a matrix
} #include<stdio.h>
O/p:- #include<conio.h>
Enter matrix void main()
in 3 X 3 Answer = {
1 1 int i, j, det;
2 4 int a[2][2];
3 7 printf(“enter matrix in 2 x 2”);
4 2
for( i=0; i<2; i++)
5 5
{
6 8 for(j=0; j<2; j++)
7 3 {
8 6 scanf(“%d”,&a[i][j]);
9 9 }
}
6. Write a program to find sum of diagonal det = a[0][0] * a[1][1] - a[1][0] * a[0][1];
elements of a matrix printf(“answer = %d”, det);
#include<stdio.h> getch();
#include<conio.h> }
void main()
{
int i, j, sum;
int a[3][3]; O/p:-
printf(“enter matrix in 3 x 3”); Enter 2 x 2 matrix
4
for( i=0; i<3; i++) 8
{ 3
for(j=0; j<3; j++) 9
{ Answer = 12
scanf(“%d”,&a[i][j]);
}
}
“Weak people revenge; Strong people forgive; Wise people ignore” – Dr.APJ Abdul Kalam
8.Explain some string handling functions with strupr() to change letters to upper case (Captials)
example C programs. #include<stdio.h>
Strcat() to concatenate two strings (join) #include<conio.h> O/p:-
#include<stdio.h> void main()
#include<conio.h> O/p:- { MECHANICAL
void main() clrscr();
mechengg
{ char s[10]=”mechanical”;
clrscr(); printf(“%s”,strupr(s) );
char s1[10] = “mech”; getch();
char s2[10] = “engg”; }
strcat(s1,s2);
printf(“%s”,s1); strlwr() to change letters to lower case (small letter)
getch(); #include<stdio.h>
} #include<conio.h> O/p:-
void main()
Strlen() to find length of a string { mechanical
#include<stdio.h> clrscr();
#include<conio.h> char s[10]=”MECHANICAL”;
void main() O/p:-
printf(“%s”,strlwr(s) );
{ 10 getch();
clrscr(); }
int a;
char s[10]=”mechanical”; strset() to change letters to some other letters
a=strlen(a); #include<stdio.h>
printf(“%d”,a); #include<conio.h> O/p:-
getch(); void main()
} { zzzzzzzzzz
clrscr();
Strcpy() to copy one string to another string char s[10]=”mechanical”;
#include<stdio.h> strset(s,z);
#include<conio.h> O/p:-
printf(“%s”,s );
void main() getch();
{ mechanical
}
clrscr();
char s1[10]=”mechanical”; 9. Explain binary search with an example.
char s2[10]=” ”; It follows divide and conquer technique.
strcpy(s2,s1); Key means, search element.
printf(“%s”,s2); Key is compared with middle element.
getch(); If key > middle, ignore first half of elements
}
If key < middle, ignore second half of elements
This process is repeated recursively.
strrev() to reverse a string Until the element is found.
#include<stdio.h>
Input: sorted array of elements
#include<conio.h> O/p:-
Output: element found (or) not found.
void main()
{ lacinahcem Sorted array Element Found
clrscr(); Binary search
Key Element not found
char s[10]=”mechanical”;
printf(“%s”,strrev(s) ); Steps:-
getch(); Find middle element of array
} Compare it with key
If middle < key, repeat steps 1 and 2 for 1st half of array
If middle > key, repeat steps 1 and 2 for 2nd half of array
if middle == key, element is found.
“Weak people revenge; Strong people forgive; Wise people ignore” – Dr.APJ Abdul Kalam
Example:-
Sorted array: 10, 20, 30, 40, 50, 60 10. Explain linear search with an example.
The process of finding the key from the collection.
Key : 50
Compare all the elements one by one.
Here, left = 10, right = 60, mid = (10 + 60) / 2 = 35 No technique is used
Therefore, mid = 30 or 40. We take mid = 30 Input: unsorted array of elements
30 = 50 ? (false). Output: element found (or) not found
30 < 50 ? (true)
So, take second half of elements: 40, 50, 60 unsorted array element found
linear
Sorted array: 40, 50, 60
Key : 50 key search element not found
Here, left = 40, right = 60, mid = (40 + 60) / 2 = 50
50 = 50 ? (true) Steps:-
Therefore, element found at 5th position. Read the first element
Compare with key
Program:- If true, element is found.
#include<stdio.h> If not true, compare next element.
#include<conio.h> Continue step 2, 3, and 4 till the last element.
void main() Example:-
{ Unsorted array: 9, 7, 3, 6, 8, 1, 20
clrscr(); Key : 8
int a[10], i, n, key, c, left, right, mid;
printf(“enter number of elements”); 9 7 3 6 8 1 20
scanf(“%d”,&n);
printf(“enter the sorted array elements”); 8 ? (false). Go to next
for(i=0; i<n; i++)
{ 9 7 3 6 8 1 20
scanf(“%d”,&a[i]);
}
8 ? (false). Go to next
printf(“enter the key”);
scanf(“%d”,&key);
9 7 3 6 8 1 20
right = n-1;
while(left<=right)
8 ? (false). Go to next
{
mid = (left+right)/2;
9 7 3 6 8 1 20
if(key==a[mid] c=1;
else if(key < a[mid]) right = mid -1;
else left = mid +1; 8 ? (false). Go to next
}
if ( c ==0 ) printf(“element not found”); 9 7 3 6 8 1 20
else printf(“element found”);
getch(); 8 ? (true) element found
} Program:-
#include<stdio.h>
O/p:- #include<conio.h>
Enter number of elements 6 void main()
Enter sorted array 10 20 30 40 50 60 {
Enter key 50 clrscr();
int a[10], key, c, i;
Element found
printf(“enter number of elements”);
scanf(“%d”,&n);
Advantages Disadvantages printf(“enter the sorted array elements”);
Efficient and faster Not suitable for unsorted for(i=0; i<n; i++)
array {
Uses divide and conquer Not suitable for dynamically scanf(“%d”,&a[i]);
changing data }
Suitable for large elements printf(“enter the key”);
scanf(“%d”,&key);
“Weak people revenge; Strong people forgive; Wise people ignore” – Dr.APJ Abdul Kalam
2 1 3 4 5
for(i=0; i<n; i++)
{
if(a[i] = = key)
{ 1 2 3 4 5
c = 1;
break; 1 2 3 4 5
}
} 1 2 3 4 5
if( c ==0) printf(“element not found”);
else printf(“element found”); 1 2 3 4 5
getch();
Program:-
}
#include<stdio.h>
O/p:- #include<conio.h>
Enter number of elements 7 void main()
Enter sorted array 9 7 3 6 8 1 20 {
Enter key 8 clrscr();
Element found int n, temp, i, j, a[10];
printf(“enter number of elements”);
scanf(“%d”,&n);
Advantages Disadvantages
printf(“enter the elements”);
Easy and faster Not suitable for large
number of elements for(i=0; i<n, i++)
Array can be unsorted Slower method {
Suitable for small number of Very basic technique scanf(“%d”,&a[i]);
elements }
for(i=n-1; i>0; i--)
11. Explain bubble sort with an example {
It the oldest and easiest sorting. for(j=0; j<=i; j++)
In this sorting, the elements are arranged in some {
technique. if(a[j]>a[j+1])
Take first element, compare it with next element. {
If first > second, swap the elements. temp = a[j];
Then, again compare 1st element with next element a[j] = a[j+1];
Continue this process until, each element is a[j+1]= temp;
compared with every other element. }
}
Unsorted array Sorted array
Bubble }
sort printf(“answer=”);
for(i=0; i<n; i++)
Example:-
{
4 2 3 1 5
printf(“%d”,a[i]);
}
2 4 3 1 5
getch();
}
2 3 4 1 5 O/p:-
Enter number of elements 6
2 3 1 4 5 Enter elements 8 9 6 2 3 7
Answer = 2 3 6 7 8 9
2 3 1 4 5 Advantages Disadvantages
Easy technique Not suitable for large
2 1 3 4 5 number of elements
simple Not efficient
2 1 3 4 5 Suitable for small number of Very basic technique
elements
“Weak people revenge; Strong people forgive; Wise people ignore” – Dr.APJ Abdul Kalam
12. Explain insertion sort with example
Insertion sort is the process of taking elements from Advantages Disadvantages
the list one by one and inserting them at correct Easy technique Not suitable for large
position in the array. number of elements
It is the simplest sorting. Simple and stable method expensive
Suitable for small number of
Unsorted array Insertion Sorted array elements
sort 13. Explain selection sort with example
It is one of the basic sorting technique.
Steps:- It is used to sort the elements in ascending order
Take first element in the list It is based on comparison and swapping.
Then take 2nd element in the list Take the first element and compare it with all the
Check if it is less than 1st other elements.
Insert at correct position The elements are swapped if first one is greater
Then take next element, insert at correct position than the other element.
Repeat this process till all elements are sorted.
Example:- selection
20 10 60 40 30 15 Unsorted array sorted array
sort
10 20 60 40 30 15
10 20 60 40 30 15
Steps:-
10 20 40 60 30 15
Take first element
10 20 30 40 60 15
Compare it with next element
10 15 20 30 40 60
If 1st < 2nd, compare 1st element with 3rd
element
Program:-
#include<stdio.h> If 1st > 2nd element, swap them.
#include<conio.h> Continue this process till all the elements are in
void main() ascending order.
{ Example:-
clrscr(); 4 2 3 1 5
int n, temp, i, j, a[10];
printf(“enter number of elements”); 2 4 3 1 5
scanf(“%d”,&n);
printf(“enter the elements”); 2 4 3 1 5
for(i=0; i<n, i++)
{
scanf(“%d”,&a[i]); 1 4 3 2 5
}
For(i=0; i<s; i++)
{
Temp = a[i]; 1 4 3 2 5
J = i=1;
While( ( temp < a[j] ) && (j>=0) ) 1 3 4 2 5
{
A[j+1] = a[j]; 1 2 4 3 5
J= j-1;
} O/p:-
A[j+1]=temp; Enter number of elements
} 6
printf(“answer=”); 1 2 4 3 5
for(i=0; i<n; i++) Enter elements
{ 20 10 60 40 30 15 1 2 3 4 5
printf(“%d”,a[i]);
Answer=10 15 20 30 40 60
}
getch();
} 1 2 3 4 5
“Weak people revenge; Strong people forgive; Wise people ignore” – Dr.APJ Abdul Kalam
Program:-
#include<stdio.h> Example:-
#include<conio.h> 9 6 3 7 4 2 5 1
void main()
{
clrscr(); 4 2 5 1
9 6 3 7
int n, temp, i, j, a[10];
printf(“enter number of elements”);
scanf(“%d”,&n);
printf(“enter the elements”); 9 6 3 7 4 2 5 1
for(i=0; i<n, i++)
{
scanf(“%d”,&a[i]); 9 6 3 7 4 2 5 1
}
for(i=0; i<n; i++)
{ 6 9 3 7 2 4 1 5
for(j=i+1; j<n; j++)
{
if(a[i] > a[j])
{
3 6 7 9 1 2 4 5
temp = a[i];
a[i]=a[j];
a[j]=temp; 1 2 3 4 5 6 7 9
}
} Program:-
} Refer book page number: 3.44
printf(“answer=”); O/p:-
for(i=0; i<n; i++) Enter number of elements 15. Differentiate entry checked and exit checked
{ 5 conditional constructs with an example.
printf(“%d”,a[i]); (or)
} Enter elements
4 2 3 1 5
Differentiale WHILE loop and Do..WHILE loop
getch();
constructs with an example.
}
Answer= 1 2 3 4 5
WHILE loop
Entry checked loop
Top-tested loop
Advantages Disadvantages Condition is checked at the entry
Easy technique Not suitable for large Loop will not execute if condition is FALSE
number of elements Execute loop until condition is satisfied
Simple method Slow process While(condition)
Suitable for small number of {
elements Body of the loop
}
14. Explain merge sort with example Example:-
It uses divide and conquer technique #include<stdio.h>
Any number of elements can be sorted #include<conio.h> O/p:-
The unsorted array is divided into smaller pieces. void main() 0
Divided until single element. { 1
Then it is merged (joined) in the increasing order. clrscr(); 2
Both dividing and merging is done recursively. int a=0; 3
while(a<5) 4
{
Unsorted array Merge sort Sorted array printf(“%d”,a);
a++;
}
getch();
}
“Weak people revenge; Strong people forgive; Wise people ignore” – Dr.APJ Abdul Kalam
Do..While loop:-
Exit checked loop
Bottom tested loop
Condition is checked at the end
If condition is false, loop will execute one time
Execute the loop until the condition is true.
Do
{
Body of the loop;
} while(condition);
Example program:-
#include<stdio.h>
#include<conio.h> O/p:-
void main() 0
{ 1
clrscr(); 2
int a=0; 3
do 4
{
printf(“%d”,a);
a++;
} while(a<5);
getch();
}
“Weak people revenge; Strong people forgive; Wise people ignore” – Dr.APJ Abdul Kalam
UNIT 4 Functions and Pointers
Part – A (2mark questions) 3. Define pointer in C.
A pointer is a variable that is used to store the
1. Define Function in C address of another variable
A function is a self-contained program, or a sub- It is declared with *
program of one or more statements which is used It is used to make the program small and save
to do some particular task. memory space
Types of functions: i)User defined, ii)pre-defined. Ex:-
2. What are pre-defined functions? Give ex. int a;
int *b;
Pre-defined functions are functions that are
created already and stored in the C – library. b = &a;
They perform certain operations. 4. What is the use of pointers in C?
Ex: printf(), scanf(), getch(), sqrt(), cos(), sin().. To make the program simple
3. What is RETURN statement? To make the size of the program small.
It is used to return the control from calling function To save memory space
to the next statement in the program. To access array elements easily
It can also return some values. To pass information between function
Ex:- To increase the speed of execution of program
return; return 0; return(a+b); 5. Write the differences betweencall by value
4. What are the types of functions based on and call by reference.
return values?
(or) Call by value Call by reference
What are the types of function prototypes?
Values are passed in Address or pointers are
Function with arguments, with return values
function call passed in function call
Function with arguments, without return values
Functions without arguments, with return values If the values of the formal If the values of the formal
Functions without arguments, without return values parameters changed, parameters changed,
5. Define recursion. values of the actual values of the actual
A function calls itself again and again parameters will not parameters will also
Until a condition is TRUE change change.
This process is called as Recursion. Part – B (8 and 16 mark questions)
Ex:-
function() 1. Write a C program to print the sum of digits of a
{ number using recursion.
function();
} #include<stdio.h>
#include<conio.h>
6. What are the advantages and disadvantages of void main()
recursion? {
clrscr();
Advantages Disadvantages int n, r, sum;
printf(“enter a number”);
Less number of Difficult to clear errors
scanf(“%d”,&n);
statements
while(n>0) O/p:-
It needs less number of Difficult to think the logic of { Enter a number
variables a function r = n % 10; 234
sum = sum + r;
It is useful in branching Not easy to write code n = n /10;
process }
printf(“answer=%d”,sum);
getch();
}
“Run like a horse until you WIN; Run faster than the horse after you WIN”
2. Explain predefined function and user-defined 3. Explain function prototypes with an example.
function with an example. Function with arguments, with return values
Pre-defined function:- Function with arguments, without return values
Pre-defined functions are functions that are Functions without arguments, with return values
created already and stored in the C – library. Functions without arguments, without return values
They perform certain operations.
Ex: printf(), scanf(), sqrt(), cos(), sin(), … Function with arguments, with return values:-
This type of function contains arguments and return values.
stdio.h printf() To print to the console output Return statement is used, arguments are used.
scanf() To get values from user Called function will return some value to the calling function
getch() To stay in console screen until Program:-
a key is pressed #include<stdio.h>
string.h strlen() To find length of a string #include<conio.h>
strrev() To reverse a string int add(int, int);
void main()
strcmp() To compare two strings {
math.h sin() To perform sine operation clrscr();
cos() To perform cosine operation int a = 5, b = 10;
sqrt() To perform square root operation printf(“%d”, add ( a, b) );
stdlib.h malloc() To allocate memory getch();
O/p:-
}
exit() To exit the execution
int add(int a, int b) 15
{
Example program:-
return a+b;
#include<stdio.h> }
O/p:-
#include<conio.h> Function with arguments, without return values:-
Mechanical This type of function contains arguments and no return values.
#include<string.h> 10
#include<math.h> Return statement is not used, arguments are used.
100 Called function will not return any value to the calling function
void main() 4
{ Program:-
#include<stdio.h>
clrscr();
#include<conio.h>
printf(“%d”, printf(“Mechanical”) ); void add(int, int);
printf(“%d”, sqrt ( 10 ) ); void main()
printf(“%d”, strlen(“mech”) ); {
getch(); clrscr();
exit(0); int a = 5, b = 10;
} add ( a, b);
User defined function:- getch();
O/p:-
They are defined by the user }
User can create their own function void add(int a, int b) 15
{
Function prototype should be mentioned before
Printf(“%d”,a+b);
main() function }
They perform certain operations. Function without arguments, with return values:-
#include<stdio.h> This type of function contains no arguments, with return values.
#include<conio.h> Return statement is used, arguments are not used
int add(int, int); Called function will return some value to the calling function
void main() Program:- int add()
{ {
#include<stdio.h>
clrscr(); return a+b;
#include<conio.h>
printf(“%d”, add(10, 20) ); }
int add();
getch(); O/p:- void main()
}
30 { O/p:-
int add ( int a, int b)
clrscr();
{
printf(“%d”,add() ); 15
return a+b ;
getch();
}
}
“Run like a horse until you WIN; Run faster than the horse after you WIN”
Function without arguments, without return values:- 5._Write a C program to sort the array elements in
This type of function contains no arguments, no return values. ascending order
Return statement and argument are not used. #include<stdio.h>
Called function will not return any value to the calling function #include<conio.h>
Program:- void sort(int *, int);
#include<stdio.h> void main()
#include<conio.h> {
void add();
clrscr();
void main()
{ int n, a[10], i;
clrscr(); printf(“enter number of elements”);
add ( a, b) ; scanf(“%d”,&n);
getch(); printf(“enter elements”);
O/p:-
} for(i = 0; i < n; i++)
void add() 15 {
{ scanf(“%d”,&a[i]);
int a = 5, b = 10; }
printf(“%d”,a+b); sort(a,n);
} getch();
}
4._Explain recursion with an example.
void sort( int a[], int n)
(or)
{
Write a program to find the factorial of a number using
int i, j, temp;
recursion.
for(i = 0; i<n; i++) O/p:-
A function calls itself again and again Enter number of elements
{
Until a condition is TRUE for( j = 0; j<n; j++)
5
“Run like a horse until you WIN; Run faster than the horse after you WIN”
Use of pointers:- Pointer addition:- Pointer subtraction:-
To make the program simple #include<stdio.h> #include<stdio.h>
To make the size of the program small. #include<conio.h> #include<conio.h>
To save memory space void main() void main()
{ {
To access array elements easily
clrscr(); clrscr();
To pass information between function int a = 5, b = 10; int a = 5, b = 10;
To increase the speed of execution of program int *x, *y; int *x, *y;
Program:- x = &a, y = &b; x = &a, y = &b;
printf(“%d”, (*x + *y)); printf(“%d”, (*x - *y));
#include<stdio.h> getch(); getch();
#include<conio.h> } }
void main()
{ O/p:- O/p:- O/p:-
clrscr();
5 15 -5
int a;
int *b; 5 8. Explain call by value and call by reference.
a = 5;
b = &a; fff0 Call by value Call by reference
printf(“%d”,a); Values are passed in Address or pointers are
fff0
printf(“%d”,*b); function call passed in function call
printf(“%x”,&a);
If the values of the formal If the values of the formal
printf(“%x”,b);
parameters changed, parameters changed,
getch();
} values of the actual values of the actual
7._Explain pointer arithmetic in C. parameters will not parameters will also
(or) change change.
Explain the arithmetic operations using pointers in C. Slow process Fast process
There are four arithmetic operations that can be Different memory Same memory locations
done using pointers. They are:- locations used used
++ Pointer increment No chance or wrong Chances of wrong
-- Pointer decrement calculation calculation
+ Pointer addition Program:- Program:-
- Pointer subtraction #include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
void swap(int, int); void swap(int, int);
Pointer increment:- Pointer decrement:-
void main() void main()
#include<stdio.h> #include<stdio.h>
{ {
#include<conio.h> #include<conio.h>
clrscr(); clrscr();
void main() void main()
int a=5, b=10; int a=5, b=10;
{ {
printf(“before %d%d”,a,b); printf(“before %d%d”,a,b);
clrscr(); clrscr(); swap(a,b); swap(&a,&b);
int a[3] = { 10, 20, 30 }; int a[3] = { 10, 20, 30 }; printf(“after %d%d”,a,b); printf(“after %d%d”,a,b);
int i, *ptr; int i, *ptr; getch(); getch();
ptr = a; ptr = a; } }
for(i = 0; i < 3; i++) for(i = 3; i > 0; i - -) void swap(int x, int y) void swap(int *x, int *y)
{ { { {
printf(“address=%x”,ptr); printf(“address=%x”,ptr);
int t; int t;
printf(value=%d”,*ptr); printf(value=%d”,*ptr); t = x; t = *x;
ptr++; ptr++;
x = y; *x = *y;
} } y = t; *y = t;
getch(); getch(); printf(“inside %d%d”,x,y); printf(“inside %d%d”,*x, *y);
} } } }
O/p:- O/p:- O/p:- O/p:-
Address = fff0 value=10 Address = fff4 value=30 Before 5 10 Before 5 10
Address = fff2 value=20 Address = fff2 value=20 Inside 10 5 Inside 10 5
Address = fff4 value=30 Address = fff0 value=10 After 5 10 After 10 5
“Run like a horse until you WIN; Run faster than the horse after you WIN”
UNIT 5 Structures and Unions
Part – A (2mark questions) 5. What are the differences between a structure and
an union?
1. Define structure. Structure Union
It is a user define data type. Memory space allocated Memory space allocated for
It contains variables of different data type. for all members the highest size member
Variables inside the structure = structure members Each member has own All members share same
We can store int, float, char, etc in a struct memory space memory space
Memory space is created for all members It is used when all It is used when members
Struct keyword is used. members are are not accessed at same
Ex:- independently used time
struct student Members can be accessed Members cannot be
{ at the same time accessed at the same time
int roll; 6. What are the different types of storage classes in
char name[10]; C?
float marks; (or)
} What are the different types of variable scope?
2. What is the purpose of structures? Storage class Keyword
(or) Automatic variables auto
What is the use of structured data type? Static variables static
External variables extern
In arrays, same data type elements are stored. Register variables register
In struct, Different data type elements can be stored.
Difficult data types can be handled 7. Define preprocessor in C.
We can create pointers to the structure It is a program that converts C program with pre-
User can define their own data type. processor directives to the modified version
That modified version can be converted to object
3. Mention the differences between structure and
code by the compiler
arrays
Structure Array It gives three facilities:-
o Include header files
Contains different data Contains same data type
o Macro expansion
type elements elements
o Conditional compilation
Dynamic memory Static memory allocation
allocation
C Program Preprocessor Modified version
(.) dot operator is used to Array index is used to Of C program
access structure members access array members
It is not a pointer It is a base pointer
compiler
4. Define Union in C.
It is a user define data type
It contains variables of different data type Object code
Variables inside the union = union members 8. Write a C program to use macro expansion
We can store int, float, char, etc in an union #include<stdio.h>
Memory space is not created for all members. #include<conio.h>
Memory space is created for Highest Byte size #define area(r) (3.14* (r) * (r) )
member void main()
{
That memory will be shared by all members
clrscr();
Union keyword is used int r = 2;
Ex:- printf(“%f”, area(r) );
union student getch();
{ O/p:-
}
int roll; 12.56
char name[10];
float marks;
}
“My failure created fear to me; My failure after a success created fear to my opponents” – Sachin Tendulkar
9. What is the use of #define pre-processor? struct student
It is used to define a macro {
The substitution value should be mentioned char name[20];
The macro will be substituted by its value char reg[20];
Ex:- int avg;
char grade;
#include<stdio.h>
} s[50], *p;
#include<conio.h> printf(“enter number of students”);
#define a 10 scanf(“%d”,&n);
O/p:-
void main() for(i = 0; i < n; i + + )
{ 10 {
printf(“%d”,a); printf(“enter name”);
getch(); scanf(“%s”,s[i].name);
} printf(“enter register number”);
10. Define static storage class. scanf(“%s”,s[i].reg);
To declare variables statically printf(“enter average”);
scanf(“%d”,s[i].avg);
Static variables are stored in main memory
}
It can be used locally or globally p = s;
Static variables are automatically initialized to 0 for(p = s; p < s +n; p ++)
Ex:- {
#include<stdio.h> if(p - > avg < 50) p - > grade = ‘u’;
#include<conio.h> else if(p - > avg < 55) p - > grade = ‘e’;
void main() else if(p - > avg < 60) p - > grade = ‘d’;
{ O/p:- else if(p - > avg < 70) p - > grade = ‘c’;
int a; 847(garbage value) else if(p - > avg < 80) p - > grade = ‘b’;
static int b;
0 else if(p - > avg < 90) p - > grade = ‘a’;
else p - > grade = ‘s’;
printf(“%d”,a);
}
printf(“%d”,b); for(p = s; p < s +n; p ++)
getch(); {
} printf(“%s”, p - > name);
printf(“%s”, p - > reg);
Part-B ( 8 and 16 marks) printf(“%d”, p - > avg);
printf(“%c”, p - > grade);
1. What is a structure? Write a C program to create a }
mark sheet for students using structure. getch();
It is a user define data type. }
It contains variables of different data type. O/p:-
Variables inside the structure = structure members Enter number of students
We can store int, float, char, etc in a struct 2
Memory space is created for all members Enter name
Sachin
Struct keyword is used. Enter register number
Ex:- 001
struct student Enter average
{ 91
int roll;
char name[10]; Enter name
float marks; Tendulkar
} Enter register number
002
Program:- Enter average
#include<stdio.h> 82
#include<conio.h>
void main() Sachin 001 91 S
{ Tendulkar 002 82 A
int i, n;
“My failure created fear to me; My failure after a success created fear to my opponents” – Sachin Tendulkar
2. What is a structure? Write a C program to store and 3. What are the storage classes in C? Explain them in
display employee details using structure. detail with example.
It is a user define data type. Storage class Keyword
It contains variables of different data type. Automatic variables auto
Variables inside the structure = structure members Static variables static
We can store int, float, char, etc in a struct External variables extern
Memory space is created for all members Register variables register
Struct keyword is used. Automatic variables:-
Ex:- Ordinary variables
struct employee O/p:- Defined inside a function or a block
{ Enter number of employees Default storage class
char name[10]; 2 “auto” keyword is used
char id[10]; Its lifetime is, within that function or block
Enter name
float salary; After execution, they are erased from memory.
Rahul
} Enter ID Ex:-
Program:- 001 #include<stdio.h>
#include<stdio.h> Enter salary #include<conio.h>
#include<conio.h> 39000 void f1();
void main() void f2();
{ Enter name void main()
Dravid {
int i, n;
Enter ID
struct employee clrscr();
002
{ Enter salary int a = 10; O/p:-
char name[10]; 51000 printf(“%d”, a);
char id[10]; f1(); 10
float salary; Rahul 001 39000 associate f2();
20
char post[10]; Dravid 002 51000 chairman getch();
} s[50], *p; } 30
for(i = 0; i < n; i++) void f1()
{ {
printf(“enter name”); int a = 20;
scanf(“%s”,s[i].name); printf(“%d”,a);
printf(“enter ID”); }
scanf(“%s”,s[i].id);
void f2()
printf(“enter salary”);
{
scanf(“%f”,s[i].salary);
} int a = 30;
p = s; printf(“%d”,a);
for(p = s; p < s +n; p ++) }
{ Static variables:-
if (p - > salary < 10000) p - > post=”trainee”; To declare variables statically
else if(p - > salary <20000) p -> post=”executive”; Static variables are stored in main memory
else if(p - > salary <30000) p -> post=”associate”; It can be used locally or globally
else if(p - > salary <40000) p -> post=”director”; “static” keyword is used
else if(p - > salary <50000) p -> post=”manager”;
Static variables are automatically initialized to 0
else p -> post=”chairman”;
} Ex:-
for(p = s; p < s +n; p ++) #include<stdio.h>
{ #include<conio.h>
printf(“%s”, p - > name); void main()
printf(“%s”, p - > id); { O/p:-
printf(“%d”, p - > salary); int a; 847(garbage value)
printf(“%c”, p - > post); static int b; 0
} printf(“ %d ” ,a);
getch(); printf(“ %d ” ,b);
} getch();
}
“My failure created fear to me; My failure after a success created fear to my opponents” – Sachin Tendulkar
External variables:- Include header files:-
They are like global variables They are used to include header files
They can be used anywhere in the function Program will be linked with that header file
They are declared outside the functions Ex:- printf() and scanf() are present in “stdio.h”
They are declared before main() function #include<stdio.h> Printf() and scanf()
“extern” keyword is used #include<conio.h> Getch(), gets(), puts(), …
Ex:- #include<string.h> Strlen(), strrev(), strcmp(), …
#include<stdio.h> #include<math.h> Sin(), cos(), sqrt(), …
#include<conio.h>
int n = 10; O/p:- Macro expansion:-
void main() 10 It is defined by #define pre-processor directive.
{ Macro contains an item with its associated text.
clrscr(); In the program,macro will be substituted by its value
printf(“%d”, n);
This is called as Macro substitution
getch();
It is done before compilation of program.
}
#define To define a macro
Register variables:-
#undef To undefined a macro
They are stored in registers
Example:-
They are not stored in main memory
#include<stdio.h>
“Register” keyword is used
#include<conio.h>
For the purpose of fast access #define area(r) (3.14 * (r) * (r) )
More variables cannot be stored in register. void main()
Ex:- { O/p:-
#include<stdio.h> clrscr();
#include<conio.h> 12.56
O/p:- int r = 2;
void main() printf(“area = %f”, area(r) );
0
{ getch();
1
clrscr(); 2 }
register int a = 1; 3 Conditional compilation:-
for(a=0;a<5;a++) 4 #ifdef If macro is defined
{ #ifndef If macro is not defined
printf(“%d”,a); #endif To end the macro “if”
} #if “if” conditional compilation
getch(); #elif “else if”
} #else “else”
4. What are pre-processor directives? Explain any
four directives with example. “#ifdef” and “#ifndef” #if...#elif...#else
It is a program that converts C program with pre- #include<stdio.h> #include<stdio.h>
processor directives to the modified version #include<conio.h> #include<conio.h>
That modified version can be converted to object void main() void main()
code by the compiler { {
It gives three facilities:- clrscr(); clrscr();
o Include header files #define A 10 #define A 10
o Macro expansion #ifdef A #if A<0
o Conditional compilation printf(“defined”); printf(“negative”);
#endif #elif A>0
C Program Preprocessor Modified version printf(“positive”);
Of C program #undef A #else
#ifndef A printf(“Zero”);
compiler printf(“undefined”); #endif
#endif O/p:- getch(); O/p:-
getch(); Defined } positive
Object code } undefined
“My failure created fear to me; My failure after a success created fear to my opponents” – Sachin Tendulkar