I recently learned the network layer of the computer network, so I want to write a program to calculate the network number when learning the IP address. You don't have to worry about it manually...
Because the content is very simple, there are not many comments.
Code:
1 # include <stdio. h> 2 # define max 4 3 4 int main () 5 {6 int hostip [Max] = {0}, subnet [Max] = {0}; 7 while (1) 8 {9 printf ("Please input your host IP:"); 10 scanf ("% d. % d. % d. % d ", & hostip [0], & hostip [1], & hostip [2], & hostip [3]); 11 printf (" Please input your host IP: "); 12 scanf (" % d. % d. % d. % d ", & subnet [0], & subnet [1], & subnet [2], & subnet [3]); 13 14 if (! Hostip [0]) 15 {16 break; 17} 18 19 int net [Max] = {0}; 20 int I; 21 for (I = 0; I <Max; ++ I) 22 {23 net [I] = hostip [I] & subnet [I]; // & operation to get netid24} 25 printf ("net ID: % d. % d. % d. % d \ n ", net [0], net [1], net [2], net [3]); 26} 27 return 0; 28}