You are here:   HomeC Programming (Pratical)C Program Find the Greatest Between Four Number
by Dinesh Thakur Category: C Programming (Pratical)

 A structure includes many variables into one. Works as a Report card that has name, marks and total. The record would be a structure. The structure then is used to group a set of similar data not forming a new data type.

To create a structure we use the struct command. Its general form is:

struct student
{
    char name[10];
    int m[3];
    int total;
    char result[5];
}p,s;

The student is the name for the structure. The p and s are optional and would be variable names that you'd be declaring and that would be of student type. A first example:

# include <string.h>

# include <stdio.h>
struct student
{
        char name[10];
        int m[3];
        int total;
        char result[5];
}*p,*s;
        void main()
    {
              int i,j,l,n;
              clrscr();
              printf("Enter the no. of students : ");
              scanf("%d",&n);
              p=(struct student*)malloc(n*sizeof(struct student));
              s=p;
              for(i=0;i<n;i++)
                  {
                             printf("Enter a name : ");
                             scanf("%s",&p->name);
                             p-> total=0;l=0;
                             for(j=0;j<3;j++)
                                {
                                     one:printf("Enter Marks of %d Subject : ",j+1);
                                     scanf("%d",&p->m[j]);
                                     if((p->m[j])>100)
                                        {
                                            printf("Wrong Value Entered");
                                            goto one;
                                        }
                                            p->total+=p->m[j];
                                            if(p->m[j]<40)
                                                l=1;
                                  }
                                            if(l==0)
                                              strcpy(p->result,"PASS");
                                            else
                                              strcpy(p->result,"FAIL");
                                              p++;
                      }
                                              for(i=0;i<n;i++)
                                                  {
                                                        printf("\n%s\t%s",s->name,s->result);
                                                        s++;
                                                  }
                                                        getch();
       } 

Structure of N Students using Dynamic Memory Allocation



What's New and Popular





Search Content







Advance Courses



Basic Courses



Advertise with Us