click on this link......

LinkGrand.com

Sunday, 31 March 2013

Structure in C

Structure
    A structure is a user defined data type that can store many values of different datatypes under a single variable name.  A structure can thus easily represent a real life object.
e.g.
    To represent a student on your computer, we can define a structure to store the students rollno, name, age, eng, hin, mar marks under a single variable name

struct student
{
 int rollno;
 char name[30];
 float age, eng, hin, mar;
};

The above datatype called "struct student" will occupy (2+30+4+4+4+4) = 48 bytes in memory.  Thus "struct student" is a new datatype just like int or char etc.  To declare a variable of this data type,
 struct student a;
Then "a" is a variable that will occupy 48 bytes in memory. The structure elements of variable "a" are rollno, name, age, eng, hin and mar.
To access a variables structure elements, use the dot operator as
e.g.
  a.name
 a.age

Create the following structures in C
1) For an employee with the following structure elements
     no, name, age, basicsalary, hra, da, pf, ld, netsal
Ask for no, name, age and basicsalary and calculate
hra (0.1), da (0.01), pf (0.2), ld(0.02), netsal(basicsalary + hra +da - pf -ld)

2) For a book with the following structure elements
    bookno, name, price, noofpages, publication, author
Ask for all details and print them.

3) For a coordinate in the 2 dimension
    x, y
Ask for a coordinate and print it. Find its quadrant.

4) For a location on the globe
    latitude, longitude
Ask for a location and print it.

5) For a Teacher
    no, name, qualification, age, experience.
Ask for a teachers information and find if the
 - teacher has an experience of 5 years or above
 - age > 30

No comments:

Post a Comment