473,395 Members | 1,656 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

compliling a program which uses gethostid function call



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
5 4627
"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
"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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Ken | last post by:
When running a program in the debugger, what would cause it to crash without any error messages? I get "The program has exited with code 0 (0x0)". The program is a MDI app with threading for...
6
by: Patrick Sullivan | last post by:
I want to use this algorithm but can't figure it out, I never used BASIC. I tried translating it but got lost in the gosubs and "for i - 1 to ... nexts". I have figured out that FNU(X) is degree...
5
by: inkexit | last post by:
What all does it take to compile a program written with visual studio 2003 as a Mac .exe and a linux .exe. Since OS X runs a form of unix now, do I only have to worry about creating a unix .exe. ...
3
by: Dave | last post by:
I've seen at least one article on this: How to access classes, etc. managed-to-unmanaged. I can't find it. Any tricks or pointers? I guess the class name won't get munged but I'm not clear about...
9
by: axs221 | last post by:
I am trying to move some of our large VBA Access front-end file into ActiveX DLL files. I created two DLL files so far, one was a module that contains code to integrate into the QuickBooks...
24
by: John | last post by:
I know this is a very fundamental question. I am still quite confused if the program call stack stack should always grows upwards from the bottom, or the opposite, or doesn't matter?? That means...
15
by: sunny | last post by:
Hai, Anybody can give me idea how to get the ip address of my machine through a cprogram with out using the socket programming. Is there any system call to get the ip address of my machine...
4
by: =?Utf-8?B?UGFycm90?= | last post by:
I am attempting to import a DLL function from a COBOL program using the DLLImport function but I always get an error that says the Specified module (dtonsub.dll) cannot be found. I used the link...
8
by: Horacius ReX | last post by:
Hi, I am developing some code in C which first I compile on my linux machine and afterwards I test on another special hardware which has almost no debug capabilities at all. Usually I get a lot...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.