You are on page 1of 10

FaaDoOEngineers.

com
Engaging Peers, Inspiring Careers!

India’s No.1 website for:
• IIT-JEE/AIEEE preparation resources • Coaching centre packages • Engineering Major and Minor projects • Seminar reports

• Paper presentations
• EBOOKS • Resumes/CVs, and so much more…

each book is an object. .C Data Types: Primary data types Derived data types User-defined data types Derived Types Function Type Array Type Pointer Type Structure Type Union Type Array – Collection of one or more related variables of similar data type grouped under a single name Structure – Collection of one or more related variables of different data types. The characteristics of record are called the members of the structure. a record is implemented with a derived data type called structure. In C. grouped under a single name In a Library. and its characteristics like title. author. A record is group of fields and each field represents one characteristic. price are grouped and represented by one record. no of pages. The characteristics are different types and grouped under a aggregate variable of different types.

Book-1 BookID: 1211 Title : C Primer Plus Author : Stephen Prata Pages : 984 Price : Rs. 125. char author[40] . char title[50] . }.00 book bookid title author pages book_id title author pages price integer 2 bytes 50 bytes Array of 50 characters Array of 40 characters integer float 2 bytes 4 bytes 40 bytes price Memory occupied by a Structure variable STRUCTURE. int pages . …. 305. float price . 585. ….00 Book-3 BookID: 1213 Title : C By Example Author : Greg Perry Pages : 498 Price : Rs. …. struct book { int book_id .00 Book-2 BookID: 1212 Title : The ANSI C Programming Author : Dennis Ritchie Pages : 214 Price : Rs. Structure tag .BOOK struct < structure_tag_name > { data type < member 1 > data type < member 2 > …. data type < member N > }.

percentage). float percentage. scanf(“%f”. Declaring a Structure Variable struct student s1. Kishore “ ) . printf(“\nEnter the name”).s3. float percentage.percentage = 78. Reading values to members at runtime: struct student s3.s3.Avinash ”. scanf(“%s”.&s3.s2. }. printf(“\nEnter the roll no”). scanf(“%d”. 87.25 } .name).roll_no).Declaring a Structure Type struct student { int roll_no. s1. (or) struct student { int roll_no. roll_no = 1111. “ K. char name[30]. }s1.5 .s3. membership operator . Initialization of structure members individually : s1.&s3. name . printf(“\nEnter the percentage”). Initialization of structure Initialization of structure variable while declaration : struct student s2 = { 1001. char name[30].s2. strcpy ( s1. “ B.

}.salary).empid.age.name. “K.name. Initialization of Structure members individually emp1. emp2. ” S.age > emp2.Murali ” .age = 27.empid. &emp2. &emp2.salary).Ravi”). else printf(“Employee1 is junior than Employee2\n”). emp1. 32000. Accessing members of Structure printf(“Emp ID:%d\n Name:%s\n Age:%d\n Salary:%f”. } . char name[35]. scanf(“%d %s %d %f “ . int age.Implementing a Structure struct employee { int empid. if(emp1.00 } . emp1.name. &emp2.age. 31 .emp1.emp1.00.emp1.empid=1211. Reading values to members of Structure printf(“Enter the details of employee 2”). int main() { struct employee emp1. float salary.emp2 .age) printf( “ Employee1 is senior than Employee2\n” ). Declaration of Structure Type Declaration of Structure variables Declaration and initialization of Structure variable struct employee emp3 = { 1213 . emp1. strcpy(emp1.salary=30000.

year = 1978 .j++) { scanf(“%d”. Accessing Inner p1. Outer Structure int month .day = 25 . Ramesh “ ) . for(j=0. }. } } . }.j<3.sub[j]). } } for(i=0. strcpy ( p1. struct date b_day . int age . } OUTPUT: Enter student 1 marks: 60 60 60 Enter student 2 marks: 70 70 70 Enter student 3 marks: 90 90 90 Total marks of student 1 is: 180 Total marks of student 2 is: 240 Total marks of student 3 is: 270 int main( ) { struct student s[3]. Nesting of structures struct date { int day .i++) { s[i]. Structure members p1. month = 8 . int year . for(j=0.i+1). age = 32 .total +=s[i].b_day.j. int main( ) { Inner Structure struct person p1.j++) { s[i].i<3.sub[j].b_day.i<3. p1. for(i=0.Arrays And structures struct student { int sub[3] . } printf(“\nTotal marks of student %d is: %d”. }.total =0.i++) { printf(“\n\nEnter student %d marks:”.name . i+1.j<3.&s[i]. “S. int i. p1.s[i]. struct person { char name[40]. int total .b_day.total ).

name . s1. f. s2 node is linked to s1 node Prints P. NULL } .> name ) . s1. strcpy ( s1. struct student_node *next . A self referential structure is one that includes at least one member which is a pointer to the same structure type. . “P. struct student_node s2 = { 1111. int main( ) { struct student_node s1 .next .Kiran Prints B. } int main ( ) { struct fraction f1 = { 7. With self referential structures. }. int denominator . } OUTPUT: 7 / 12 } Self referential structures struct student_node { int roll_no . roll_no = 1234 .Kiran “ ) .Mahesh printf ( “ %s “. we can create very useful data structures such as linked -lists.structures and functions struct fraction { int numerator . “B.Mahesh”. name ) .denominator ) . printf ( “ %s “ . 12 } . show ( f1 ) . void show ( struct fraction f ) { printf ( “ %d / %d “. trees and graphs. next = & s2 . s1. char name [25] .numerator. }. f. s1.

ptr<inventory+3.ptr->prodid). ptr->name).> prodid = 111 .ptr->name). } } Accessing structure members through pointer : i) Using . Name. printf(“Read Product Details : \n"). strcpy ( ( *ptr ) . prodid = 111 . }.Pointer to a structure struct product { int prodid. struct product *ptr. } printf("\noutput\n").> name .ptr++) { printf("\n\nProduct ID :%5d". “Pencil”) .> ( arrow ) operator : ptr .ptr<inventory +3.ptr++) { scanf("%d %s". ii) Using . int main() { struct product inventory[3]. for(ptr=inventory. for(ptr = inventory. strcpy( ptr . printf("\nName : %s". “Pen”) . ( dot ) operator : ( *ptr ) . &ptr->prodid. Read Product Details : 111 Pen 112 Pencil 113 Book Print Product Details : Product ID : 111 Name : Pen Product ID : 112 Name : Pencil Product ID : 113 Name : Book . char name[20].

printf ( “ %d bytes “. which is similar to the structure and contains number of members like structure. } Output : 7 bytes 4 bytes Memory allotted to structure student Address 5000 5001 5002 5003 5004 5005 5006 avg rollno Total memory occupied : 7 bytes grade Memory allotted to union pupil Address 5000 5001 5002 5003 rollno avg grade Total memory occupied : 4 bytes . members of union share the same memory. The amount of storage allocated to a union is sufficient to hold its largest member. struct student { int rollno.A union is a structure all of whose members share the same memory Union is a variable. }. union pupil { int rollno. float avg . int main() { struct student s1 . sizeof ( union pupil ) ) . char grade. float avg . union pupil p1. sizeof ( struct student ) ) . }. In the structure each member has its own memory location whereas. printf ( “ %d bytes “. char grade .

FaaDoOEngineers. Inspiring Careers! India’s No. and so much more… .com Engaging Peers.1 website for: • IIT-JEE/AIEEE preparation resources • Coaching centre packages • Engineering Major and Minor projects • Seminar reports • Paper presentations • EBOOKS • Resumes/CVs.