Home > Net >  Can someone explain me why is this C program compiling without errors?
Can someone explain me why is this C program compiling without errors?

Time:10-14

Hey i am a student learning C programming and just wanted to know that why this program compiles as on line struct date *newdate, foo(); foo is declared as a function local to main function with the return type struct date. As foo is already declared as a function it should give an error of conflicting types as c does not support function overloading. Can somebody please help me.

#include <stdio.h>
#include <stdlib.h>

struct date {
    int month;
    int day;
    int year;
};

struct date foo(struct date x) {
      x.day;

    return x;
};

int main() {
    struct date today = {10, 11, 2014};
    int array[5] = {1, 2, 3, 4, 5};
    struct date *newdate, foo();
    char *string = "test string";
    int i = 3;

    newdate = (struct date *)malloc(sizeof(struct date));
    newdate->month = 11;
    newdate->day = 15;
    newdate->year = 2014;
    today = foo(today);

    free(newdate);

    return 0;
} 

CodePudding user response:

foo is declared as a function local to main function

Not true. It declares but does not define that a function foo exists, and that it takes an unknown number of arguments and returns a struct date.

This declaration is compatible with the actual definition of foo, as the return types match and the declaration makes no statement about the arguments.

  •  Tags:  
  • c
  • Related