C Program to Get IP Address

This post was written and published to provide code in the C programming language that prints a system's IP address. This post includes not only the code but also a description of the code. So, without further ado, let's get started.

In order to find or get the IP address in C programming, use the system() function and place the command inside it. Get the IP address and display it on the screen using the following C program:

#include <conio.h>
#include <stdlib.h>

int main()
   return 0;

The following snapshot shows the sample output produced by the above program on my system. This program was built and run in the Code::Blocks IDE.

c program to get ip address

If we focus on the main area of the output produced by the above program, which is:

IPv6 Address. . . . . . . . . . . : 2402:3a80:4097:1e1b:5319:8a6d:679f:8b94
Temporary IPv6 Address. . . . . . : 2402:3a80:4097:1e1b:5c0b:c613:7f31:27ac
Link-local IPv6 Address . . . . . : fe80::c051:9aed:4111:2268%8
IPv4 Address. . . . . . . . . . . :
Subnet Mask . . . . . . . . . . . :
Default Gateway . . . . . . . . . : fe80::fc03:71ff:fe3c:72d4%8

Then we can see the "IPv6" and "IPv4" addresses along with other information.

"system()" is a built-in function of the C language that is used when we need to invoke or call the operating system command through the program. This library function is defined in the "stdlib.h" header file.

If we're talking about a Windows-based system, I simply used the program to call the "ipconfig" command through the command prompt (cmd) indirectly. However, because "system()" is cross-platform, you can use it to call the operating system command regardless of the operating system you're using.

Since the "system()" method is used to call the operating system command, if I open the command prompt like:

c print IP address of system

and type the command "ipconfig" and hit the ENTER key, then I will get the same output as the output produced by the above program. For example:

c print IP address

Before I end this discussion about finding and printing your system's IP address using a C program, I'd like to point out that if you open "Google" and type "What's my IP," you'll get the following result:

ip address using google c

Now if you match this IP to the output produced by the program, you will see that this IP will match with the "temporary IPv6" address of your system, not with the one that can be found above the "temporary IPv6."

The same program in different languages

C Online Test

« Previous Program Next Program »

Liked this post? Share it!