unit v structures and unions

84
UNIT V STRUCTURES AND UNIONS Introduction – need for structure data type – structure definition – Structure declaration – Structure within a structure - Union - Programs using structures and Unions – Storage classes, Pre-processor directives.

Upload: pahana

Post on 04-Feb-2016

33 views

Category:

Documents


0 download

DESCRIPTION

UNIT V STRUCTURES AND UNIONS Introduction – need for structure data type – structure definition – Structure declaration – Structure within a structure - Union - Programs using structures and Unions – Storage classes, Pre-processor directives. Introduction - PowerPoint PPT Presentation

TRANSCRIPT

Structure

UNIT V STRUCTURES AND UNIONS Introduction need for structure data type structure definition Structure declaration Structure within a structure - Union - Programs using structures and Unions Storage classes, Pre-processor directives.

IntroductionC programming language which has the ability to divide the data into different types. The type of a variable determine the what kind of values it may take on. The various data types areSimple Data type Integer, Real, Void, CharStructured Data typeArray, StringsUser Defined Data type Enum, Structures, Unions2Need for a Structure Data TypeA structure is a user defined data type that groups logically related data items of different data types into a single unit. All the elements of a structure are stored at contiguous memory locations. A variable of structure type can store multiple data items of different data types under the one name. As the data of employee in company that is name, Employee ID, salary, address, phone number is stored in structure data type. 3DefinitionA Structure is a collection of different data items, that are stored under a common name.Syntax:struct structure_name{structure element1;structure element2;.};

Example:struct stud{int sno;char name[10];int mark;};

struct stud s;Example#include#includestruct stud{int regno;char name[10];int m1;int m2;int m3;};struct stud s;void main(){ float tot,avg; printf("\nEnter the student regno,name,m1,m2,m3:"); scanf("%d%s%d%d%d",&s.regno,&s.name,&s.m1,&s.m2,&s.m3); tot=s.m1+s.m2+s.m3; avg=tot/3; printf("\nThe student Details are:"); printf("\n%d\t%s\t%f\t%f",s.regno,s.name,tot,avg);

}

OutputEnter the student regno,name,m1,m2,m3:100aaa879878

The student Details are:100 aaa 263.000000 87.666664

Structure assignmentIt is possible to assign one structure information to another structure of same type using simple assignment statement.Example#include#includevoid main(){ struct { int a; }x,y; clrscr(); x.a=10; y=x; printf("The value of y.a is%d",y.a); getch(); }Output The value of y.a is10

Example#include#includestruct stud{int regno;char name[10],grade;int m1,m2,m3;float avg,tot;} s[10];void main(){ int i,n;printf("\nEnter the no.of students:"); scanf("%d",&n); for(i=0;i=35) s[i].grade='C'; } } printf("\nSTUDENT MARK LIST\n");printf("\nREGNO\tNAME\tTOTAL\tAvg\tGRADE");for(i=0;i