0

#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?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 1
    Turn on, and **mind**, your compiler warnings (your function `accept()` is defined as returning a value but it does not have a `return` statement)! Your indentation could be a little better to give an eagle's eye view of the code structure. – pmg Apr 23 '21 at 13:09
  • My compiler doesn't gave any warnings and no errors – Aditya Bachu Apr 23 '21 at 13:10
  • 2
    If your compiler does not give warning, maybe use another? (maybe it's enough to change command-line switches or configuration or ...) – pmg Apr 23 '21 at 13:11
  • 5
    You should never use the `gets()` function — it is [far too dangerous to be used, ever!](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-dangerous-why-should-it-not-be-used). – Jonathan Leffler Apr 23 '21 at 13:12
  • @AdityaBachu are you sure you're running with `-Wall -Wextra` (or whatever your compiler uses to enable warnings)? – mediocrevegetable1 Apr 23 '21 at 13:15
  • You could sensibly replace `printf("Employee name :"); puts(e.ename);` with `printf("Employee name: %s\n", e.ename);` — and similarly for printing the salary information. – Jonathan Leffler Apr 23 '21 at 13:22
  • regarding: `gets(e.ename);` and `gets(e.sal);` the function: `gets()` has been depreciated for years and completely removed from the language around 2009. Your commiler should have told you about this. Suggest using `fgets()` (which has a different parameter list, so be sure to read the MAN page for `fgets()` – user3629249 Apr 24 '21 at 04:59
  • I’m voting to close this question because it uses the function: `gets()` which has been removed from the C language some 10+ years ago. Ignoring your compiler messages isn't the way to program – user3629249 Apr 24 '21 at 05:03

2 Answers2

1
  1. main should return int. Use int main(void).

  1. sal is an int, gets would be for strings but you shouldn't ever use, it's very dangerous as no destination buffer bounds checks are performed, allowing buffer overflow. Check out this post by SO C queue legend ;)

    Use:

    fgets(e.ename, sizeof e.ename, stdin);
    

    And:

    scanf("%d", &e.sal);
    

    Note that you should always check the return value of these functions to validate inputs.


  1. puts only parameter is a pointer to char, it doesn't take int arguments.

    Use:

    printf("%d", e.sal);
    

  1. Your accept() function should return a struct employee but it doesn't.

    Possible correction:

    struct employee accept(void)
    {
        struct employee e;
        printf("Enter employee name and his sal :");
        if(fgets(e.ename, sizeof e.ename, stdin) != NULL)
        {
            e.ename[strcspn(e.ename, "\n")] = '\0'; // optional, removing \n
        }
        else
        {
            //handle error
        }
        if(scanf("%d", &e.sal) != 1)
        {
            //handle error
        }
        return e;
    }
    

    Usage:

    struct employee e;
    e = accept();
    

    You can remove f as it's not needed.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
anastaciu
  • 23,467
  • 7
  • 28
  • 53
0

You forgot to return what is read from accept.

struct employee accept(struct employee e)
{
printf("Enter employee name and his sal :");
gets(e.ename);
gets(e.sal);
return e; /* add this to return things read */
}

Also note that:

  • gets() and puts() are for reading and printing strings, not integers. int sal; in the declaration of the structure should be replaced with something like char sal[128];.
  • You should add indentation to improve readability.
  • gets() has unavoidable risk of buffer overrun. It is deprecated in C99 and removed from C11. Instead of that, you should use fgets() and manually remove newline character read.
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • Note that `gets()` was deprecated in TC3:2007 (Technical Corrigendum 3) — not in the original C99 standard. – Jonathan Leffler Apr 23 '21 at 13:15
  • Also, using `gets(e.sal)` reads into an (uninitialized) integer — hardly correct behaviour. Passing (a copy of) `e` to the function which is then used as a local variable is odd, too. The argument should be omitted and a regular local variable used and returned. Or a pointer to the structure can be passed, and the return is then unnecessary (if the function is declared `void accept(struct employee *ep)` and corresponding other changes are made). – Jonathan Leffler Apr 23 '21 at 13:17