Q:

C program to read and print the student details using structure and Dynamic Memory Allocation.

2

C program to read and print the student details using structure and Dynamic Memory Allocation.

In this program we will create a structure with student details and print the inputted details. Memory to store and print structure will be allocated at run time by using malloc() and released by free().

All Answers

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

 

/*C program to read and print the student details 
using structure and Dynamic Memory Allocation.*/
 
#include <stdio.h>
#include <stdlib.h>
 
/*structure declaration*/
struct student
{
    char name[30];
    int roll;
    float perc;
};
 
int main()
{
    struct student *pstd;
     
    /*Allocate memory dynamically*/
    pstd=(struct student*)malloc(1*sizeof(struct student));
     
    if(pstd==NULL)
    {
        printf("Insufficient Memory, Exiting... \n");
        return 0;
    }
     
    /*read and print details*/
    printf("Enter name: ");
    gets(pstd->name);
    printf("Enter roll number: ");
    scanf("%d",&pstd->roll);
    printf("Enter percentage: ");
    scanf("%f",&pstd->perc);
     
    printf("\nEntered details are:\n");
    printf("Name: %s, Roll Number: %d, Percentage: %.2f\n",pstd->name,pstd->roll,pstd->perc);
      
    return 0;
}
    Enter name: Mike
    Enter roll number: 1
    Enter percentage: 87.50

    Entered details are:
    Name: Mike, Roll Number: 1, Percentage: 87.50

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

total answers (1)

C program to read a one dimensional array, print s... >>
<< C program to read and print the N student details ...