C-Programming

How to access the member variables of the structure?

To access the member variable of the structure we have to use dot (.) operator. The syntax to read the member variable of structure is:

scanf(“formal string”,structure_variable.member_variable);

for example:

scanf(“%d”,&student.number);

This statement reads an integer type data and stores in the variable number which is declared inside the structure student.

Similarly the syntax to display the content of member variable of structure is:

                    printf(“formal_string”,structure_variable.member_variable);

For example:

printf(“%d”,student.number);

This statement prints the content of the variable number which is the member variable of structure student.

                    A program to illustrate the use of structure:

#include<stdio.h>

#include<conio.h>

void main()

{

struct student

         {

int roll;

char name[25];

float age;

char add[35];

int class;

};

printf(“please enter the roll number of the student:\t”);

scanf(“%d”,&student.roll);

printf(“\nplease enter the name of the student:\t”);

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

printf|(“\nplease enter age :\t”);

scanf(“%f”,&student.age);

printf(“\naddress:\t”);

scanf(“%s”,student.add);

printf(“\nwhich class?\t”);

scanf(“%d”,&student.class);

printf(“\n\n\n\nname of the student:\t%s”,student.name);

printf(“\nclass:%d\troll number:%d”,student.class,student.roll);

printf(“\naddress:\t%s”,student.add);

printf(“\nage:\t%f”,student.age);

getch();

}

Sample output of the program:

please enter the roll number of the student: 12

please enter the name of the student: ram

please enter the age: 18

address: champanagar

which class? 14

 

 

name of the student: ram

class:           14     roll.:  12

addresss:    champanagar

age: 18

How to declare the structure variable? (Prev Lesson)
(Next Lesson) How to declare the array of structure?
Back to C-Programming

No Comments

Post a Reply

Teacher
Bhim Gautam
Role : Lecturer
Read More
error: Content is protected !!