The IP (Internet Protocol) Address is a unique identifier used by each networked computer to identify other computers that are connected to the same network. The IPv4 has five classes A, B, C, D, and E. Each class has a specified IP address range, which ultimately determines how many devices we can connect to the network. The majority of Internet-connected devices primarily use classes A, B, and C. Class D as well as Class E are intended for unique uses.
In this article, we will learn about the classes of IPv4 and will then provide you with a C program to find the class of an IP address.
Classes of IPv4
An IPv4 address consists of 4 octets with decimal values ranging from 0 to 255. IP addresses are classified into 5 classes based on the value of the first octet:
- Class A: Class A addresses are typically used in networks that require a large number of hosts. The first byte of a Class A address is reserved for the network ID, which allows for 128 networks to be created. The leading bit of this octet is always set to zero. The network ID is completed by the final seven bits within this octet. The hosts’ ID is represented by the remaining three octets’ 24 bits., which can accommodate about 17 million hosts on a network. The values of Class A network numbers range from 1 to 126.
- Class B: Class B addresses have been developed for networks that are medium or large-sized. By utilizing the first two octets of the network ID, Class B supports 16,384 networks. The first octet’s initial two bits are always 1 and 0. The second octet and the last six bits make up the network ID. There can be roughly 65,000 hosts in a network due to the 16 bits within the third and fourth octets that represent host ID. Values for Class B networks range from 128 to 191.
- Class C: Class C addresses have been developed for small-sized local area networks. By utilizing the first three octets of the network ID, Class C supports approximately 2 million networks. The first octet’s initial three bits are always 1, 1, and 0. The first three octet’s remaining 21 bits make up the network ID. There can be roughly 254 hosts in a network due to the 8 bits within the fourth octets that represent host ID. Values for Class C networks range from 192 to 223.
- Class D: Multicasting uses class D IP addresses, which are not allotted to hosts. Multicasting permits a single host for sending a single data stream to thousands of additional hosts throughout the Internet. It is frequently employed for streaming videos and audio, such as in IP-based cable TV networks. Providing real-time stock market data from a single source to multiple brokerage firms is another example. The class D networks range from 224 to 239.
- Class E: Class E IP addresses aren’t given to hosts and aren’t accessible to everyone. These are intended only for research. Class E addresses range from 240 to 255.
How to Find the Class of an IP Address in C?
Here is a C program that determines the class of an IP address.
#include <stdlib.h>
#include <string.h>
// Converting an IP address string to a short integers array
void ip_address_to_array(char *ip_addr_str, short *ip_addr_array) {
// Temporary buffer for octet string
char oct_str[4] = {0};
int oct_ind = 0;
int array_ind = 0;
int str_len = strlen(ip_addr_str);
for (int i = 0; i < str_len; i++) {
// Add character to octet string, if not a dot
if (ip_addr_str[i] != '.') {
oct_str[oct_ind++] = ip_addr_str[i];
}
// parse octet string to integer if a dot or end of string is reached
if (ip_addr_str[i] == '.' || i == str_len - 1) {
oct_str[oct_ind] = '\0'; // Terminate octet string
oct_ind = 0; // Reset index for next octet string
ip_addr_array[array_ind++] = atoi(oct_str); // Convert octet string to integer
}
}
}
int main() {
char ip_addr_str[20] = {0};
short ip_addr_array[4];
printf("Enter IP Address: ");
scanf("%s", ip_addr_str);
ip_address_to_array(ip_addr_str, &ip_addr_array[0]);
// Determine the Class of an IP address
if (ip_addr_array[0] >= 0 && ip_addr_array[0] <= 127) {
printf("It is a Class A IP Address.\n");
} else if (ip_addr_array[0] > 127 && ip_addr_array[0] < 191) {
printf("It is a Class B IP Address.\n");
} else if (ip_addr_array[0] > 191 && ip_addr_array[0] < 224) {
printf("It is a Class C IP Address.\n");
} else if (ip_addr_array[0] > 224 && ip_addr_array[0] <= 239) {
printf("It is a Class D IP Address.\n");
} else if (ip_addr_array[0] > 239) {
printf("It is a Class E IP Address.\n");
}
return 0;
}
Conclusion
An IP address is a unique identifier that each networked computer uses to identify other computers connected to the same network. An IPv4 address space has five classes A, B, C, D, and E. To limit the number of devices the network can have, each class has a set of IP address ranges. This tutorial demonstrated the classes of IPv4 and a C program that determined the class of an IP address.