Given a CIDR notation, print the range of the possible IP addresses.


This snippet converts a CIDR notated IP address to a range of IP addresses in decimal, dotted notation.
Eg.: => -

About the Author
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(void) {
	unsigned int ip[4],ip_min[4],ip_max[4],netmask,tmp1,tmp2;
	char ip_string[19];
	for (;gets(ip_string) == NULL ;perror("Please enter an IP and a subnetwork mask!\nEg.:"));
	/*Set the bytes which won't be changed*/
	for (tmp1=tmp2=netmask/8;tmp1>0;tmp1--){
		ip_min[tmp1-1] = ip[tmp1-1];
		ip_max[tmp1-1] = ip[tmp1-1];
	/*Set the bytes which should be 0ed or 255ed.*/
	for(tmp1 = tmp2,++tmp2;tmp2< 4;tmp2++){
	/* Finally set the one which has to be shifted.*/
	if( tmp1 < 4){
		tmp2 = 8-netmask%8;
	printf("Valid addresses range from %u.%u.%u.%u to %u.%u.%u.%u\n",ip_min[0],ip_min[1],ip_min[2],ip_min[3],ip_max[0],ip_max[1],ip_max[2],ip_max[3]);
	return 0;
Dale_7 0 Newbie Poster

Hello, I have use of your code in a project. I want to know what license you prefer for this code. I'd like to put it in a library on GitHub.

Would you prefer to do this? I'll help.

JamesCherrill 4,334 Most Valuable Poster Moderator Featured Poster

Dani owns the copyright to everything on DaniWeb, but I'm sure she will be happy to permit you what you want for this piece of code. Use DaniWeb messaging to contact her directly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.