473,725 Members | 2,248 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4656
"kris" <ra************ **@gmail.comsch rieb im Newsbeitrag
news:11******** **************@ o11g2000prd.goo glegroups.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.progr ammer ... (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.comwro te in message news:11******** **************@ o11g2000prd.goo glegroups.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.netsc hrieb im Newsbeitrag
news:46******** ******@sbcgloba l.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
3129
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 several serial ports. It only crashes when data is being received on one or more of the serial ports. Could someone please give me some ideas about what would cause a program to terminate in this way? Thanks....
6
1358
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 to radians, and FNY is a modulo, but those are not what get me confused, it's just this section here. Anyone in here with "old school BASIC" experience who can unravel this for fun? :) 1740 DEF FN Y(X) = ATN(SQR(1 - X * X) / X):Y = 0:MO = 360...
5
1582
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. If so, would a Mac user have to open a unix console on OSX, or would the program run automatically on any OSX Mac with an autostart on an inserted CDR? Does Mac have an atuostart feature?
3
1339
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 necessary class declarations.
9
3101
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 accounting software. Another has general utilities. I tried referencing the utilities dll, and it shows up in the object explorer. I instantiated an instance of the class and now it shows up all okay in the Intellisense. Whenever I try to run a simple...
24
6578
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 the stack pointer should go upwards when there are "push" operations, and stack pointer should go downards when there are "pop" operations?? If this is the case, the address should go upwards (increasing) or downards (decreasing) then? i.e....
15
7176
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 that will be used in the c program to display the IP address of my machine. Regards,
4
1301
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 program to determine if there is a function by the name I am using. I have tried this with both REALIA and Microfocus COBOL programs and get the same error. Can anyone enlightened me on what the problem is? Below is my code. Dave public...
8
2133
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 of errors in the latter, because I have memory size limitations. So, I wonder what are the best practices to know for a given program, once it is compiled and before its execution:
0
8752
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9179
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9116
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6702
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6011
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3228
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2637
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.