title: Linux C: Get default interface's IP address
body: <p>My question is about the following code (in <a href="https://stackoverflow.com/questions/212528/get-the-ip-address-of-the-machine">this link</a>):</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;ifaddrs.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;string.h&gt;
  #include &lt;arpa/inet.h&gt;
  
  int main (int argc, const char * argv[]) {
      struct ifaddrs * ifAddrStruct = NULL;
      struct ifaddrs * ifa = NULL;
      void * tmpAddrPtr = NULL;
  
      getifaddrs(&amp;ifAddrStruct);
  
      for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa-&gt;ifa_next) {
          if (ifa -&gt;ifa_addr-&gt;sa_family==AF_INET) { // Check it is
              // a valid IPv4 address
              tmpAddrPtr = &amp;((struct sockaddr_in *)ifa-&gt;ifa_addr)-&gt;sin_addr;
              char addressBuffer[INET_ADDRSTRLEN];
              inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
              printf("%s IP Address %s\n", ifa-&gt;ifa_name, addressBuffer);
          }
          else if (ifa-&gt;ifa_addr-&gt;sa_family==AF_INET6) { // Check it is
              // a valid IPv6 address
              tmpAddrPtr = &amp;((struct sockaddr_in6 *)ifa-&gt;ifa_addr)-&gt;sin6_addr;
              char addressBuffer[INET6_ADDRSTRLEN];
              inet_ntop(AF_INET6, tmpAddrPtr, addressBuffer, INET6_ADDRSTRLEN);
              printf("%s IP Address %s\n", ifa-&gt;ifa_name, addressBuffer);
          }
      }
      if (ifAddrStruct != NULL)
          freeifaddrs(ifAddrStruct);
      return 0;
  }
  </code></pre>
  
  <p>This code's output is like this:</p>
  
  <pre><code>lo IP Address 127.0.0.1
  wlan0 IP Address 172.28.1.89 (I want to only this)
  lo IP Address ::1
  wlan0 IP Address fe80::6e71:d9ff:fe1d:b0
  </code></pre>
  
  <p>How can I get the default interface's IP address? (You can give an answer with another code piece.)</p>