Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C Program to validate an IP address
In this program we will see how to validate an IP address using C. The IPv4 addresses are represented in dot-decimal notation. There are four decimal numbers (all are ranging from 0 to 255). These four numbers are separated by three dots.
An example of a valid IP is: 192.168.4.1
Syntax
int validate_ip(char *ip);
Validation Steps
To validate the IP address we should follow these steps −
Tokenize the string (IP address) using the dot "." delimiter
If the sub strings are containing any non-numeric character, then return false
If the number in each token is not in range 0 to 255, then return false
If there are exactly three dots and four parts then it is a valid IP address
Example
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int validate_number(char *str) {
while (*str) {
if(!isdigit(*str)){ //if the character is not a number, return false
return 0;
}
str++; //point to next character
}
return 1;
}
int validate_ip(char *ip) { //check whether the IP is valid or not
int num, dots = 0;
char *ptr;
char ip_copy[50];
if (ip == NULL)
return 0;
strcpy(ip_copy, ip); //create a copy to avoid modifying original string
ptr = strtok(ip_copy, "."); //cut the string using dot delimiter
if (ptr == NULL)
return 0;
while (ptr) {
if (!validate_number(ptr)) //check whether the sub string is holding only number or not
return 0;
num = atoi(ptr); //convert substring to number
if (num >= 0 && num <= 255) {
ptr = strtok(NULL, "."); //cut the next part of the string
if (ptr != NULL)
dots++; //increase the dot count
} else
return 0;
}
if (dots != 3) //if the number of dots are not 3, return false
return 0;
return 1;
}
int main() {
char ip1[] = "192.168.4.1";
char ip2[] = "172.16.253.1";
char ip3[] = "192.800.100.1";
char ip4[] = "125.512.100.abc";
printf("Testing IP: %s - ", ip1);
validate_ip(ip1)? printf("Valid<br>"): printf("Not valid<br>");
printf("Testing IP: %s - ", ip2);
validate_ip(ip2)? printf("Valid<br>"): printf("Not valid<br>");
printf("Testing IP: %s - ", ip3);
validate_ip(ip3)? printf("Valid<br>"): printf("Not valid<br>");
printf("Testing IP: %s - ", ip4);
validate_ip(ip4)? printf("Valid<br>"): printf("Not valid<br>");
return 0;
}
Output
Testing IP: 192.168.4.1 - Valid Testing IP: 172.16.253.1 - Valid Testing IP: 192.800.100.1 - Not valid Testing IP: 125.512.100.abc - Not valid
How It Works
- validate_number() function checks if a string contains only digits
- validate_ip() function tokenizes the IP string using strtok() with "." as delimiter
- Each token is validated for numeric content and range (0-255)
- The function ensures exactly 3 dots are present for a valid IPv4 format
Conclusion
This program validates IPv4 addresses by checking the format, numeric content, and valid range for each octet. The validation ensures proper dot notation with exactly four octets ranging from 0 to 255.
