Structure in C

Structure contains a number of data items(int,float,char) grouped together. These data items may or may not be of same type. So we can say that structure is a collection of heterogeneous(different) data items.

 

A structure is a convenient way of grouping several pieces(like employee may contain employee name, number,department,date of joining etc) of related information together.

The “struct” keyword, identifies the beginning of a structure definition.

Example1...

struct student //Declares structure having name student

{

char name[10]; //structure element

int rn;     //structure element

}

main()

{

struct student s={"abc",1013}; //initialize the values to members

clrscr();

printf("name=%s\n",s.name); //print

printf("rollno=%d",s.rn);

getch();

}

structure in c

Example 2..

struct student

{

char name[10];

int rn;

}

main()

{

struct student s;   //creating structure variable

clrscr();

printf("enter name & roll no=");

scanf("%s%d",&s.name,&s.rn);

printf("name=%s",s.name);

printf("roll no=%d",s.rn);

getch();

}

 Structure in C

Example 3...

#include<stdio.h>

struct bio

{

char name[20];

int age;

float sal;

}

e1;

main()

{

struct bio e1={"abc",30,1500.65};

clrscr();

show(e1);

getch();

}

show(struct bio e1)

{

printf("%s\t%d\t%f",e1.name,e1.age,e1.sal);

}

Structure in C

About Us

Rekha SetiaRekha Setia is a passionate blogger of Extra Computer Notes. if you have any ideas or any request me @