Learn How To Find IP Address in C Programming. This IP Address C Program works in both Linux and Windows operating systems for both localhost server or any web server. IP stands for Internet Protocol.
This C Program To Find Localhost IP Address takes in an input string. It can be either your localhost or any website address. The IP address of your localhost server will generally be 127.0.0.1.
However, you should be connected to the internet if you need to find out the IP Address of any website. If it’s your localhost, then internet connection is not mandatory.
Must Read: C Program To List Names of all Files in a Directory
inet_ntoa(*ipaddress[count]): The function inet_ntoa() converts a network address (such as localhost or any web server like www.fb.com) to an actual IP Address consisting of numbers using struct in_addr.
herror(): The function herror() helps to display the error code and description if the name lookup terminates abnormally or if it cannot find the IP address.
C Program To Find IP Address in Linux Ubuntu
int find_ip_address(char *, char *);
char hostname, ip_address;
printf("\nEnter a Hostname\t");
find_ip_address(hostname , ip_address);
printf("\nIP Address of Hostname %s:\t%s\n", hostname, ip_address);
int find_ip_address(char *hostname, char *ip_address)
struct hostent *host_name;
struct in_addr **ipaddress;
if((host_name = gethostbyname(hostname)) == NULL)
herror("\nIP Address Not Found\n");
ipaddress = (struct in_addr **) host_name->h_addr_list;
for(count = 0; ipaddress[count] != NULL; count++)
Must Read: C Program To Shutdown Linux Operating System
C Program To Get IP Address in Windows Operating System
If you have any doubts or compilation errors in this code to get Local IP Address in C programming, let us know about it in the comment section below.
Thanks for this program. After so many searches, where I was getting only localhost ip address c program, finally I got the actual ip address code.
for a standard conforming program you have to declare main as “int main(int argc, char **argv)”. please note that f() is a k&r definition and something different than f(void).
i didnt get any output for the code u mentioned for finding ip address on windows.
Please let us know the error.
Please provide me a c program code that return local machine IP address like as 192.168.0.44