By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,696 Members | 1,422 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,696 IT Pros & Developers. It's quick & easy.

compliling a program which uses gethostid function call

P: n/a


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

Jun 22 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"kris" <ra**************@gmail.comschrieb im Newsbeitrag
news:11**********************@o11g2000prd.googlegr oups.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.
Same answer as in comp.unix.programmer ... (modulo my typos...):
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
Jun 22 '07 #2

P: n/a
"kris" <ra**************@gmail.comwrote in message news:11**********************@o11g2000prd.googlegr oups.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.
No. What happens is that by default, C "guesses" that any function you
use that doesn't have a prototype is a function that takes a variable number
of arguments and returns an int.

If this happens to match what the function really does, you get away with
it. It is probably a good idea to get your compiler to generate a warning
whenever a function is used that doesn't have a prototype, so that you don't
rely on C "guessing", and potentially guessing incorrectly, or perhaps it
is wrong but you can get away with it on your current machine, but not
when you take it to another machine.

BFN. Paul.
Jun 22 '07 #3

P: n/a
Paul Edwards said:

<snip>
What happens is that by default, C "guesses" that any function
you use that doesn't have a prototype is a function that takes a
variable number of arguments and returns an int.
Not true in C90.

"If the expression that precedes the parenthesized argument list in a
function call consists solely of an identifier, and if no declaration
is visible for this identifier, the identifier is implicitly declared
exactly as if, in the innermost block containing the function call, the
declaration

extern int identifier();

appeared."

This is /not/ a function that takes a variable number of arguments, but
a function that takes an unknown number of arguments.

If you call a function that takes a variable number of arguments,
without a valid prototype in scope, the behaviour is undefined:

"If the function[1] is defined with a type that includes a prototype,
and the types of the arguments after promotion are not compatible with
the types of the parameters, or if the prototype ends with an ellipsis
( ", ..." ), the behavior is undefined."

[1] In context, this refers to a function that has been called but not
prototyped.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jun 22 '07 #4

P: n/a
Joachim Schmitz wrote:
>In case of gethostid() an implicit int is assumed, on
linux int == long, so you're pretty safe here.
Not on many 64-bit linux versions, you may be Windows programmers.
Jun 22 '07 #5

P: n/a
"Tim Prince" <ti***********@sbcglobal.netschrieb im Newsbeitrag
news:46**************@sbcglobal.net...
Joachim Schmitz wrote:
>In case of gethostid() an implicit int is assumed, on
linux int == long, so you're pretty safe here.
Not on many 64-bit linux versions, you may be Windows programmers.
No I'm not, I've just forgotten about 64bit Linux... sorry about that.

Bye, Jojo
Jun 22 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.