SlideShare a Scribd company logo
1 of 27
GURUNANAK INSTITUTE
OF
TECHNOLOGY
COURSE: B.SC. CYBER SECURITY
BATCH: 1ST SEMESTER (1ST YEAR)
C PROGRAMMING LANGUAGE
CYS101
SUBMITEFD BY: ARVIND SHUKLA
ROLL NUMBER:31184422012
REGISTRATION NUMBER
STRUCTURE
• A structure is a collection of variables of
 different data types under asinglename.
 The variables are called members
of the structure.
 Thestructure isalso called auser-defined data type.
STRUCTURE
Contents:
• Introduction
• Defining a structure
• Structure Initialization
• Partial Initialization
• Structure Variables
• Reading Values
• Sorting Values
• Nested Structure
• PassingStructure to Function
INTRODUCTION TO
STRUCTURE
• Problem:
– How to group together a collection of data items
of different types that are logically related to a
particular entity??? (Array)
Solution: Structure
DEFINING A
STRUCTURE
• Syntax:
struct structure_name
{
data_type member_variable1;
data_type member_variable2;
………………………………; data_type
member_variableN;
};
Oncestructure_name isdeclaredasnew data type, then
variables of that typecanbe declared as:
structstructure_namestructure_variable;
Note: The members of a structure do not occupymemory until
theyare associatedwith a structure_variable.
• Example
struct student
{
char name[20];
int roll_no;
float marks;
char gender;
long intphone_no;
};
struct studentst;
• Multiple variablesof structstudenttype canbe declared
as:
struct student st1, st2,st3;
DEFINING A
STRUCTURE…
• Each variable of structure has its own copy of
member variables.
• The member variables are accessed using the
dot (.) operator or memberoperator.
• For example: st1.name is member variable
name of st1 structure variable while
st3.gender is member variable gender of st3
structure variable.
DEFINING A
STRUCTURE…
• The structure definition
variable declaration can
combinedas:
structstudent
{
charname[20];
int roll_no;
float marks;
char gender;
long intphone_no;
}st1, st2,st3;
The use of structure_name is
and
be optional.
struct
{
charname[20];
int roll_no;
float marks;
char gender;
long intphone_no;
}st1, st2,st3;
struct student
{
charname[20];
int roll_no;
float marks;
char gender;
long int phone_no;
};
void main()
{
struct student st1={“ABC", 4, 79.5, 'M', 5010670};
clrscr();
printf("NametttRoll No.tMarksttGendertPhone No.");
printf("n.........................................................................n");
printf("n %stt %dtt %ft%ct %ld", st1.name, st1.roll_no, st1.marks,
st1.gender, st1.phone_no);
getch();
}
STRUCTURE INITIALIZATION
PARTIAL INITIALIZATION
• We can initialize the first few members and
leave the remainingblank.
• However, the uninitialized members should be
only at the end of the list.
• The uninitialized members are assigned
default values asfollows:
– Zero for integer and floating pointnumbers.
– ‘0’ for characters andstrings.
struct student
{
charname[20];
int roll;
char remarks;
float marks;
};
void main()
{
struct student s1={“name", 4};
clrscr();
printf("Name=%s", s1.name);
printf("n Roll=%d", s1.roll);
printf("n Remarks=%c",s1.remarks);
printf("n Marks=%f", s1.marks);
getch();
}
STRUCTURE/PROCESSI
NG A STRUCTURE
• By using dot (.) operator or period operator or
member operator.
• Syntax:
structure_variable.member
• Here, structure_variable refers to the name of
a struct type variable and member refers to
the nameof amember within thestructure.
QUESTION
• Create a structure named student that has
name, roll and mark as members. Assume
appropriate types and size of member. Write a
program using structure to read and display
the data entered by theuser.
struct student
{
charname[20];
int roll;
float mark;
};
voidmain()
{
struct student s;
clrscr();
printf("Enter name:t");
gets(s.name);
printf("n Enter roll:t");
scanf("%d", &s.roll);
printf("n Enter marks:t");
scanf("%f", &s.mark);
printf("n Name t Roll t Markn");
printf("n...................................n");
printf("n%st%dt%f", s.name,s.roll,s.mark);
getch();
}
STRUCTURE
VARIABLES
• Twovariables of the samestructure type canbe copied in
the sameway asordinary variables.
• If student1 andstudent2belongtothe samestructure, then
the following statements arevalid:
student1=student2;
student2=student1;
• However, the statements suchas:
student1==student2
student1!=student2
are not permitted.
• If we needto compare the structure variables, we maydo
soby comparing members individually.
STUDENT
{
charname[20];
int roll;
};
void main()
{
struct student student1={“ABC",4,};
struct student student2;
clrscr();
student2=student1;
printf("nStudent2.name=%s", student2.name);
printf("nStudent2.roll=%d", student2.roll);
if(strcmp(student1.name,student2.name)==0 &&
(student1.roll==student2.roll))
{
printf("nn student1 and student2 aresame.");
}
getch();
} 16
Here, structurehas beendeclared
global i.e.outside of main()
function.Now
, any function can
access it and create a structure
variable.
HOW STRUCTURE ELEMENTS ARE
STORED?
• The elements of a structure are always stored in
contiguous memory locations.
• A structure variable reserves number of bytes
equal to sum of bytes needed to each of its
members.
• Computer stores structures using the concept of
“word boundary”. In a computer with two bytes
word boundary, the structure variables are stored
left aligned and consecutively one after the other
(with at most one byte unoccupied in between
them called slackbyte).
READING
VALUES
for(i=0; i<5; i++)
{
printf("n Enterrollnumber:");
scanf("%d", &s[i].roll_no);
printf("n Enter first name:");
scanf("%s", &s[i].f_name);
printf("n Enter Lastname:");
scanf("%s", &s[i].l_name);
}
SORTING
V
ALUES
for(i=0; i<5;i++)
{
for(j=i+1; j<5; j++)
{
if(s[i].roll_no<s[j].roll_no)
{
temp =s[i].roll_no;
s[i].roll_no=s[j].roll_no;
s[j].roll_no=temp;
}
}
}
STRUCTURE WITHINANOTHERSTRUCTURE
(NESTED STRUCTURE)
• Let us consider a structure personal_record
to store the information of apersonas:
• struct personal_record
{
char
name[20];
int
day_of_birth;
int
month_of_birth;
int year_of_birth;
float salary;
}person;
STRUCTURE WITHIN ANOTHER
STRUCTURE
(NESTED STRUCTURE)…
• Here, the structure personal_record contains a member
named birthday which itself is a structure with 3
members.Thisiscalled structure within structure.
• The members contained within the inner structure can be
accessedas:
person.birthday.day_of_birth
person.birthday
.month_of_birth
person.birthday
. year_of_birth
• The other members within the structure personal_record
are accessedasusual:
person.name
person.salary
printf("Enter name:t");
scanf("%s", person.name);
printf("nEnter day ofbirthday:t");
scanf("%d", &person.birthday.day_of_birth);
printf("nEnter month of birthday:t");
scanf("%d", &person.birthday
.month_of_birth);
printf("nEnter year of birthday:t");
scanf("%d", &person.birthday
.year_of_birth);
printf("nEnter salary:t");
scanf("%f", &person.salary);
Passing structure member to functions
• Structure members can be passed to functions
as actual arguments in function call like
ordinary variables.
• Problem: Hugenumber of structuremembers
• Example: Let us consider a structure employee
having members name, id and salary and pass
these members to afunction:
display(emp.name,emp.id,emp.salary);
VOID DISPLAY(CHAR E[],INT ID ,FLOAT
SAL)
{
printf("nNamettIDttSalaryn);
printf("%st%dt%.2f",e,id,sal);
}
PASSING WHOLE STRUCTURE TO
FUNCTIONS
 Whole structure canbe passedto afunction by the
syntax:
 function_name(structure_variable_name);
 Thecaled function hasthe form:
 return_typefunction_name(structtag_name
structure_variable_name)
 {
 … … … … …;
 }
45
TYPEDEF STATEMENT

U s e r Defined Data Types
The C language provides a facility called typedeffor
creating synonyms for previously defined data type names.
For example, the declaration:
typedef int Length;
makes the name Length a synonym (or alias) for the data
type int.
CA2_CYS101_31184422012_Arvind-Shukla.pptx

More Related Content

Similar to CA2_CYS101_31184422012_Arvind-Shukla.pptx

Introduction to structures in c lang.ppt
Introduction to structures in c lang.pptIntroduction to structures in c lang.ppt
Introduction to structures in c lang.pptshivani366010
 
data structure and c programing concepts
data structure and c programing conceptsdata structure and c programing concepts
data structure and c programing conceptskavitham66441
 
Data Structure & Algorithm - Self Referential
Data Structure & Algorithm - Self ReferentialData Structure & Algorithm - Self Referential
Data Structure & Algorithm - Self Referentialbabuk110
 
Easy Understanding of Structure Union Typedef Enum in C Language.pdf
Easy Understanding of Structure Union Typedef Enum in C Language.pdfEasy Understanding of Structure Union Typedef Enum in C Language.pdf
Easy Understanding of Structure Union Typedef Enum in C Language.pdfsudhakargeruganti
 
Basic of Structure,Structure members,Accessing Structure member,Nested Struct...
Basic of Structure,Structure members,Accessing Structure member,Nested Struct...Basic of Structure,Structure members,Accessing Structure member,Nested Struct...
Basic of Structure,Structure members,Accessing Structure member,Nested Struct...Smit Shah
 
Structures in C.pptx
Structures in C.pptxStructures in C.pptx
Structures in C.pptxBoni Yeamin
 
Pointers and Structures
Pointers and StructuresPointers and Structures
Pointers and StructuresGem WeBlog
 
VIT351 Software Development VI Unit4
VIT351 Software Development VI Unit4VIT351 Software Development VI Unit4
VIT351 Software Development VI Unit4YOGESH SINGH
 
STRUCTURES IN C PROGRAMMING
STRUCTURES IN C PROGRAMMING STRUCTURES IN C PROGRAMMING
STRUCTURES IN C PROGRAMMING Gurwinderkaur45
 

Similar to CA2_CYS101_31184422012_Arvind-Shukla.pptx (20)

Introduction to structures in c lang.ppt
Introduction to structures in c lang.pptIntroduction to structures in c lang.ppt
Introduction to structures in c lang.ppt
 
Unit4
Unit4Unit4
Unit4
 
C Language Unit-4
C Language Unit-4C Language Unit-4
C Language Unit-4
 
CP Handout#10
CP Handout#10CP Handout#10
CP Handout#10
 
data structure and c programing concepts
data structure and c programing conceptsdata structure and c programing concepts
data structure and c programing concepts
 
Data Structure & Algorithm - Self Referential
Data Structure & Algorithm - Self ReferentialData Structure & Algorithm - Self Referential
Data Structure & Algorithm - Self Referential
 
Easy Understanding of Structure Union Typedef Enum in C Language.pdf
Easy Understanding of Structure Union Typedef Enum in C Language.pdfEasy Understanding of Structure Union Typedef Enum in C Language.pdf
Easy Understanding of Structure Union Typedef Enum in C Language.pdf
 
Structure & union
Structure & unionStructure & union
Structure & union
 
Structures and Unions
Structures and UnionsStructures and Unions
Structures and Unions
 
Structures
StructuresStructures
Structures
 
structure1.pdf
structure1.pdfstructure1.pdf
structure1.pdf
 
Basic of Structure,Structure members,Accessing Structure member,Nested Struct...
Basic of Structure,Structure members,Accessing Structure member,Nested Struct...Basic of Structure,Structure members,Accessing Structure member,Nested Struct...
Basic of Structure,Structure members,Accessing Structure member,Nested Struct...
 
structure.ppt
structure.pptstructure.ppt
structure.ppt
 
Session 6
Session 6Session 6
Session 6
 
Structures in C.pptx
Structures in C.pptxStructures in C.pptx
Structures in C.pptx
 
Pointers and Structures
Pointers and StructuresPointers and Structures
Pointers and Structures
 
VIT351 Software Development VI Unit4
VIT351 Software Development VI Unit4VIT351 Software Development VI Unit4
VIT351 Software Development VI Unit4
 
User defined data types.pptx
User defined data types.pptxUser defined data types.pptx
User defined data types.pptx
 
Structures
StructuresStructures
Structures
 
STRUCTURES IN C PROGRAMMING
STRUCTURES IN C PROGRAMMING STRUCTURES IN C PROGRAMMING
STRUCTURES IN C PROGRAMMING
 

Recently uploaded

VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...Suhani Kapoor
 
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...shivangimorya083
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...Florian Roscheck
 
定制英国白金汉大学毕业证(UCB毕业证书) 成绩单原版一比一
定制英国白金汉大学毕业证(UCB毕业证书)																			成绩单原版一比一定制英国白金汉大学毕业证(UCB毕业证书)																			成绩单原版一比一
定制英国白金汉大学毕业证(UCB毕业证书) 成绩单原版一比一ffjhghh
 
Call Girls In Mahipalpur O9654467111 Escorts Service
Call Girls In Mahipalpur O9654467111  Escorts ServiceCall Girls In Mahipalpur O9654467111  Escorts Service
Call Girls In Mahipalpur O9654467111 Escorts ServiceSapana Sha
 
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptxEMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptxthyngster
 
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Callshivangimorya083
 
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Jack DiGiovanna
 
04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationshipsccctableauusergroup
 
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Callshivangimorya083
 
Unveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data AnalystUnveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data AnalystSamantha Rae Coolbeth
 
Aminabad Call Girl Agent 9548273370 , Call Girls Service Lucknow
Aminabad Call Girl Agent 9548273370 , Call Girls Service LucknowAminabad Call Girl Agent 9548273370 , Call Girls Service Lucknow
Aminabad Call Girl Agent 9548273370 , Call Girls Service Lucknowmakika9823
 
Ukraine War presentation: KNOW THE BASICS
Ukraine War presentation: KNOW THE BASICSUkraine War presentation: KNOW THE BASICS
Ukraine War presentation: KNOW THE BASICSAishani27
 
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
Schema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfSchema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfLars Albertsson
 
RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998YohFuh
 
100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptxAnupama Kate
 

Recently uploaded (20)

VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...
 
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
 
定制英国白金汉大学毕业证(UCB毕业证书) 成绩单原版一比一
定制英国白金汉大学毕业证(UCB毕业证书)																			成绩单原版一比一定制英国白金汉大学毕业证(UCB毕业证书)																			成绩单原版一比一
定制英国白金汉大学毕业证(UCB毕业证书) 成绩单原版一比一
 
Call Girls In Mahipalpur O9654467111 Escorts Service
Call Girls In Mahipalpur O9654467111  Escorts ServiceCall Girls In Mahipalpur O9654467111  Escorts Service
Call Girls In Mahipalpur O9654467111 Escorts Service
 
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptxEMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
 
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
 
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
 
04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships
 
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
 
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
 
Unveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data AnalystUnveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data Analyst
 
Aminabad Call Girl Agent 9548273370 , Call Girls Service Lucknow
Aminabad Call Girl Agent 9548273370 , Call Girls Service LucknowAminabad Call Girl Agent 9548273370 , Call Girls Service Lucknow
Aminabad Call Girl Agent 9548273370 , Call Girls Service Lucknow
 
Ukraine War presentation: KNOW THE BASICS
Ukraine War presentation: KNOW THE BASICSUkraine War presentation: KNOW THE BASICS
Ukraine War presentation: KNOW THE BASICS
 
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
 
Schema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfSchema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdf
 
Decoding Loan Approval: Predictive Modeling in Action
Decoding Loan Approval: Predictive Modeling in ActionDecoding Loan Approval: Predictive Modeling in Action
Decoding Loan Approval: Predictive Modeling in Action
 
꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...
꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...
꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...
 
RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998
 
100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx
 

CA2_CYS101_31184422012_Arvind-Shukla.pptx

  • 1. GURUNANAK INSTITUTE OF TECHNOLOGY COURSE: B.SC. CYBER SECURITY BATCH: 1ST SEMESTER (1ST YEAR) C PROGRAMMING LANGUAGE CYS101 SUBMITEFD BY: ARVIND SHUKLA ROLL NUMBER:31184422012 REGISTRATION NUMBER
  • 2. STRUCTURE • A structure is a collection of variables of  different data types under asinglename.  The variables are called members of the structure.  Thestructure isalso called auser-defined data type.
  • 3. STRUCTURE Contents: • Introduction • Defining a structure • Structure Initialization • Partial Initialization • Structure Variables • Reading Values • Sorting Values • Nested Structure • PassingStructure to Function
  • 4. INTRODUCTION TO STRUCTURE • Problem: – How to group together a collection of data items of different types that are logically related to a particular entity??? (Array) Solution: Structure
  • 5. DEFINING A STRUCTURE • Syntax: struct structure_name { data_type member_variable1; data_type member_variable2; ………………………………; data_type member_variableN; }; Oncestructure_name isdeclaredasnew data type, then variables of that typecanbe declared as: structstructure_namestructure_variable; Note: The members of a structure do not occupymemory until theyare associatedwith a structure_variable.
  • 6. • Example struct student { char name[20]; int roll_no; float marks; char gender; long intphone_no; }; struct studentst; • Multiple variablesof structstudenttype canbe declared as: struct student st1, st2,st3;
  • 7. DEFINING A STRUCTURE… • Each variable of structure has its own copy of member variables. • The member variables are accessed using the dot (.) operator or memberoperator. • For example: st1.name is member variable name of st1 structure variable while st3.gender is member variable gender of st3 structure variable.
  • 8. DEFINING A STRUCTURE… • The structure definition variable declaration can combinedas: structstudent { charname[20]; int roll_no; float marks; char gender; long intphone_no; }st1, st2,st3; The use of structure_name is and be optional. struct { charname[20]; int roll_no; float marks; char gender; long intphone_no; }st1, st2,st3;
  • 9. struct student { charname[20]; int roll_no; float marks; char gender; long int phone_no; }; void main() { struct student st1={“ABC", 4, 79.5, 'M', 5010670}; clrscr(); printf("NametttRoll No.tMarksttGendertPhone No."); printf("n.........................................................................n"); printf("n %stt %dtt %ft%ct %ld", st1.name, st1.roll_no, st1.marks, st1.gender, st1.phone_no); getch(); } STRUCTURE INITIALIZATION
  • 10. PARTIAL INITIALIZATION • We can initialize the first few members and leave the remainingblank. • However, the uninitialized members should be only at the end of the list. • The uninitialized members are assigned default values asfollows: – Zero for integer and floating pointnumbers. – ‘0’ for characters andstrings.
  • 11. struct student { charname[20]; int roll; char remarks; float marks; }; void main() { struct student s1={“name", 4}; clrscr(); printf("Name=%s", s1.name); printf("n Roll=%d", s1.roll); printf("n Remarks=%c",s1.remarks); printf("n Marks=%f", s1.marks); getch(); }
  • 12. STRUCTURE/PROCESSI NG A STRUCTURE • By using dot (.) operator or period operator or member operator. • Syntax: structure_variable.member • Here, structure_variable refers to the name of a struct type variable and member refers to the nameof amember within thestructure.
  • 13. QUESTION • Create a structure named student that has name, roll and mark as members. Assume appropriate types and size of member. Write a program using structure to read and display the data entered by theuser.
  • 14. struct student { charname[20]; int roll; float mark; }; voidmain() { struct student s; clrscr(); printf("Enter name:t"); gets(s.name); printf("n Enter roll:t"); scanf("%d", &s.roll); printf("n Enter marks:t"); scanf("%f", &s.mark); printf("n Name t Roll t Markn"); printf("n...................................n"); printf("n%st%dt%f", s.name,s.roll,s.mark); getch(); }
  • 15. STRUCTURE VARIABLES • Twovariables of the samestructure type canbe copied in the sameway asordinary variables. • If student1 andstudent2belongtothe samestructure, then the following statements arevalid: student1=student2; student2=student1; • However, the statements suchas: student1==student2 student1!=student2 are not permitted. • If we needto compare the structure variables, we maydo soby comparing members individually.
  • 16. STUDENT { charname[20]; int roll; }; void main() { struct student student1={“ABC",4,}; struct student student2; clrscr(); student2=student1; printf("nStudent2.name=%s", student2.name); printf("nStudent2.roll=%d", student2.roll); if(strcmp(student1.name,student2.name)==0 && (student1.roll==student2.roll)) { printf("nn student1 and student2 aresame."); } getch(); } 16 Here, structurehas beendeclared global i.e.outside of main() function.Now , any function can access it and create a structure variable.
  • 17. HOW STRUCTURE ELEMENTS ARE STORED? • The elements of a structure are always stored in contiguous memory locations. • A structure variable reserves number of bytes equal to sum of bytes needed to each of its members. • Computer stores structures using the concept of “word boundary”. In a computer with two bytes word boundary, the structure variables are stored left aligned and consecutively one after the other (with at most one byte unoccupied in between them called slackbyte).
  • 18. READING VALUES for(i=0; i<5; i++) { printf("n Enterrollnumber:"); scanf("%d", &s[i].roll_no); printf("n Enter first name:"); scanf("%s", &s[i].f_name); printf("n Enter Lastname:"); scanf("%s", &s[i].l_name); }
  • 19. SORTING V ALUES for(i=0; i<5;i++) { for(j=i+1; j<5; j++) { if(s[i].roll_no<s[j].roll_no) { temp =s[i].roll_no; s[i].roll_no=s[j].roll_no; s[j].roll_no=temp; } } }
  • 20. STRUCTURE WITHINANOTHERSTRUCTURE (NESTED STRUCTURE) • Let us consider a structure personal_record to store the information of apersonas: • struct personal_record { char name[20]; int day_of_birth; int month_of_birth; int year_of_birth; float salary; }person;
  • 21. STRUCTURE WITHIN ANOTHER STRUCTURE (NESTED STRUCTURE)… • Here, the structure personal_record contains a member named birthday which itself is a structure with 3 members.Thisiscalled structure within structure. • The members contained within the inner structure can be accessedas: person.birthday.day_of_birth person.birthday .month_of_birth person.birthday . year_of_birth • The other members within the structure personal_record are accessedasusual: person.name person.salary
  • 22. printf("Enter name:t"); scanf("%s", person.name); printf("nEnter day ofbirthday:t"); scanf("%d", &person.birthday.day_of_birth); printf("nEnter month of birthday:t"); scanf("%d", &person.birthday .month_of_birth); printf("nEnter year of birthday:t"); scanf("%d", &person.birthday .year_of_birth); printf("nEnter salary:t"); scanf("%f", &person.salary);
  • 23. Passing structure member to functions • Structure members can be passed to functions as actual arguments in function call like ordinary variables. • Problem: Hugenumber of structuremembers • Example: Let us consider a structure employee having members name, id and salary and pass these members to afunction:
  • 24. display(emp.name,emp.id,emp.salary); VOID DISPLAY(CHAR E[],INT ID ,FLOAT SAL) { printf("nNamettIDttSalaryn); printf("%st%dt%.2f",e,id,sal); }
  • 25. PASSING WHOLE STRUCTURE TO FUNCTIONS  Whole structure canbe passedto afunction by the syntax:  function_name(structure_variable_name);  Thecaled function hasthe form:  return_typefunction_name(structtag_name structure_variable_name)  {  … … … … …;  } 45
  • 26. TYPEDEF STATEMENT  U s e r Defined Data Types The C language provides a facility called typedeffor creating synonyms for previously defined data type names. For example, the declaration: typedef int Length; makes the name Length a synonym (or alias) for the data type int.