C Program to find IP Address, Subnet Mask & Default Gateway

C programming language can be used to find the details of the Internet connection of the system. Now, let's learn about the basic terms that we need in this problem.

IP Address − IP Address stands for Internet Protocol address. An IP address is a fixed numerical identification number that is associated with each device. IP address allows communication of your device using IP address over the internet.

Subnet Mask − A 32-bit component of the IP address. The subnet mask differentiates the network component of IP address into two parts. One being network address and other being host address. Subnet is the number that is designed to divide networks into smaller sub-networks and complete the IP address of the system connected to the network.

Default Gateway − It is an access point or IP router of a computer that is connected to the network. This default gateway is the gateway that is defined by the computer by default until any other gateway is used by an application. This gateway is the connecting route of the network of the systems to the rest of the internet. Failure of this network may disconnect the subnetwork from the internet.

Syntax

#include <stdlib.h>

// Using system() function
int system(const char *command);

// Using execl() function  
int execl(const char *path, const char *arg0, ...);

In C programming language there are two methods that can be used to check the network configuration of the system −

  • System command
  • Execl command

Method 1: Using system() Function

C programming language provides system() function in stdlib library that can be used to access the IP configuration of the system using ipconfig. In the call of the function we will pass the full address of the ipconfig file that needs to be executed −

Note: This program is platform-specific and works on Windows systems. On Linux/Unix, replace "ipconfig" with "ifconfig" command.

#include <stdio.h>
#include <stdlib.h>

int main() {
    printf("Network Configuration Details:<br>");
    printf("==============================<br>");
    
    // Execute ipconfig command on Windows
    system("c:\windows\system32\ipconfig");
    
    return 0;
}

Method 2: Using execl() Function

Another way to fetch the IP details of the system is by using the execl() function. This function replaces the current process with the specified program and needs the full path to the executable −

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main() {
    printf("Executing network configuration command...<br>");
    
    // Execute ipconfig using execl
    execl("c:\windows\system32\ipconfig", "ipconfig", NULL);
    
    // This line will not execute if execl succeeds
    printf("Error: Failed to execute command<br>");
    return 0;
}

Key Points

  • Both methods are platform-specific and require appropriate system commands.
  • The system() function creates a new process and returns control to the program.
  • The execl() function replaces the current process entirely.
  • Network details are system-specific and may require administrator privileges to view complete information.

Conclusion

These C programs demonstrate how to retrieve network configuration details using system calls. The actual output depends on your system's network configuration and may require appropriate permissions to execute successfully.

Updated on: 2026-03-15T12:05:44+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements