21lali)
In Cprogramming, input and output operations are
essential for interating with users and displaying
results. Jhe standard input and output funtions are
'print)' and 'scanf) Jhe `printf)' funion is used for
output, and the `scant) fundtion is used for input.
Here's a simple example to ilustrate input and output
statements in C:
C
#include <stdio . h>
int main() {
1/ Output statement using
printf()
printf("Welcome to the C
Programming Example! \n");
1/ Input statement using scanf()
int num;
printf("Enter an integer number:
");
Scanf("%d", &num);
1/ Output the result
printf("You entered: %d\n"
num);
return 0;
}
In this example. we use
printf to display a welcome
message to the user. Jhen, we use 'scanf) to get an
integer inputtrom the user. Jhe user's input is stored
in the varigble 'num', and finally we use printf
again to display the entered number back to the user.
When you run this program, it will display the
welcome message and ask the user to enter an
integer. Ater entering the integer, it will display the
entered number back to the user. For instance.
J5
21)ali)
#inclydesstdio.h
#incudesconio.h>
Void main()
int age,
clrscrl);
print("Enter age\n");
scanf"%d,&age);
fages18):
print"You are not eligible to vote\n")
print"You have to wait %d years to vote\n",18-age);
fage>18):
print"You are eligible to vote\n");
printf"Program ended n);
return O;
6:43 pm
22)a)/)
Arrays are afundamental data strycture in
programming used to store multiple elements of
the same data type under a single variable name.
They prouide a way to eficiently manage a collection
of values in contiguous memory locations. In C,
rrays can be one-dimensional, two-dimensiona, or
multi-dimensional, depending on the number of indices
required to access elements.
1. One-dímensional array:
Aone-dimensional array is a inear collection of
elements of the same data type, stored in a contiguous
block of memory. Each element in the array is
accessed using a single index. Jhe syntax for declaring
a one-dimensional array in Cis as folows:
C
dataType arrayName[arraySize];
Here, datatype' specfes the data type f the
elements, arrayName' is the identfer of the array,
and 'arraySize' is the number of elements the array
Can hold.
Example:
C
int numbers[5]; // Declares an
integer array that can store 5
elements
float prices [ 10]; // Declares a
floating-point array that can store
10 elements
char letters[26]; // Declares a 26
character array that can store
elements
2. Two-dimensional array:
Atwo-dimensional array is an array of arrays. (t
organizes elements in atable-like structure with rouws
and columns. Each element in the array is accessed
using two indices-one for the rouw and one for the
column. Jhe syntaxfor decaring a tuo-dmensiona
array in Cis as followsi
C
dataType arrayName [rowSize]
[columnSize];
Hare, dataType` spacifes the d¡ta typa of the
elements,`arrayName is the identifier of the
array,`rowSize' specifes the numbar frows, and
columnßize' spectfes the number of columns in the
array.
Example:
C
int matrix[3][31; // Declares a 3x3
integer matrix
float table [4][5] ; // Declares a 4x5
floating-point table
char chessboa rd [8] [8] ; 1/ Declares
an 8x8 character chessboard
3. Multi-dimensional array:
Amulti-dimensional array reters to arrays with
more than two dimensions. They can be thought of
as arrays of arrays of arrays and so on. Jhe syntax
for declaring a multi-dimensional array in Cis an
extension of the two-dimensional array declaration.
C
dataType arrayName [size1 ][size2]...
[sizeN] ;
Example:
C
int matrix[3] [3]; // Declares a 3x3
integer matrix
float table [4][5]; // Declares a 4x5
floating-point table
char chessboard[8] [8]; // Declares
an 8x8 character chessboard
3. Muti-dimensional array
Amulti-dimensional array refers to arrays with
more than two dimensions. Jhey can be thought of
as arraysof arrays of arrays and so on. Jhe syntax
for declaring g muti-dimensiona! array in Cis an
extension of the two-dimensional array declaration:
C
dataType arrayName [size1][size2] . . .
[sizeN] ;
Here, dataType' specfes the data type of the
elements, 'arrayName is the identifer o the anray,
and 'sizel', 'size2,.., 'sizeN represent the sizes for
each dimension.
Example:
C
int threeDArray [2] [3][4]: 1/
Declares a 3-dimensional integer
array with size 2x3x4
float fourDArray [2] [2] [3]
[3]; /I Declares a 4-dimensional
floating-point array with size
2x2x3x3
Multi-dimensional arrays can be challenging to work
with for larger dimensions due to their complexity.
but they useful for representing data in muliple
are
dimensions, such as images, matrices, and 3D data
Structures.
7:14 pm
22)a))#includesstdio.ho
#include<conio.h>
void mainl)
int al21[2),temp,
print"Enter array elements");
forlint i=0;i<2;i++)
forlint j=0;<2j++\
scanf"%d,&ali\ljl);
forlint i=0;i<2;i++\\
forlint j=0;<2;j++)\
falilljl>ali+1\\j+1)M
temp=alill);
alil(jl=ali+1\l+11;
ali+1llj+1]=temp,
printf"Aray elements in sorted form");
Forli=0;i<2;;++)
Forlj=0j<2j++)
{
printf"%df ali\lj)
return 0;
6:57 pm
23lal)
Nested structures and arrays are useful data
strucures in programming that allou you to organize
and store data in a hierarchical manner. Lete's define
what nested strucures and arrays are and then
prouide an example to illustrate their usage.
1. Nested Struture:
Anested structure refers to a data structure in which
one or more elements of the structure are themselues
structures. Jhis allows you to create more complex
data types by combining, mutiple simpler data types.
Nested structures are often used to model real-world
entities that have multiple attributes or properties.
Example ofa nested struture in Cprogramming:
C
#include <stdio. h>
struct Address {
char street [50];
char city[30];
int zipCode;
}i
struct Person {
char name [50];
int age
struct Address address;
};
int main() {
struct Person person1 = {
"John Doe"
30,
{"123 Main St", "New York",
10001}
};
printf("Name: %s\n"
person1. name);
printf("Age: %d\n",
person1.age);
printf("Street: %s\n",
person1.address .street);
2. Array of Strucures:
An array of structures is a collection of multiple
Structures of the same type, where each element of the
array represents one instance o the structure, Arrays
of structures are commonly used to store and manage
multiple records of the same structure type.
Example of an array of structures in Cprogramming:
C
#include <stdio . h>
struct Student {
char name [50];
int age;
int rollNumber;
};
int main() {
struct Student students [3] = {
{"Alice", 20, 101},
{"Bob", 21, 102},
{"Charlie", 19, 103}
for (int i = 0; i<3; i++) {
printf("Student %d: \n", i +
1);
printf("Name: %s\n",
students[i]. name);
printf("Age: %d\n",
students [i]. age);
printf("Roll Number: %d\n",
students[i]. rollNumber);
printf("\n");
}
return 0;
}
In this example, we have defined a Sudent trucure
represent information about sudents. We then
create an array of three Student structures. Each
element of the arras holds information about a
24)a))
In Cprogramming, random accessfles can be
implemented using the standard /O library fundtions,
Speccally aokand uritel) for writing data, and
fseektl' and readl) for reading data from specifc
positions within the file. Let's create a C program to
demonstrate how to work with random access files.
In this example, we ll use a similar scenario as before,
storing intormation about employees.
C
#include <stdio, h>
#include <string.h>
1/ Define the size off each record in
the file (name: 50 bytes, salary: 8
bytes)
#define RECORD_SIZE 58
1/ Employee structure to represent
the data
struct Employee {
int id;
char name [50] ;
long salary;
1/ Function to write employee data
to the file
void writeEmployeeDataToFile(const
char* filename, int recordNumber,
struct Employee* employee) {
FILE* file = fopen(filename ,
"rb+"); 1/ Open the file in binary
mode for read and write
if (file == NULL) {
perror("Error opening
file") ;
return;
}
1/ Move the file pointer to the
correct position for the specified
record number
fseek(file, recordNumber *
1/ Write the employee's name and
salary to the file
fwrite(employee->name,
sizeof (char), 50, file);
fwrite(&(employee->salary),
sizeof(long), 1, file);
fclose(file);
}
1/ Function to read employee data
from the file
void readEmployeeData FromFile(const
char* filename, int recordNumber ,
struct Employee* employee) {
FILE* file = fopen(filename,
"rb"); 1/ Open the fille in binary
mode for read
if (file == NULL) {
perror("Error opening
file");
return;
}
1/ Move the file pointer to the
correct position for the specified
record number
fseek(file, recordNumber *
RECORD SIZE, SEEK _SET);
1/ Read the employee's name from
the file
fread(employee->name,
sizeof (char), 50, file);
employee->name [49] = \0'; //
Null-terminate the string
1/ Read the employee's salary
from the file
fread (&(employee->salary),
sizeof(long), 1, file);
fclose(file) ;
}
25)a)
ln C, interrupts are aamechanism that allows the
interruption of the normal program flow to handle
specific events or prioritize certain tasks. lnterrupts
are
typically used in embedded systems and real-time
applications to respond to hardware euents or
time-critical tasks.
Interrupts can be both harduare or
software-generated. Hardware interrupts
generated by external devicgs such as timers,
keyboard, mouse, or /O devices to signal the
occurrence of an event. Sotware interrupts are
typically generated by the software itsetf using special
instrucions or funtion calls.
In C, interrupts can be handled using function pointers
or by directly writing interrupt seruice routines.
Here s a general overuiew of how interrupts can be
Used in C.
1. Deine an interrupt seruice routine (l8R): An l8R is
responsibla fer handling the interrupt and performing
the required ation. ln C, an l8R is typically a void
tuntion with no arguments or a structure spectying
the interrupt hander.
2.Register the l8R: Jhis Stop involwes associating
the l8R with the corresponding interrupt source.
Depending on the hardware or operating system, you
can use libraries or APls specific to that plattorm.
3. Enable interrupts: Before the program enters the
main event loop, interrupts must be enabled to listen
for the euents. Jhis step varies depending on the target
system and may involve enabling a specific interrupt
line or setting an interrupt enable flag.
4. Main program loop: Jhe main program continues
normal operation while waiting for the interrupt to
OCcur.
5. Interrupt handler execution: When tha interrupt
occurs, the associated l8R is called, and the execution
flow jumps to the l8R Jhe l8R handles the
event and performs the required actions, suchinterrupt
as
processing data,
with other devices.updating variables,or communicating
6. Returnfrom l8R: Ater completing the
actions, the l8R returns control to the necessary
program flow, which continues executioninterrupted
frrom where it
was interrupted.
lt is
important to note that interrupt handling requires
careful consideration, especially in multi-threaded
or
multi-tasking systems. Proper
and protection mechanisms shouldsynchronization
be used to
shared resources, prevent race conditions, andhandle
maintain data integrity.
Ouerall, interrupts prouide way to respond to events
a
in real-time and efcienty handle
time-sensitive tasks,
making them essentialin many embedded
real-time applications. systems and
Program:
#include <dos.h
void printScreenContentsl) {
union REgS regs;
regs.h.ah =O-0E;
regs.h.al= 0;
regs.h.bh = 0;
reqs.h.bl= 0:07
int86(0x10, ®s, ®s);
int main() {
printScreen Contents();
return 0;
7:11 pm