Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 14

COMPUTER PROGRAMMING

Engr. Anees Ahmed Soomro


Assistant Professor
CSE QUEST Nawabshah
INTRODUCTION TO COMPUTER PROGRAMMING

1) Structures in C
2) Initialization Structures in C
3) Multiple struct variables of same types, Combining declaration
4) Unions, Unions of structures
Structs in C
• Simple variables can hold one piece of information at a
time and how arrays can hold number of pieces of
information of same data type.
• We want to operate on data items of different type together
as unit.
• In this can neither the variable nor array is adequate.
• For example, suppose you want a program to store data
concerning an employee in an organization.
• You want to store Employees Name(a character array),
Department Number(an integer), Salary(a floating point
number) and so forth.
• Even a multidimensional array will not solve this problem,
since all the elements of any array must be of same data
type.
• You could several different arrays.
• A character array for name, a floating point array of
salaries and so on.
• To solve this type of problem C provides a
special type : the Structure.
• A structure consists of a number of data items which need
not be of same type grouped together.
• A structure would consists of the Employee’s Name,
Department Number, Salary and other pertinent
information
• Simple variables, arrays and structures
Simple Structure
void main(void)
{
struct easy
{
int num;
char ch;
};
struct easy ez1;
ez1.num=2;
ez1.ch=‘z’;
printf(“ez1.num=%d,ez1.ch=%c\n”,ez1.num,ez1.ch);
}
Initializing structures
• Like simple variables and arrays structure variables can
be initialized given specific values at the beginning of
program.
• The format used is quite similar to that used to initialize
arrays.
struct personel
{
char name[30];
int agnumb;
};
struct personel agent1={“harriton tweedbusy”,012};
struct personel agent2={“james bond”,007};
void main(void)
{
printf(“\n list of agents:\n”);
printf(“name:%s\n”,agent1.name);
printf(“agent number:%03d\n”,agent1.agnumb);
printf(“name:%s\n”,agent2.name);
printf(“agent number:%03d\n”,agent2.agnumb);
}
Multiple structure variables of same type
• There can be more than one int or float variable in program
• There can be any number of variables of given structure type
void main(void)
{
struct easy
{
int num;
char ch;
};
struct easy ez1;
struct easy ez2;
ez1.num=2;
ez1.ch=‘z’;
ez2.num=3;
ez2.ch=‘y’;
printf(“ez1.num=%d,ez1.ch=%c\n”,ez1.num,ez1.ch);
printf(“ez2.num=%d,ez2.ch=%c\n”,ez2.num,ez2.ch);
Combining declaration
void main(void)
{
struct easy
{
int num;
char ch;
}ez1,ez2;
.
.
.
.
.
Unions
• Unions have same relationship to structures that you might
have to a distant cousin who resembled you but turned out
to be smuggling contraband in a third world country.
• Both structures and union are used to group a number of
different variables together.
• But while a structure enables us to treat as a unit a number
of different variables stored at different places in memory.
• A union enables us to treat the space in memory as a
number of different variables.
• That is unions is a way for a section of memory to be
treated as a variable of one type on one occasion.
Unions
void main(void)
{
union intflo
{
int intnum;
float fltnum;
}unex;
printf(“sizeof(union intflo)=%d\n”,sizeof(union intflo));
unex.intnum=734;
printf(“unex.intnum=%d\n”,unex.intnum);
unex.fltnum=867.43;
printf(“unex.fltnum=%.2f\n”,unex.fltnum);
}
Unions of Structures
• Structures can be nested within each other, so too can union be nested
in unions, Unions in structures, and structures in unions.
void main(void)
{
struct twoints
{
int intnum1;
int intnum2;
}stex;
union intflo
{
struct twoints stex;
float fltnum;
}unex;
continued……
Unions of Structures

printf(“sizeof(union intflo)=%d\n”,sizeof(union intflo));


unex.stex.intnum1=734;
unex.stex.intnum2=-333;
printf(“unex.stex.intnum1=%d\n”,unex.stex.intnum1);
printf(“unex.stex.intnum2=%d\n”,unex.stex.intnum2);

unex.fltnum=867.43;
printf(“unex.fltnum=%.1f\n”,unex.fltnum);
}

You might also like