C++: Hostname

Bjarne-stroustrup
 

Find the name of the host on which the routine is running.

#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#include <unistd.h>

int main(void)
{
	char name[_POSIX_HOST_NAME_MAX + 1];
	return gethostname(name, sizeof name) == -1 || printf("%s\n", name) < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}

SOURCE

Content is available under GNU Free Documentation License 1.2.