Chapter 10-p - Example 10.1 Define a structure type, struct...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Example 10.1 Define a structure type, struct personal that would contain person name, date of joining and salary. Using this structure, write a program to read this information for one person from the keyboard and print the same on the screen. Structure definition along with the program is shown in Fig.10.1. The scanf and printf functions illustrate how the member operator `.' is used to link the structure members to the structure variables. The variable name with a period and the member name is used like an ordinary variable. DEFINING AND ASSIGNING VALUES TO STRUCTURE MEMBERS Program struct personal { char name[20]; int day; char month[10]; int year; float salary; }; main() { struct personal person; printf("Input Values\n"); scanf("%s %d %s %d %f",, &, person.month, &person.year, &person.salary); printf("%s %d %s %d %f\n",,, person.month, person.year, person.salary); }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Output Input Values M.L.Goel 10 January 1945 4500 M.L.Goel 10 January 1945 4500.00 Fig.10.1 Defining and accessing structure members Example 10.2 Write a program to illustrate the comparison of structure variables. The program shown in Fig.10.2 illustrates how a structure variable can be copied into another of the same type. It also performs member-wise comparison to decide whether two structure variables are identical. COMPARISON OF STRUCTURE VARIABLES Program struct class { int number; char name[20]; float marks; }; main() { int x; struct class student1 = {111,"Rao",72.50}; struct class student2 = {222,"Reddy", 67.00}; struct class student3; student3 = student2; x = ((student3.number == student2.number) && (student3.marks == student2.marks)) ? 1 : 0; if(x == 1) { printf("\nstudent2 and student3 are same\n\n"); printf("%d %s %f\n", student3.number,, student3.marks); }
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 05/14/2010 for the course CS SS ZG653 taught by Professor Shanta during the Spring '09 term at Birla Institute of Technology & Science.

Page1 / 7

Chapter 10-p - Example 10.1 Define a structure type, struct...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online