Program to get IP Address in C
Written by
An Internet Protocol address (IP address) is a numerical label assigned to each device connected to a computer network that uses the Internet Protocol for communication.
Algorithm
- Create a character array called
hostbuffer
with a size of 256, a pointer to a character calledIPbuffer
, and astruct hostent
pointer calledhost_entry
. - Call the
gethostname()
function and store the result in a variable calledhostname
. Passhostbuffer
and its size as arguments to the function. - Check the value of
hostname
using thehostnamechk()
function and exit the program if it is equal to -1. - Call the
gethostbyname()
function and passhostbuffer
as an argument. Store the result inhost_entry
. - Check the value of
host_entry
using thehostentrychk()
function and exit the program if it isNULL
. - Use the
inet_ntoa()
function to convert the Internet network address stored inhost_entry
to an ASCII string and store the result inIPbuffer
.
Code
// C program to print 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 hostnamechk(int hostname) {
if (hostname == -1) {
perror("gethostname");
exit(1);
}
}
// getting host information of the host name
void hostentrychk(struct hostent * hostentry) {
if (hostentry == NULL) {
perror("gethostbyname");
exit(1);
}
}
// Converting space-delimited IPv4 address
// to dotted-decimal format
void ipbufferchk(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;
hostname = gethostname(hostbuffer, sizeof(hostbuffer));
hostnamechk(hostname);
// To get host information
host_entry = gethostbyname(hostbuffer);
hostentrychk(host_entry);
// Converting Internet network address to ASCII string
IPbuffer = inet_ntoa(*((struct in_addr*)host_entry->h_addr_list[0]));
printf("Host IP: %s", IPbuffer);
return 0;
}
Output
Host IP: 192.168.0.1
Use of terms used
- The <unistd.h> header defines miscellaneous symbolic constants and types, and declares miscellaneous functions.
- <errno.h> is a header file in the standard library of the C programming language. It defines macros for reporting and retrieving error conditions using the symbol errno.
- The <netdb.h> header shall define the hostent structure
- The <netinet/in. h> header also defines the IN6ADDR_ANY_INIT macro. This macro must be constant at compile time and can be used to initialize a variable of type struct in6_addr to the IPv6 wildcard address. This variable is initialized by the system to contain the loopback IPv6 address.
- <arpa/inet.h> – definitions for internet operations
- The inet_ntoa() function converts the specified Internet host address to a string in the Internet standard dot notation.
- perror is used in C and C++ to print an error message to stderr.