Course Content
History of C Language
0/2
Basic Structure of C Program
0/1
Types of Errors
0/1
Language Fundamentals
0/1
Data Types and Modifiers
0/1
Programming with C Language
About Lesson

We learnt about basic data types of C language i.e. int, float, double and char.  Those are useful to specify the characteristics of variable declared out of them.  For example, the data type ‘int’ specifies 2 bytes of memory to be allocated and rounded numbers only allowed to store in that memory.  But is there any way to define our own data type in C language?

We understood that we can declare an array out of any basic data types.  But all the elements of array can store only same type of data.  What is the solution when we want to store different types of related data element as a single item.  For example, we want to store a record of student i.e. his name, RollNo, marks etc.  You can understand name can be declared as character string, rollno as integer and marks as float.  But these three variables belong to same person.  It is worth store them a single variable.  But this cannot be stored as an array.  The solution of above mentioned two problems is defining our own data type. 

What is a Structure?

Structure in C language allows us to define user-defined data types and then declare variables out of them.  Each structure can have different data items of different types.  Each variable declared out of the structure have a copy of all the data items in structure definition.  Each data item existed in structure definition are known as members of structure.  The variable declared out of the structure can access its copy of members with dot (.) operator.

Data type vs data structure

Data type is a keyword used to specify characteristics and valid operations for the variable declared out it.  But a data structure is an arrangement of data which can be any basic or user-defined data type.  Data structure specifies relationship between data elements regarding their storage and accessibility.  Array is homogenous data structure while Structure is heterogenous user-defined data type.

There are three syntaxes to define a structure data type.

Syntax1:

struct structName

{

Datatype Member1;

Datatype Member2;

Datatype member N;

};

struct structName var1; //declaration of structure variable

Syntax2:

struct structName

{

Datatype Member1;

Datatype Member2;

Datatype member N;

}var1;

Syntax3:

typedef struct

{

Datatype Member1;

Datatype Member2;

Datatype member N;

}structName;

structName var1;

//Example program showing how structure can be defined and used

#include

typedef struct

{

int sno;

float m1,m2,m3;

}student;

/*program*/

void main()

{

student st={123,80,90,70};//declaration and initialisation of student variable st

void display(int, float, float, float); //declaration of function display()

display(st.sno,st.m1,st.m2,st.m3); //invocation of display function

}

void display(int sno,float s1, float s2, float s3) //definition of display function

{

printf(“\nrollNo: %d\nsub1 : %f\nsub2 : %f\nsub3 : %f”,sno,s1,s2,s3);

}

/*Program to store and retrieve the values from the student structure*/

#include

typedef struct{ //definition of student structure(datatype)

int roll_no;

char name[20];

char course[20];

float marks_obtained ;

}student;

void main()

{

student s1 ; //declaration of student variable s1

printf (“Enter the student roll number:”);

scanf (“%d”,&s1.roll_no);

printf (“\nEnter the student name: “);

scanf (“%s”,s1.name);

printf (“\nEnter the student course”);

scanf (“%s”,s1.course);

printf (“Enter the student percentage\n”);

scanf (“%f”,&s1.marks_obtained);

printf (“\nData entry is completed”);

printf ( “\nThe data entered is as follows:\n”);

printf (“\nThe student roll no is %d”,s1.roll_no);

printf (“\nThe student name is %s”,s1.name);

printf (“\nThe student course is %s”,s1.course);

printf (“\nThe student percentage is %f”,s1.marks_obtained);

}

INITIALIZING STRUCTURES

/* Program to illustrate to access the values of the structure initialized with some

initial values*/

#include

struct telephone{

int tele_no;

int cust_code;

char cust_name[20];

char cust_address[40];

int bill_amt;

};

main()

{

struct telephone tele = {2314345,

5463,

“Ram”,

“New Delhi”,2435 };

printf(“The values are initialized in this program.”);

printf(“\nThe telephone number is %d”,tele.tele_no);

printf(“\nThe customer code is %d”,tele.cust_code);

printf(“\nThe customer name is %s”,tele.cust_name);

printf(“\nThe customer address is %s”,tele.cust_address);

printf(“\nThe bill amount is %d”,tele.bill_amt);

}

You cannot copy content of this page