-3
#include<stdio.h>
struct stud
{
  char name[20];
  int mark;
  int per;
  char grad[5];
};
void main(){
int i,n;
printf("Enter number of students");
scanf("%d",&n);
struct stud s[n];
for(i=1;i<=n;i++)
{
    printf("Enter name of student");
    scanf("%d",s[i].name);
    printf("Enter obtained marks");
    scanf("%d",&s[i].mark);
    s[i].per=s[i].mark/5;
}
for(i=1;i<=n;i++)
{
    if(s[i].per>=80)
     strcpy(s[i].grad,"A");
    else if(s[i].per>=60)
      strcpy(s[i].grad,"B");
    else if(s[i].per>=50)
      strcpy(s[i].grad,"C");
    else if(s[i].per>=40)
     strcpy(s[i].grad,"D");
    else
     strcpy(s[i].grad,"F");
}
for(i=1;i<=n;i++)
{
    printf("&s",s[i].name);
    printf("&d",s[i].mark);
    printf("&d",s[i].per);
    printf("&s",s[i].grad);
}

}

When executing this code it's showing error :

main.c: In function ‘main’:
main.c:25:10: warning: implicit declaration of function ‘strcpy’ [-Wimplicit-function-declaration]
          strcpy(s[i].grad,"A");
          ^~~~~~
main.c:25:10: warning: incompatible implicit declaration of built-in function ‘strcpy’
main.c:25:10: note: include ‘<string.h>’ or provide a declaration of ‘strcpy’
main.c:27:11: warning: incompatible implicit declaration of built-in function ‘strcpy’
           strcpy(s[i].grad,"B");
           ^~~~~~
main.c:27:11: note: include ‘<string.h>’ or provide a declaration of ‘strcpy’
main.c:29:11: warning: incompatible implicit declaration of built-in function ‘strcpy’
           strcpy(s[i].grad,"C");
           ^~~~~~
main.c:29:11: note: include ‘<string.h>’ or provide a declaration of ‘strcpy’
main.c:31:10: warning: incompatible implicit declaration of built-in function ‘strcpy’
          strcpy(s[i].grad,"D");
          ^~~~~~
main.c:31:10: note: include ‘<string.h>’ or provide a declaration of ‘strcpy’
main.c:33:10: warning: incompatible implicit declaration of built-in function ‘strcpy’
          strcpy(s[i].grad,"F");
          ^~~~~~
main.c:33:10: note: include ‘<string.h>’ or provide a declaration of ‘strcpy’

What's the error here ?

kuro
  • 3,214
  • 3
  • 15
  • 31
Vivank Sharma
  • 395
  • 1
  • 4
  • 11

1 Answers1

1

scanf("%d",s[i].name); is undefined behavior. It will be "%s" format specifier. And printf("%s",s[i].name) not &s.

Include string.h header file.

Also array index out of bound while getting input. Also undefined behavior. Array indices start from 0. All loop would be for(i=0;i<=n-1;i++)

and include header like this:

#include <string.h> 

for(i=0;i<=n-1;i++)
{
    printf("Enter name of student");
    scanf("%s",s[i].name);
    printf("Enter obtained marks");
    scanf("%d",&s[i].mark);
    s[i].per=s[i].mark/5;
}

Another change

for(i=1;i<=n;i++)
{
    printf("%s",s[i].name);
    printf("%d",s[i].mark);
    printf("%d",s[i].per);
    printf("%s",s[i].grad);
}
user438383
  • 5,716
  • 8
  • 28
  • 43
user2736738
  • 30,591
  • 5
  • 42
  • 56