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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | #include <stdio.h> #include <arpa/inet.h> #include <errno.h> #include <stdlib.h> #include <netdb.h> #include <string.h> #include <sys/socket.h> int find_ip_address(char *, char *); int main() { char hostname[50], ip_address[50]; printf("\nEnter a Hostname\t"); scanf("%s", hostname); find_ip_address(hostname , ip_address); printf("\nIP Address of Hostname %s:\t%s\n", hostname, ip_address); return 0; } int find_ip_address(char *hostname, char *ip_address) { struct hostent *host_name; struct in_addr **ipaddress; int count; if((host_name = gethostbyname(hostname)) == NULL) { herror("\nIP Address Not Found\n"); return 1; } else { ipaddress = (struct in_addr **) host_name->h_addr_list; for(count = 0; ipaddress[count] != NULL; count++) { strcpy(ip_address, inet_ntoa(*ipaddress[count])); return 0; } } return 1; } |
Must Read: C Program To Shutdown Linux Operating System
C Program To Get IP Address in Windows Operating System
1 2 3 4 5 6 7 | #include<stdlib.h> int main() { system("C:\\Windows\\System32\\ipconfig"); return 0; } |
Output

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