C Programs to practice - Display vehicle information using Structure

 Structures are used to create user defined data structures. It contains variables with different data types combined together.

It has a basic syntax given below...

struct structure_name

{

  datatype variablename1;

  datatype variablename2;

  .......

 datatype variable name n;

};


Next, create the variable for the structure that declared already.

Syntax for creating structure variable is given below...

struct structure_name variable_name;

using the above syntax,we can create 'n' number of structure variables. After the declaration of structure variable,you can get the input or assign the values for structures.

An example with describes the structure which displays vehicle details is given below...

#include<stdio.h>

struct vehicle {

char type[50];

char company[50];

char model[50];

};

void main()

{

struct vehicle v1={“Motor bike”,”RoyalEnfield”,”Hunter”};

struct vehicle v2={ “Car”,”Maruthi”,”Swift”};

struct vehicle v3={“Scooty”,”Tvs”,”zest”};

printf(“ Type: %s Company: %s Model :%s”, v1.type,v1.company,v1.model);

printf(“ Type: %s Company: %s Model :%s”, v2.type,v2.company,v2.model);

printf(“ Type: %s Company: %s Model :%s”, v3.type,v3.company,v3.model);

getch();

}

The output is given below..

Type : Motcomor bike Company : RoyalEnfield Model :Hunter

Type : Car  Company :Maruthi Model :Swift

Type: Scooty Company:Tvs Model : zest

These are the way to create and access structure and its variable. You can create any structures like payslip of an employee by using structures.




No comments:

Post a Comment