STRUCTURES
IN C PROGRAMMING
By
Fathima C.Asees
Introduction Structure
Declaring Structure Variable
Structure Initialization
Accessing member of Structure
Copying & Comparing Structure Variable
Array of Structures
Conclusion
Reference
CONTENTS
INTRODUCTION
Structure are collection of different datatype grouped
together under a single variable name for convienient
handling. 'struct' is a keyword, struct type is a name
which identifies the structure.
syntax: struct <structure name >
{
datatype member1;
datatype member2;
};
Example:
struct employee
int id;
char name[10];
Float salary;
};
DECLARING STRUCTURE VARIABLES
• Structure variable declaration is similar to the declaration of variables of any other data types.
• Structure variables can be declared in following two ways
1.Declaring Structure variable separately
Struct employee,
{
int id;
char name [10];
float salary;
};
void main()
{
struct employee e1, e2;
2.Declarimg structure variable with in structure definintion
Struct employee
{
int id;
char name (10);
float salary;
}e1,e2;
notes:
If no: of variable are not fixed 1st way, it provide you the flexibility to declare the
structure variable many times
If no: of variables are fixed use 2nd way. It save your code to declare a variable in
main () function.
•Like any other data type, structure variable can also be initialized at compile time.
Syntax: struct structure_name variable={value 1,value 2,value 3,......value n}
Eg: struct employee
{
int id;
char name [10];
float salary;
}
void main()
{
struct employee e1= {01, "Joy", 1000 };
struct employee e={02, "Ram",2000};
STRUCTURE INITIALIZATION
ACCESSING MEMBER OF STRUCTURE
•By using dot (.) operator or member operator
Syntax: structure variable.member
Eg: struct employee
{
int id;
char name [10];
float salary;
};
void main();
{
struct employee e1 = {01, "Joy", 1000 };
printf("%d",e1.id);
COPYING AND COMPARING STRUCTURE VARIABLES
•If employee 1 and employee 2 belong to same
structure
then following statment is valid ;
e1=e2;
e2=e1;
•However, the statement such as:
e1==e2
e1!=e2
not permitted
Eg: for copying:
struct employee
{
int id;
Char name [20];
Float salary;
};
void main ()
Struct employee e1;
Struct employer e2 {02,"Joy", 1000);
e1=e2;
printf("Details of e1");
printf("%d %s %f,e1.id, e1.name,e1.salary);
}
Eg: for comparing:
struct employee
{
int id;
Char name [10];
Float salary;
};
void main ()
Struct employee e1{01,"ram",1000);
Struct employer e2 {02,"Joy", 2000);
If (e1.id<e2.id)
printf("Yes");
}
Consider a case ,where we need to store data of 3 students we can store it
by using the structure as shown below,
#include <stdio.h>
struct employee
{
int id;
Char name [20];
Float salary;
};
void main ()
{
ARRAY OF STRUCTURES
Struct employee e1{01,"ram",1000);
Struct employee e2 {02,"joy", 2000);
Struct employee e3{03,"roy",3000);
printf("Details of e1");
scanf("%d %c %f,e1.id, e1.name,e1.salary)
printf(Details of e2");
Scanf("%d %c %f";e2.id,e2.name,e2.salary);
printf("Details of e3");
scanf("%d %c %f",e3.id,e3.name,e3.salary);
}
•An array of structres in C can be defined as the collection of multiple
structures variables where each variable contains information about
different entities. The array of structures in C are used to store information
about multiple entities of different data types. The array of structures is
also known as the collection of structure.
Eg: struct employee
{
int id;
char name;
float salary;
}
void main ()
{
Struct employee e[3]
for(i=0;i < 3;i++)
{
scanf("%d %s %f", &e[i]. id, &e[i].name, &e[i] .salary);
for(i=0;i<3;i++)
{
printf("%d %s %f", &e[i]. id ,&e[i] .name. &e[i]. salary);
}}
CONCLUSION
THANK YOU!

Cprogramming_structures_into_types_conclusion.pdf

  • 1.
  • 2.
    Introduction Structure Declaring StructureVariable Structure Initialization Accessing member of Structure Copying & Comparing Structure Variable Array of Structures Conclusion Reference CONTENTS
  • 3.
    INTRODUCTION Structure are collectionof different datatype grouped together under a single variable name for convienient handling. 'struct' is a keyword, struct type is a name which identifies the structure. syntax: struct <structure name > { datatype member1; datatype member2; }; Example: struct employee int id; char name[10]; Float salary; };
  • 4.
    DECLARING STRUCTURE VARIABLES •Structure variable declaration is similar to the declaration of variables of any other data types. • Structure variables can be declared in following two ways 1.Declaring Structure variable separately Struct employee, { int id; char name [10]; float salary; }; void main() { struct employee e1, e2;
  • 5.
    2.Declarimg structure variablewith in structure definintion Struct employee { int id; char name (10); float salary; }e1,e2; notes: If no: of variable are not fixed 1st way, it provide you the flexibility to declare the structure variable many times If no: of variables are fixed use 2nd way. It save your code to declare a variable in main () function.
  • 6.
    •Like any otherdata type, structure variable can also be initialized at compile time. Syntax: struct structure_name variable={value 1,value 2,value 3,......value n} Eg: struct employee { int id; char name [10]; float salary; } void main() { struct employee e1= {01, "Joy", 1000 }; struct employee e={02, "Ram",2000}; STRUCTURE INITIALIZATION
  • 7.
    ACCESSING MEMBER OFSTRUCTURE •By using dot (.) operator or member operator Syntax: structure variable.member Eg: struct employee { int id; char name [10]; float salary; }; void main(); { struct employee e1 = {01, "Joy", 1000 }; printf("%d",e1.id);
  • 8.
    COPYING AND COMPARINGSTRUCTURE VARIABLES •If employee 1 and employee 2 belong to same structure then following statment is valid ; e1=e2; e2=e1; •However, the statement such as: e1==e2 e1!=e2 not permitted
  • 9.
    Eg: for copying: structemployee { int id; Char name [20]; Float salary; }; void main () Struct employee e1; Struct employer e2 {02,"Joy", 1000); e1=e2; printf("Details of e1"); printf("%d %s %f,e1.id, e1.name,e1.salary); }
  • 10.
    Eg: for comparing: structemployee { int id; Char name [10]; Float salary; }; void main () Struct employee e1{01,"ram",1000); Struct employer e2 {02,"Joy", 2000); If (e1.id<e2.id) printf("Yes"); }
  • 11.
    Consider a case,where we need to store data of 3 students we can store it by using the structure as shown below, #include <stdio.h> struct employee { int id; Char name [20]; Float salary; }; void main () { ARRAY OF STRUCTURES
  • 12.
    Struct employee e1{01,"ram",1000); Structemployee e2 {02,"joy", 2000); Struct employee e3{03,"roy",3000); printf("Details of e1"); scanf("%d %c %f,e1.id, e1.name,e1.salary) printf(Details of e2"); Scanf("%d %c %f";e2.id,e2.name,e2.salary); printf("Details of e3"); scanf("%d %c %f",e3.id,e3.name,e3.salary); }
  • 13.
    •An array ofstructres in C can be defined as the collection of multiple structures variables where each variable contains information about different entities. The array of structures in C are used to store information about multiple entities of different data types. The array of structures is also known as the collection of structure.
  • 14.
    Eg: struct employee { intid; char name; float salary; } void main () { Struct employee e[3] for(i=0;i < 3;i++) { scanf("%d %s %f", &e[i]. id, &e[i].name, &e[i] .salary); for(i=0;i<3;i++) { printf("%d %s %f", &e[i]. id ,&e[i] .name. &e[i]. salary); }}
  • 15.
  • 16.