Saturday, 14 July 2018

Structures in C briefly explain with program of structure

A structure is data type which collect different atoms of the information that comprise a given entity and structure is also known as the collection of heterogeneous element . Structure is mainly used in the offices , schools . A structure contains a number of data types grouped together . These  data type may be same or different .

                       if you want to store data about the student then there information is like name , age , roll no , fees , class etc then there are uses of different data structure like string , int , float , char so for that we have use these different methods.
1 . use a structure variable.
2 . you may use array for each different variables.

Program to use of structure :

# include<stdio.h>
 void main ()
struct student
float fee ;
int roll ;
char name ;
struct student s1 , s2 s3 ;
printf ("\n enter fee , roll and name of 3 students:") ;
scanf ("%f %d %c", &s1,fee, &s1,roll. &s1,name) ;
scanf ("%f %d %c", &s2,fee, &s2,roll. &s2,name) ;
scanf ("%f %d %c", &s3,fee, &s3,roll. &s3,name) ;
print ("\n Enter value are:") ;
printf ("%f %d %c", s1,fee, s1,roll, s1,name) ;
printf ("%f %d %c", s2,fee, s2,roll, s2,name) ;
printf ("%f %d %c", s3,fee, s3,roll, s3,name) ;
return 0 ;

How to uses structure in c :

(a)  declaration of a structure variables 

(b) accessing of structure element

(A)  Declaring a structure 

This is the first and important step in which we have to declare the name of variable name of variable and also write how much data types used in that given program. In the previous program we have use declaration of variable in which we have used three types of data types i.e float , char and int . Here you declare the data types as per the uses of the program. 
                                                                                             The general form for structure declaration statement is given below :

struct <structure name>
structure element 1 ;
structure element 2 ;
structure element 3 ;
struct structure name structure variable name ;

This is the way by which you can declare a structure in C. 

(B) Accessing structure element :

In structure we can access different elements of an array using a subscript . we have used in the first sample program we have written about the structure ,

structure variable.structure element

How to use array using structure :  

As we know array is collection of homogenous element ( of same type ) which have a common name and share a common property in this we have to declare more than a single elements.

/* usage of an array of structure *?
# include <stdio.h>
void main ()
struct student
char name ;
float fees ;
int roll no ;
struct student s[50] ;
int i ;
for ( i = 0 ; i < 50 ; i++ )
printf ("\n enter name, fees and roll no:") ;
scanf ("%c %f %d", &s[i].name, &s[i].fees, &s[i].roll no) ;
for ( i = 0 ; i < 50 ; i++ )
printf ("%C %f %d\n", s[i].name,s[i].fees, s[i].roll no ) ;

By this method you can use an Array using structure element we further discuss in the next post .

No comments:

Post a Comment

Featured post

Program to find maximum of three number using user define function in C++

A function can be define by the user as per the requirement of the user or programmer . To implement a user define function , a programmer ...