C++ Program for Obtaining and Displaying IP Addresses

This article was written and published to provide the code as well as a brief explanation for printing an IP address for the system. So, without further ado, let's get started.

To find and print the IP address of your computer system using C++, use a function named system() and place the command ipconfig after providing the full path of System32 i.e.,

C:\\Windows\\System32\\ipconfig

inside it, which calls the Windows Command Prompt (cmd) to find, get, and display the IP address as shown here in the following program.

C++ Programming Code to Get an IP Address

The following C++ program gets the IP address and displays it on the screen:

#include<cstdlib>
int main()
{
   system("C:\\Windows\\System32\\ipconfig");
   return 0;
}

When the above C++ program is compiled and executed, it will produce the following result:

C++ program to get IP address

Because I did not use any object or method from the "iostream" header file in the preceding program, it is not necessary to include "iostream" in the program. I thought I'd share this information because most C++ programmers, especially beginners, react negatively when they see a program that lacks a "iostream" header file 😏.

The "cstdlib" header file used in the above example contains the definitions of those functions and does the following tasks:

In C, we use "stdlib.h,"  but in C++, we use "cstdlib," which refers to the standard library of C++. Among other methods, there is a method named "system()" defined in this header file that is used to execute the system command.

When you execute the above C++ example code on your system, you will get all the "IP configuration" information of your system. However, in my case, these are the details that are relevant to this article:

Wireless LAN adapter Wi-Fi:

   Connection-specific DNS Suffix  . :
   IPv6 Address. . . . . . . . . . . : 2402:3a80:1f88:d40e:5630:8d40:fbb5:f3ef
   Temporary IPv6 Address. . . . . . : 2402:3a80:1f88:d40e:c1b:edc5:194b:1cee
   Link-local IPv6 Address . . . . . : fe80::c051:9aed:4111:2268%8
   IPv4 Address. . . . . . . . . . . : 192.168.18.187
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : fe80::fc03:71ff:fe3c:72d4%8
                                       192.168.18.23

The same program in different languages

You might enjoy the following program in another programming language:

C++ Online Test


« Previous Program Next Program »