1

I am getting errors during the compilation of this C program and is related to the declaration of the function. What is the problem here? When I declare void display(student); it shows a warning but if I change to void display(struct student st) it shows some errors.

#include<stdio.h>
void display(student);
void read_student(student);
struct student{
    int roll;
    char name[20],depart[20],sex,result;

    float percent,marks[5],total; 

};

void main(){
    int n;
    printf("enter the no of data you want to enter ??");
    scanf("%d",&n);

     struct student s[n];
     for(int i=0;i<n;i++)
        read_student(&s[i]);
        printf("\n---------------------------------------------------Result Sheet --------------------------------------------------------------------");
        printf("\nRoll No.\tName\t\tSex\tDepartment\t\tMarks\t\t\t\tTotal\tPercentage\tResult ");
    printf("\n----------------------------------------------------------------------------------------------------------------------------------------");
    printf("\n                              \t\t\t\tA\tB\tC\tD\tE\n");
    printf("----------------------------------------------------------------------------------------------------------------------------------------");

     for(int i=0;i<n;i++)  
        display(s[i]);


}
 void display(struct student st){


    printf("\n%2d\t%10s\t\t%c\t%10s\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%4.2f\t  %2.2f\t\t%c\n",st.roll,st.name,st.sex,st.depart,st.marks[0],st.marks[1],st.marks[2],st.marks[3],st.marks[4],st.total,st.percent,st.result);


}
 void read_student(struct student *std){
     int c=0,i;
    printf("Enter the roll no:");
    scanf("%d",&std->roll);
     printf("enter the name:\n");
     scanf("%s",std->name);
     printf("enter Sex:\n");
     scanf(" %c",&std->sex);
     printf("Enter the department:\n");
     scanf("%s",std->depart);
     printf("enter the marks in 5 subjects:\n");
     for(i=0;i<5;i++){
      scanf("%f",&std->marks[i]);
      std->total=std->total+std->marks[i];
      if(std->marks[i]>=40)
      c++;
     }
      if(c==5)
      std->result='p';
      else
        std->result='f';



     std->percent=(std->total/500)*100;


}
Sudarshan
  • 96
  • 6
  • 2
    The compiler reads your code from top to bottom. So at the point where you declare the functions, it has no idea what `student` means, because you haven't told it yet. – user3386109 Feb 22 '20 at 09:41
  • 1
    BTW, the best way to solve this problem is to put `main` at the end of the file. Then you don't need the function declarations at all. – user3386109 Feb 22 '20 at 09:43
  • 2
    `void main()` is not a valid signature for `main()` in Standard C: `void main()` --> `int main(void)`. Curious: why pass an entire copy of the `student` structure to the `display_student()` function when you could just pass a pointer to `struct student`? – ad absurdum Feb 22 '20 at 10:13

1 Answers1

3

The compiler needs to know what struct student is, when using it elsewhere in the code, for example declaring a function with it as type of an argument/parameter. The compiler reads the source file from top to bottom. You need to declare the structure before you use it as a type in the declaration of a function:

struct student;   // forward declaration of structure `student`.

void display(struct student);       // ok, because `struct student` is declared before.
void read_student(struct student*); // ok, because `struct student` is declared before.

struct student{                     // definition of structure `student`.
    int roll;
    char name[20],depart[20],sex,result;
    float percent,marks[5],total; 
};

Alternatively, you can define the structure before the declarations of the functions:

struct student{    // define the structure `student` before the function declarations.
    int roll;
    char name[20],depart[20],sex,result;
    float percent,marks[5],total; 
};

void display(struct student);
void read_student(struct student*);

Another way would be to put the definitions of the functions read_student and display before main(), but after the the definition of the structure student. In this way you can omit the separate declarations of the functions read_student and display and of course the forward declaration of the structure student as well:

#include<stdio.h>

struct student{
    int roll;
    char name[20],depart[20],sex,result;
    float percent,marks[5],total; 
};

void read_student(struct student *std){
     int c=0,i;
     printf("Enter the roll no:");
     scanf("%d",&std->roll);
     printf("enter the name:\n");
     scanf("%s",std->name);
     printf("enter Sex:\n");
     scanf(" %c",&std->sex);
     printf("Enter the department:\n");
     scanf("%s",std->depart);
     printf("enter the marks in 5 subjects:\n");
     for(i=0;i<5;i++){
      scanf("%f",&std->marks[i]);
      std->total=std->total+std->marks[i];
      if(std->marks[i]>=40)
      c++;
     }
      if(c==5)
      std->result='p';
      else
        std->result='f';

     std->percent=(std->total/500)*100;
}

void display(struct student st){
    printf("\n%2d\t%10s\t\t%c\t%10s\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%4.2f\t  %2.2f\t\t%c\n",st.roll,st.name,st.sex,st.depart,st.marks[0],st.marks[1],st.marks[2],st.marks[3],st.marks[4],st.total,st.percent,st.result);
}

int main(){
    int n;
    printf("enter the no of data you want to enter ??");
    scanf("%d",&n);

    struct student s[n];

    for(int i=0;i<n;i++)
        read_student(&s[i]);

    printf("\n---------------------------------------------------Result Sheet --------------------------------------------------------------------");
    printf("\nRoll No.\tName\t\tSex\tDepartment\t\tMarks\t\t\t\tTotal\tPercentage\tResult ");
    printf("\n----------------------------------------------------------------------------------------------------------------------------------------");
    printf("\n                              \t\t\t\tA\tB\tC\tD\tE\n");
    printf("----------------------------------------------------------------------------------------------------------------------------------------");

    for(int i=0;i<n;i++)  
        display(s[i]);
}

At the declaration of functions you can omit identifiers of parameters, but not the type of parameters.

It is also not permissible to use the structure student without preceding the struct keyword as you tried it with:

void display(student);

since you use the structure student by its definition:

struct student{
...
};

If you would use a typedef like for example:

typedef struct student{
...
} student;

You could omit the struct keyword, but if you use a typedef or just use the structure as it is, is a kind of controversy topic amongst the community with regards to readibility. All references to that, you can see on the answers to these questions:

typedef struct vs struct definitions

Why should we typedef a struct so often in C?

I personally recommend to keep on using the struct variant, even if it requires a little bit more typing with the keys, but makes your code a little bit clearer.