Topics

6/recent/ticker-posts

Structures in C language


            
Structures in C 

 

Definition: It is a collection of dissimilar  type of elements. Structures are user defined data types.

 

"struct" is a keyword to define structures.

 

 

Declaration:

 

Syntax :


struct   tag_name

{

       data_type  member1;

       data_type  member2;

       data_type  member3;

     =================;

     =================;

     =================;

};   à end of statement

 

 

Eg:

            struct  Employee

            {

                        int  empNum; 

                        char empName[30];

                        float salary;

                        char dept[15];

            };

 

 

Structure is an abstract idea of something. Structure members do not acquire any memory without declaring variable for the structure

 

Declaring a structure variable:

 

       data_type  var_name;

 

E.g.: struct  Employee  emp1,  emp2;

 

in above Example, 


struct  Employee  à is data type.

emp1 and emp2  à are the structure variables.

 

Memory map of structure members:

 

emp1

                   empNum                  empName                                  salary                          dept

2 bytes

30 bytes

4bytes

15 bytes

                |------------------------------------ 51 byes -----------------------------------------------------------|

 

emp2

                   empNum                  empName                                   salary                              dept

2 bytes

30 bytes

4bytes

15 bytes

                |------------------------------------ 51 byes ------------------------------------------------------------|

 

Accessing structure members:

 

" . dot ( period operator )"   is an operator to access structure members.

 

 

emp1. empNum;                                 emp2 . empNum        

emp1. empName;                               emp2 . empName

emp1. salary;                                      emp2 . salary

emp1. dept;                                         emp2 . dept

 

 

Assignment:

emp1. empNum = 1001 ;                                            emp2 . empNum=1002;

strcpy(emp1. empName, "dsk") ;                           strcpy(emp2 . empName,"dsk1");

emp1. salary= 5000.00;                                              emp2 . salary=6000.00;

strcpy(emp1. dept,"admin");                                      strcpy(emp2 . dept,"IT");

 

emp1

                   empNum              empName                               salary                 dept

1001

dsk\0

5000.00

Admin\0

 

 

emp2

                   empNum              empName                                salary                  dept

1002

dsk1\0

6000.00

IT\0

 

Initialization:

 

struct  Employee  emp={1001,”nrit1”,5000.50,”admin”};

Reading from key board:

 

scanf(“%d %s  %f  %s”,& emp1. empNum, emp1. empName,  & emp1. salary,  emp1. dept );

 

Displaying structure variables:

 

printf(“%d %s  %f  %s”, emp1. empNum, emp1. empName,  emp1. salary,  emp1. dept );

 

program to read and display  Book details.

 

#include<stdio.h>

main()

{

struct Book

{

int id;

char title[20];

            float price;

char author[20];

            char publisher[20];

};

 

struct Book b1;

 

   printf("\n Enter Book Details");

   printf("\n Enter Id:");

   scanf("%d",&b1.id);

   printf("\n Enter Title");

   scanf("%s",b1.title);

   printf("\n Enter price");

   scanf("%f",&b1.price);

   printf("\n Enter author");

   scanf("%s",b1.author);

   printf("\n Enter publisher");

   scanf("%s",b1.publisher);

 

   printf("\n Book Details are :");

   printf("\n  Id : %d",b1.id);

   printf("\n Title         : %s",b1.title);

   printf("\n Price         : %f",b1.price);

   printf("\n Author      : %s",b1.author);

   printf("\n Publisher  : %s",b1.publisher);

 

   getch();

}

 


program to read and display five employees details:

 

#include<stdio.h>

 

struct Employee

{

   int eno;

   char ename[20];

   float salary;

   char dept[20];

};

main()

{

            struct Employee  emp[5];

            int i;


 

            for(i=0; i<5 ; i++)

            {

               printf("\n Enter emp details ");

 

               printf("\n Emp number :");

               scanf("%d",&emp[i].eno);

 

               printf("\n Emp name :");

               scanf("%s",emp[i].ename);

 

               printf("\n Emp salary :");

               scanf("%f",&emp[i].salary);

 

               printf("\n Emp Dept:");

               scanf("%s",emp[i].dept);

            }

 

            printf("\n Num \t Name \t Salary \t Dept ");

            for(i=0; i<5 ; i++)

            {

              printf("\n%d \t %s \t %f \t %s ",emp[i].eno,emp[i].ename,emp[i].salary,emp[i].dept);

            }

getch();

}


 

**Note: All the programs given here are pre compiled and runned programs 

Post a Comment

0 Comments