Welcome !!

NICETECHVIDYA

Tutorials

Web Designs

You are welcome to Aptech Designers, designed by Pratik from Nepal.

Creativity is my passion. Logo describes your details about company.

Graphics Design needs vision to create yours unique digital concept.

Responsive and All Browser Compatible Dynamic Web Layouts our target.

Home Python C Language J A V A Our Courses
This WebSite is under Construction....

Friday, 11 March 2022

66.User defined types: struct, Defining a structure, Creating a variable, Initializing while declaration of a structure variable

 

User defined types:

 

Need of user defined types:

While developing real time applications it is needed to store multiple values of different types in a common memory allocation. For example details of a book.

Any variable belongs to primary type is capable to store a single value.

An array is capable to store multiple values but of the same type.

C language has no specific type to store multiple values of different types in a common memory allocation.

But provides the following user defined types to define our own data types.

  1. struct
  2. union
  3. typedef
  4. enum
  5. bitfields

 

struct:

It is a user defined type used to define our own data type to store multiple values of different types in a common memory.

Struct is a heterogeneous collection of members defined with a name.

The definition of struct doesn't allocate the memory allocation of members rather behaves like a template.

An instance or a variable belongs to the struct allocates the memory allocation of members.

Every member of variable is accessed using the direct member access operator (.).

 

Defining a structure:

Keyword struct is used to declare the structure.

It should be identified with a name which must be a valid identifier.

All the members are declared with in a block { }

The definition must be terminated with a semi-colon (;)

 

 

Syntax:

struct   <name>

{

    ---------------

    ---------------    List of members

    ---------------

};

 

 

 

 

 

 

 

Example:

 

 

 

 


                                                             

 

 

                                                                                                       

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Creating a variable:

Definition of structure will not allocate the memory allocation of members.

Only the instance or a variable belongs to structure allocates the memory allocation of members.

Every variable has a separate set of members.

Every member in a variable is accessed by using direct member access (.) operator.

 

Syntax:

struct   <name of type>  <name of variable>

 

Example:

 

 

 

 

 

 

 

 


Specification1:

Accept the radios of a circle and print the area and circumference.

 

Program:

#define PI 3.14

#include<stdio.h>

struct circle

{

    int rad;

    float area,cir;

};

void main()

{

    struct circle x;

    clrscr();

    printf("Enter the radios:");

    scanf("%d",&x.rad);

    x.area=PI*x.rad*x.rad;

    x.cir=2*PI*x.rad;

    printf("Area of circle %f",x.area);

    printf("\n Circumference of circle %f",x.cir);

    getch();

}

 

Execution:

Enter the radios: 17

Area of circle 907.460022

Circumference of circle 106.760002

 

Example explained:

circle is a user defined type defined with the struct

"x" is the variable belongs to "struct circle" has all the members rad, area and cir

Members are accessed with direct member access operator (.)

 

Specification2:

Accept the empno, ename, sal of an employee and print the pay slip details if the H.R.A is 13% of salary and D.A is 15% of salary.

Program:

#include<stdio.h>

struct employee

{

    int empno;

    char ename[50];

    int sal;

    float hra,da,gross;

};

void main()

{

     struct employee x;

     clrscr();

     printf("Empno:");

     scanf("%d",&x.empno);

     printf("Employee Name:");

     scanf("%s",x.ename);

     printf("Salary:");

     scanf("%d",&x.sal);

     x.hra=(float)x.sal*13/100;

     x.da=(float)x.sal*15/100;

     x.gross=x.sal+x.hra+x.da;

     printf("\nPay Slip Details:\n");

     printf("Empno:%d\n",x.empno);

     printf("Employee Name:%s\n",x.ename);

     printf("Salary:%d\n",x.sal);

     printf("House Rent Allowance: %f\n",x.hra);

     printf("Dearness Allowance: %f\n",x.da);

     printf("Gross Salary: %f\n",x.hra);

     getch();

}

 

Execution:

Empno: 1000

Employee Name: Smith

Salary: 7500

 

Pay Slip Details:

Empno: 1000

Employee Name: Smith

Salary: 7500

House Rent Allowance: 975.000000

Dearness Allowance: 1125.000000

Gross Salary: 9600.000000

 

 

 

 

 

 

 

 

 

 

 

A structure variable can be declared while its definition.

 

Example:

#include<stdio.h>

struct product

{

     char pname[20];

     float price;                                                                               

     int qty;

     float bill;

     float dis;

    float netbill;

}a;  

void main()

{

    clrscr();

    printf("Product Name:");

    scanf("%s",a.pname);

    printf("Price:");

    scanf("%f",&a.price);

    printf("Quantity:");

    scanf("%d",&a.qty);

    a.bill=a.price*a.qty;

    a.dis=a.bill*5/100;

    a.netbill=a.bill-a.dis;

    printf("Billing details:\n");

    printf("Product Name:%s\nPrice:%f\nQuantity:%d\n",a.pname,a.price,a.qty);

    printf("Bill:%f\nDiscount:%f\nNet bill:%f",a.bill,a.dis,a.netbill);

    getch();

}

 

Execution:

Product Name: Burger

Price: 85.000000

Quantity: 5

Billing details:

Product Name: Burger

Price: 85.000000

Quantity: 5

Bill: 425.000000

Discount: 21.250000

Net bill: 403.750000

 

Example explained:

In the above example structure variable "a" is declared while defining the structure.

 

Initializing while declaration of a structure variable:

A structure variable can be initialized with a set of values while its declaration.

 

Example:

struct address

{

    char name[50];

    char street[50];

    char city[50];

    long int pin;

};

void main()

{

     struct address x={"Smith","Road-43","Kavali",524201};

     clrscr();

     printf("Address:\n");

     printf("Name:%s\nStreet:%s\nCity:%s\nPin:%ld",x.name,x.street,x.city,x.pin);

     getch();

}

 

Output:

Address:

Name:Smith

Street:Road-43

City:Kavali

Pin:524201

 

Example explained:

Here the set of values are initialized according the sequence of members in structure variable.

0 comments:

Post a Comment