Skip to main content
To Get All Information of Sarkari Jobs | Results | Admit Card | And courses ,Internships then CLICK HERE

Structure In C Language

 Introduction
  • Structure can be defined as,"A finite collection of heterogenous elements".
  • A Keyword "Struct" is used to declare  a structure.
Syntax-:
                     struct structure name
                            { datatype member name 1;
                               datatype member name 2;
                                 -
                                 -
                                 -
                               datatype member name n;
                             };
Example-:
                       struct Emp
                            { int id;
                               char name[10];
                               float salary;
                             };
  • The structure declaration, however does not allocate any memory or consume storage space.Memory allocated for the structure,when we declare a variable of the structure.
Example-:
                     struct Emp
                            {  int id;
                               char name[10];
                               float salary;
                             };
                            struct Emp e1;
  • After the compilation of above code,the memory is allocated for the structure variable e1.
techprogramiz
sizeof(e1)=4+10+4 =>18 bytes

Initialization Of Structure Variable
  • A Structure can be initialize in the same way as other datatype are initialize.Initializing a structure means assigning some constant to member of the structure.
  • A Structure can be initialize by providing legal values,according to the structure member to structure variable in the following manner.
Example-:
                    struct Emp
                            { int id;
                               char name[10];
                               float salary;
                             };
                   struct emp e1={0001,"Sudarshan",20000.78};

Accessing Structure Element
  • Individual member of a structure variable are referenced by using dot (.) notation in the following manner.
Syntax-:
                      structure variable name.member name;
Example-:
                           e1.id;
                           e1.name;
                           e1.salary;

Initialization Of Structure Variable At Runtime
  • A structure variable can also be initialize at runtime in following manner-:
Program-:

struct Emp
{ int id;
   char name[15];
   float per;
};
struct emp e1={0001,"Sudarshan",20000.78},e2;
printf("Enter the id,name and salary-:");
scanf(" %d %s %f ",&e2.id,&e2.name,&e2.salary);
printf("\n Structure Variable Have The Following Value:-\n");
printf("ID=%d,NAME=%s,SALARY=%f",e1.id,e1.name,e1.salary\n);
printf("ID=%d,NAME=%s,SALARY=%f",e2.id,e2.name,e2.salary);
getch();
}

Output-:
Enter the id,name and salary-:0002
Arun
25000.782
 Structure Variable Have The Following Value:-
ID=0001 NAME=Sudarshan SALARY=20000.78
ID=0002 NAME=Arun SALARY=25000.78

Finding Structure Size
  • The sizeof operator is a uniary operator used to calculate the sizeof data type,this operator is applied at all the data type.
  • The operator return the sizeof the variable or expression in bytes, To calculate the size of structure using the sizeof operator the following syntax is used.
Syntax-:
                Variable=sizeof(structure variable name);
Program-:

struct Emp
{ int id;
   char name[15];
   float per;
};
struct emp e1={0001,"Sudarshan",20000.78};
printf("\n Size of Emp is %d".,sizeof(e1));
getch();

Output-:
Size of Emp is 18. 


Comments


Download The Book "Let Us C" By Yashavant P.Kanetkar,Click The Below Button

-->Email Subscription

Enter Your Email Address:-

Delivered by FeedBurner

Copyright © 2020 TechProgramiz|All Right Reserved|Made By-: Sudarshan Pandey