gethostname() — Get the name of the host processor

Standards

Standards / Extensions C or C++ Dependencies

XPG4.2
Single UNIX Specification, Version 3

both  

Format

X/Open:
#define _XOPEN_SOURCE_EXTENDED 1
#include <unistd.h>

int gethostname(char *name, size_t namelen);
Berkeley sockets:
#define _OE_SOCKETS
#include <unistd.h>

int gethostname(char *name, int namelen);

General description

The gethostname() call returns the name of the host processor that the program is running on. Up to namelen characters are copied into the name array. The returned name is NULL-terminated unless there is insufficient room in the name array.
Parameter
Description
name
The character array to be filled with the host name.
namelen
The length of name.

Special behavior for C++: To use this function with C++, you must use the _XOPEN_SOURCE_EXTENDED 1 feature test macro.

Returned value

If successful, gethostname() returns 0.

If unsuccessful, gethostname() returns -1 and sets errno to one of the following values:
Error Code
Description
EFAULT
Using name and namelen would result in an attempt to copy the address into a portion of the caller's address space to which data cannot be written.
EMVSPARM
Incorrect parameters were passed to the service.

Related information