C Program to display hostname and IP address Last Updated : 14 Oct, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Method 1: There are many ways to find Hostname and IP address of a local machine. Here is a simple method to find hostname and IP address using C program. We will be using the following functions :- gethostname() : The gethostname function retrieves the standard host name for the local computer. gethostbyname() : The gethostbyname function retrieves host information corresponding to a host name from a host database. inet_ntoa() : The inet_ntoa function converts an (Ipv4) Internet network address into an ASCII string in Internet standard dotted-decimal format. C // C program to display hostname // and IP address #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <netdb.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> // Returns hostname for the local computer void checkHostName(int hostname) { if (hostname == -1) { perror("gethostname"); exit(1); } } // Returns host information corresponding to host name void checkHostEntry(struct hostent * hostentry) { if (hostentry == NULL) { perror("gethostbyname"); exit(1); } } // Converts space-delimited IPv4 addresses // to dotted-decimal format void checkIPbuffer(char *IPbuffer) { if (NULL == IPbuffer) { perror("inet_ntoa"); exit(1); } } // Driver code int main() { char hostbuffer[256]; char *IPbuffer; struct hostent *host_entry; int hostname; // To retrieve hostname hostname = gethostname(hostbuffer, sizeof(hostbuffer)); checkHostName(hostname); // To retrieve host information host_entry = gethostbyname(hostbuffer); checkHostEntry(host_entry); // To convert an Internet network // address into ASCII string IPbuffer = inet_ntoa(*((struct in_addr*) host_entry->h_addr_list[0])); printf("Hostname: %s\n", hostbuffer); printf("Host IP: %s", IPbuffer); return 0; } OutputHostname: 31811ca6b90f Host IP: 172.17.0.4Output varies machine to machineMethod 2: The hostname and IP address of a local machine can be found in a variety of ways. Here is a straightforward C programme to find hostname and IP address. The following operations will be used:- gethostname(): The gethostname method returns the local computer's default host name. gethostbyname(): This function extracts host data from a host database corresponding to a host name. The inet_ntoa function transforms a (Ipv4) Internet network address into an ASCII string using the dotted-decimal Internet standard. C // C++ program to display hostname // and IP address #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/socket.h> #include <arpa/inet.h> #include <netdb.h> // Function use int main() { char hostbuffer[256]; struct hostent *host_entry; int hostname; struct in_addr **addr_list; // retrieve hostname hostname = gethostname(hostbuffer, sizeof(hostbuffer)); if (hostname == -1) { perror("gethostname error"); exit(1); } printf("Hostname: %s\n", hostbuffer); // Retrieve IP addresses host_entry = gethostbyname(hostbuffer); if (host_entry == NULL) { perror("gethostbyname error"); exit(1); } addr_list = (struct in_addr **)host_entry->h_addr_list; for (int i = 0; addr_list[i] != NULL; i++) { printf("IP address %d: %s\n", i+1, inet_ntoa(*addr_list[i])); } return 0; } OutputHostname: bd15a44cb17c IP address 1: 172.17.0.3Time Complexity: O(n)Auxilitary Space: O(1) C++ Program to Get and Display IP Address Comment More infoAdvertise with us Next Article Amazing stuff with system() in C / C++ S Smitha Dinesh Semwal Follow Improve Article Tags : C Language Similar Reads C Program to find IP Address, Subnet Mask & Default Gateway Terminology IP Address : An IP address, short for Internet Protocol address, is an identifying number for a piece of network hardware. Having an IP address allows a device to communicate with other devices over an IP-based network like the internet. Subnet mask: A subnet mask is a 32-bit number used 2 min read Getting System and Process Information Using C Programming and Shell in Linux Whenever you start a new process in Linux it creates a file in /proc/ folder with the same name as that of the process id of the process. In that folder, there is a file named "status" which has all the details of the process. We can get those Process Information Through shell as follows: cat /proc/ 2 min read Amazing stuff with system() in C / C++ system() plays a very special role in executing Operating System Commands. By using this library function we can run all those terminal commands that our Operating System allows us to perform, just by using our C program. Now, we will learn a very simple code to fetch the IP Address - to identify ea 2 min read Creating a PortScanner in C Picture a bay where lots of private boats are docked. The location is called a seaport, literally a port at or on the sea. Everyone wanting to dock there, requesting landing services uses the same port. Seaports work with berth numbers assigned to individual boats. The port name and the berth number 5 min read Print an Integer Value in C Printing an Integer in C is one of the most basic tasks. Input and output of the elements are essential for taking input from the user and then giving the output to the user. Here we are going to take an integer as input from the user with the help of the scanf() function and print that integer with 2 min read Reverse a string in C/C++ using Client Server model This article describes a Client and Server setup where a Client connects, sends a string to server and the server shows the original string and sends reversed string to client using socket connection. Prerequisite : Socket Programming Examples: Input : welcome Output :emoclew Input :geeks for geeks 3 min read Like