compiling a program which uses the function gethostid
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > compiling a program which uses the function gethostid




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    compiling a program which uses the function gethostid  
kris


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-22-07 12:23 PM

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);



}


output : (I obtain the correct hostid)

I hope I would get a reply soon.


Thanks,
Krish






[ Post a follow-up to this message ]



    Re: compiling a program which uses the function gethostid  
Joachim Schmitz


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-22-07 12:23 PM

"kris" <raghavakrishna.j@gmail.com> schrieb im Newsbeitrag
news:1182504234.052356.153550@x35g2000prf.googlegroups.com...
> 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);
>
>
>
> }
>
>
> output : (I obtain the correct hostid)
>
> I hope I would get a reply soon.
Use "-Wall -ansi -pedantic -O2" to get the max warning level.
Using printf (or any varadic finction) wothout a prototy is causeing
Undefined Behavoir. In case of gethistid() an implicit int is assuned, on
linux int == long, so you're pretty safe here.

Bye, Jojo







[ Post a follow-up to this message ]



    Re: compiling a program which uses the function gethostid  
Joachim Schmitz


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-22-07 12:23 PM

"Joachim Schmitz" <nospam.jojo@schmitz-digital.de> schrieb im Newsbeitrag
news:f5g4ql$19i$1@online.de...
> "kris" <raghavakrishna.j@gmail.com> schrieb im Newsbeitrag
> news:1182504234.052356.153550@x35g2000prf.googlegroups.com... 
> Use "-Wall -ansi -pedantic -O2" to get the max warning level.
> Using printf (or any varadic finction) wothout a prototy is causeing
> Undefined Behavoir. In case of gethistid() an implicit int is assuned, on
> linux int == long, so you're pretty safe here.
Sorry for the typos, serious finger trouble... I ment to write:
Use "-Wall -ansi -pedantic -O2" to get the max warning level.
Using printf (or any varadic function) without a prototyp is causing
Undefined Behavoir. In case of gethostid() an implicit int is assumed, on
linux int == long, so you're pretty safe here.

Bye, Jojo







[ Post a follow-up to this message ]



    Re: compiling a program which uses the function gethostid  
Spoon


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
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 ]



    Re: compiling a program which uses the function gethostid  
Spoon


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-22-07 12:23 PM

Joachim Schmitz wrote:

> Using printf (or any variadic function) without a prototype is causing
> Undefined Behavior. In case of gethostid() an implicit int is assumed, on
> linux int == long, so you're pretty safe here.

This is /not/ true.

cf. the x86-64 ABI:
http://www.x86-64.org/documentation/abi.pdf

sizeof(int)  = 4
sizeof(long) = 8

These days, one should never assume sizeof(int) == sizeof(long)





[ Post a follow-up to this message ]



    Re: compiling a program which uses the function gethostid  
Joachim Schmitz


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-22-07 12:23 PM

"Spoon" <root@localhost> schrieb im Newsbeitrag
news:467ba190$0$23885$426a34cc@news.free.fr...
> Joachim Schmitz wrote:
> 
>
> This is /not/ true.
>
> cf. the x86-64 ABI:
darn, forgot about the 64-bit Intels...

Bye, Jojo







[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 07:34 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register