Jump to content United States-English
HP.com Home Products and Services Support and Drivers Solutions How to Buy
» Contact HP
More options
HP.com home
HP-UX Reference > G


HP-UX 11i Version 3: February 2007

Technical documentation

» Feedback
Content starts here

 » Table of Contents

 » Index


gethostname() — get name of current host system


#include <unistd.h>

int gethostname(char *hostname, size_t size);


gethostname() returns in the array to which hostname points, the standard host name for the current system as set by sethostname() (see sethostname(2)). size specifies the length of the hostname array. hostname is null-terminated unless insufficient space is provided.


gethostname() returns 0 if successful. Otherwise, it returns -1 and sets errno to indicate the error.


gethostname() can fail if the following is true:


hostname points to an illegal address. The reliable detection of this error is implementation dependent.


size is less than one.


It is recommended that programs use sysconf(_SC_HOST_NAME_MAX) to dynamically size the buffer used to accept the host name from gethostname(). This is preferred over use of the MAXHOSTNAMELEN constant.

For cases where the MAXHOSTNAMELEN constant must be used, there are two values: 64 and 256. The smaller value is compatible with current releases and is used by default. To use the larger value consult the related documentation (see nodehostnamesize(5)).

The administrator may, with appropriate configuration options enabled, set a host name larger than 64 bytes. Existing applications which were compiled using the smaller MAXHOSTNAMELEN to size a buffer passed to the gethostname() system function obtain only the first 64 bytes. This truncation may cause the applications to exhibit anomalous behavior or to fail.


gethostname() was developed by the University of California, Berkeley.

Printable version
Privacy statement Using this site means you accept its terms Feedback to webmaster
© 1983-2007 Hewlett-Packard Development Company, L.P.