How to Read, Store and Print Structures in C
Notes:
How to Read, Store and Print Structures in C Programming Language:
Example Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
struct Student
{
int rollnum;
char *name;
float marks;
};
struct Student student1 = {0,"",0.0};
printf("student1 rollnum = %d\n",student1.rollnum); // 0
printf("student1 name = %s\n",student1.name); //
printf("student1 marks = %.1f\n",student1.marks); // 0.0
printf("\n");
printf("Enter student1 rollnum :\n");
scanf("%d",&student1.rollnum); // 10
printf("Enter student1 name :\n");
free(student1.name);
student1.name = (char *) malloc(20 * sizeof(char));
scanf("%s",student1.name); // Manjunath
printf("Enter student1 marks :\n");
scanf("%f",&student1.marks); // 76.5
printf("\n");
printf("student1 rollnum = %d\n",student1.rollnum); //10
printf("student1 name = %s\n",student1.name); // Manjunath
printf("student1 marks = %.1f\n",student1.marks); // 76.5
return 0;
}