C Program User defined data type – struct keyword – student array example

C Programming language have built in data types likes integer, float, character and its extended types as long, double, signed and unsigned. Any other data type you want to work with, you have to define yourself. like Student or Employee or Customer, Product etc , Because they are not built-in, they are called as user defined. User defined data types are data types created by developer using primitive data type, and are used for specific purpose. It is a collection of elements whose data types are different.

Examples

Student  has various attributes, like Name, Roll number, Age, Address, Standards, Division, School or College name etc. So we can create a structure named Student so each Student has all these attributes and we can access each Student by its Roll number.

Employee of a bank has various attribute, like name, employee number, salary, address, phone number. So we can create a structure named Employee so each employee has all these attributes and we can access each employee based on employee number.

User defined data types are usually defined by the user. Here the compiler doesn’t know the functionality. The programmer writes his own logic using his own functions which are user defined data types .

There are two mechanisms for declaring User Defined Types in  C: using structures and unions

using structures

Structures are aggregate data types. For example, you can create a structure which contains an integer, a float and a string.  This grouping of data types into a single structure may have particular meaning to your application.  For example, a School or college Management application will require some form of student record which will probably consist of an Roll number, Name, Age, College_name, Standards and Division. It seems logical to store these different items in such a way that they are accessible as a unit or using a single reference.  The struct keyword allows us to design such a collection of data items.

Syntax

Example

When a structure is declared, it creates a user-defined type but, no storage or memory is allocated. memory get allocated only when you create a variable of it.

To actually declare a variables of this struct student data type

Here s1, s2, s3 are the record variables of student data type. memory space is allocated for each attribute of variable.

user-defined-type-struct-student
user-defined-type-struct-student

Initialize struct type variable

init-user-defined-type-struct-student
init-user-defined-type-struct-student

Accessing members of a structure

 Any member of a structure can be accessed by dot operator. dot operator is also called as member selection operator.

To assigne value 555 to roll_number of variable s1. As example ,s1.roll_number=555;

How to Read & Write Struct type variable

OUTPUT

output-struct-student-read-write-program

DOWNLOAD CODE: c-code-struct-student-read-write-program.c (33 downloads)

How to Declare , Read and Write Array of struct type variable

Declare Array of struct type variable

Here 5 elements of student data type get declared. Every variable of array holds all the attributes or data members data type.

Read Array of struct type variable

Write Array of struct type variable

Write program to Read array of 5 students and display in tabular form

OUTPUT

output-struct-student-array-read-write-program
output-struct-student-array-read-write-program
DOWNLOAD CODE: c-code-struct-student-array-read-write-program.c (24 downloads)