06-22-07 12:23 PM
kris wrote:
> Hi I have written a program which prints the hostid on a linux
> system.
> The programm uses gethostid() function call which is defined in the
> library file unistd.h. But my programm gets compiled without any
> warnings even if I didnot include any of the header files.
>
>
> can I know how does this happen i.e how does the compiler identifies
> this function gethostid.
> Is there any default path from where the compiler picks up the
> definition from.
>
>
> My application is as follows..
>
>
> main()
> {
>
>
> long id,hostid;
>
>
> printf("%ld\n",gethostid());
> id = gethostid();
> printf("%lu\n",id);
>
>
>
> }
Here's what GCC 3.4.4 thinks of your program:
$ cat foo.c
main()
{
long id,hostid;
printf("%ld\n",gethostid());
id = gethostid();
printf("%lu\n",id);
}
$ gcc -std=c89 -Wall -Wextra -pedantic foo.c
foo.c:2: warning: return type defaults to `int'
foo.c: In function `main':
foo.c:8: warning: implicit declaration of function `printf'
foo.c:8: warning: implicit declaration of function `gethostid'
foo.c:8: warning: long int format, int arg (arg 2)
foo.c:5: warning: unused variable `hostid'
foo.c:14: warning: control reaches end of non-void function
In fact, -std=c89 will "hide" the declaration for gethostid.
$ cat foo.c
#include <stdio.h>
#include <unistd.h>
int main(void)
{
long id;
id = gethostid();
printf("%ld\n", id);
return 0;
}
$ gcc -std=gnu89 -Wall -Wextra -pedantic foo.c
/* NO WARNING */
[ Post a follow-up to this message ]
|