返回当前主机的名字,原型为:
#include <unistd.h>
int gethostname(char *name, size_t namelen);
成功返回0,失败返回-1
代码如下 :
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#define BUFLEN 128
int main(int argc, char **argv)
{
char buf[BUFLEN];
if (gethostname(buf, sizeof(buf)) < 0) {
printf("gethostname error:%s\n", strerror(errno));
return -1;
}
printf("hostname=%s\n", buf);
return 0;
}
输出为: