Q:

Write a C Program to display student details using structure members

0

Write a C Program to display student details using structure members. Here’s a Simple Program to display student details using structure members in C Programming Language.

This program is used to store and access “name, roll no. and marks ”  for many students using array of structures members.

C Structure : :

C Structure is a collection of different data types which are grouped together and each element in a C structure is called member.

  • If you want to access structure members in C, structure variable should be declared.
  • Many structure variables can be declared for same structure and memory will be allocated for each separately.

 

  • It is a best practice to initialize a structure to null while declaring, if we don’t assign any values to structure members.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Syntax of structure

struct structure_name

{

                    data_type member1;

                    data_type member2;

                    .

                    .

                   data_type memeber;

};

 

Accessing Structure Members

 

To access any member of a structure, we use the member access operator (.). The member access operator is coded as a period between the structure variable name and the structure member that we wish to access. You would use the keyword struct to define variables of structure type.

 

Below is the source code for C Program to pass array elements to a function which is successfully compiled and run on Windows System to produce desired output as shown below :

SOURCE CODE : :

/* Program to display the values of structure members*/
#include<stdio.h>
#include<string.h>
struct student {
                     char name[20];
                     int rollno;
                         float marks;
            };
int main( )
{
        struct student stu1 = {"John", 25, 68};
        struct student stu2, stu3;
        strcpy(stu2.name, "Smith");
        stu2.rollno = 26;
        stu2.marks  =  98;
        printf("Enter name, rollno and marks for stu3 : ");
        scanf("%s %d %f", stu3.name, &stu3.rollno, &stu3.marks);
        printf("stu1 : %s  %d  %.2f\n", stu1.name, stu1.rollno, stu1.marks);
        printf("stu2 : %s  %d  %.2f\n", stu2.name, stu2.rollno, stu2.marks);
        printf("stu3 : %s  %d  %.2f\n", stu3.name, stu3.rollno, stu3.marks);

    return 0;
    
}

 

OUTPUT : 

//OUTPUT ::


Enter name, rollno and marks for stu3 : Bolt
27
78
stu1 : John  25  68.00
stu2 : Smith  26  98.00
stu3 : Bolt  27  78.00

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C Basic Solved Programs – C Programming

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write a C Program to understand how structure memb... >>
<< Write a C Program to perform Call By Value and Cal...