compliling a program which uses gethostid function call

K

kris

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
 
J

Joachim Schmitz

kris said:
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
 
P

Paul Edwards

kris said:
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.
 
R

Richard Heathfield

Paul Edwards said:

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.
 
T

Tim Prince

Joachim said:
>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.
 
J

Joachim Schmitz

Tim Prince said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top