Basic Programming of Structure

C Programming Language / Structure in C Language

916

Program:

#include <stdio.h>  
#include <string.h>  
struct employee    
{   int id;    
    char name[50];    
}e1;  //declaring e1 variable for structure  
int main( )  
{  
   //store first employee information  
   e1.id=101;  
   strcpy(e1.name, "Rambo Azmi");//copying string into char array  
   //printing first employee information  
   printf( "employee 1 id : %d\n", e1.id);  
   printf( "employee 1 name : %s\n", e1.name);  
   return 0;  
}  

Output:

employee 1 id : 101
employee 1 name : Rambo Azmi
Press any key to continue . . .

Explanation:

Arrays allow to define type of variables that can hold several data items of the same kind. Similarly structure is another user defined data type available in C that allows to combine data items of different kinds. Structure helps to construct a complex data type in more meaningful way. It is somewhat similar to an Array. The only difference is that array is used to store collection of similar datatypes while structure can store collection of any type of data. Structure is used to represent a record. Suppose you want to store record of Student which consists of student name, address, roll number and age. You can define a structure to hold this information.

This Particular section is dedicated to Programs only. If you want learn more about C Programming Language. Then you can visit below links to get more depth on this subject.