/* Get the local machine IP address */
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>

int main()
{

   int rc;
   struct hostent *pHostEnt;
   char name[90];
   int      i = 0;
   struct   in_addr sin_addr;

   if(!gethostname(name, sizeof(name)))
         printf("hostname = %s\n", name);
   else
         printf("gethostname() failed\n");

   pHostEnt = gethostbyname( name );

/* if there are multiple IP for the same hostname,
   loop to print all of them, otherwise, you can printout 
   the first one  with i=0 only */
   while ( pHostEnt->h_addr_list[i] )
    {
       /* sin_addr.s_addr = *(long*)(pHostEnt->h_addr_list[i]); */
       memcpy(&(sin_addr), pHostEnt->h_addr_list[i], pHostEnt->h_length);
       printf("IP address is: %s\n", inet_ntoa(sin_addr));
       i++;
    }
}


