0% found this document useful (0 votes)
2 views3 pages

Practical - 4

The document explains the structure and classification of IPv4 addresses, which consist of 4 octets with values ranging from 0 to 255. It details the five classes of IP addresses based on the first octet's value and provides a C program to determine the class of a given IP address. The program extracts the octets from the input string and categorizes the IP address accordingly.

Uploaded by

heledushant47
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Practical - 4

The document explains the structure and classification of IPv4 addresses, which consist of 4 octets with values ranging from 0 to 255. It details the five classes of IP addresses based on the first octet's value and provides a C program to determine the class of a given IP address. The program extracts the octets from the input string and categorizes the IP address accordingly.

Uploaded by

heledushant47
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

An IPv4 has 4 octets having decimal value between 0 to 255 and depending on

the first octet’s value, IP Addresses divided into 5 classes.


The IP addresses are universally unique. The address space of IPv4 is 232 or
4,294,967,296.
IP address is written as a Binary (hexadecimal) or a Dotted Decimal (w/out
leading zeros)

Class A 1 to 126
Class B 128 to 191
Class C 192 to 223
Class D 224 to 239

program to find class of an IP Address in C


#include <stdio.h>
#include <string.h>
/*
Function : extractIpAddress
Arguments :
1) sourceString - String pointer that contains ip address
2) ipAddress - Target variable short type array pointer that will store ip address
octets
*/
void extractIpAddress(unsigned char *sourceString,short *ipAddress)
{
unsigned short len=0;
unsigned char oct[4]={0},cnt=0,cnt1=0,i,buf[5];

len=strlen(sourceString);
for(i=0;i<len;i++)
{
if(sourceString[i]!='.'){
buf[cnt++] =sourceString[i];
}
if(sourceString[i]=='.' || i==len-1){
buf[cnt]='\0';
cnt=0;
oct[cnt1++]=atoi(buf);
}
}
ipAddress[0]=oct[0];
ipAddress[1]=oct[1];
ipAddress[2]=oct[2];
ipAddress[3]=oct[3];
}

int main()
{
unsigned char ip[20]={0};
short ipAddress[4];
printf("Enter IP Address (xxx.xxx.xxx.xxx format): ");
scanf("%s",ip);

extractIpAddress(ip, &ipAddress[0]);

printf("\nIp Address: %03d. %03d. %03d. %03d\


n",ipAddress[0],ipAddress[1],ipAddress[2],ipAddress[3]);

if(ipAddress[0]>=0 && ipAddress[0]<=127)


printf("Class A Ip Address.\n");
if(ipAddress[0]>127 && ipAddress[0]<191)
printf("Class B Ip Address.\n");
if(ipAddress[0]>191 && ipAddress[0]<224)
printf("Class C Ip Address.\n");
if(ipAddress[0]>224 && ipAddress[0]<=239)
printf("Class D Ip Address.\n");
if(ipAddress[0]>239)
printf("Class E Ip Address.\n");
return 0;
}

You might also like