#include <stdio.h>
#include <string.h>
struct employee
{
char ename[20];
int sal;
};
struct employee accept(struct employee);
void display(struct employee);
void main()
{
struct employee e,f;
f=accept(e);
display(f);
}
struct employee accept(struct employee e)
{
printf("Enter employee name and his sal :");
gets(e.ename);
gets(e.sal);
}
void display(struct employee e)
{
printf("Employee name :");
puts(e.ename);
printf("Employee salary :");
puts(e.sal);
}
The above code is taking the details from the user and not displaying it as it is supposed to do. Can anyone help me out rectifying it?
You forgot to return what is read from
accept.Also note that:
gets()andputs()are for reading and printing strings, not integers.int sal;in the declaration of the structure should be replaced with something likechar sal[128];.gets()has unavoidable risk of buffer overrun. It is deprecated in C99 and removed from C11. Instead of that, you should usefgets()and manually remove newline character read.