0

I'm trying to figure out why the functions inperson and outperson do not work without prototypes while the function get does. Outperson and inperson works without prototypes if I put them before the main-function but not after. The get-function works both before and after without a prototype. Why?

#include <stdio.h>

struct person {
   char name[100];
   int  age;
};

#define NPERSON 1

int main()
{
 //struct person inperson(void);
//void outperson(struct person p);
    struct person p[NPERSON];
    int i, oldest;
    for(i = 0; i < NPERSON; i++) {
          p[i] = inperson();
    }
    oldest = 0;
    for(i = 0; i < NPERSON; i++) {
          if(p[i].age > p[oldest].age) {
          oldest = i;
    }
}
outperson(p[oldest]);
get();

return 0;
}

int get()
{
    printf("works");
}

struct person inperson(void)
{
     struct person p;
     scanf("%s %d", p.name, &p.age);
     return p;
}

void outperson(struct person p)
{
     printf("Person %s, %d years, is oldest\n", p.name, p.age);
}
dandan78
  • 12,242
  • 12
  • 61
  • 73
TheEagle
  • 107
  • 2
  • 8
  • The specifics in this case are an interesting lesson in C-history. The short of it is that the default return type for function in C (unless there's a prototype that says otherwise) is int. So if your function return an int, most compilers will still handle it without complaining. You notice this is the case with the functions that work in your code. – D'Nabre Nov 05 '14 at 08:19
  • Changed `int get()` to `void get()` and it didn't work. Thanks. – TheEagle Nov 05 '14 at 08:31

0 Answers0