More refactoring
This commit is contained in:
parent
c03cc73acf
commit
41a934985a
2
Makefile
2
Makefile
@ -1,5 +1,5 @@
|
|||||||
subnetcalc: subnetcalc.c
|
subnetcalc: subnetcalc.c
|
||||||
gcc -Wall -pedantic subnetcalc.c -lm -o subnetcalc
|
gcc -std=c99 -Wall -pedantic subnetcalc.c -lm -o subnetcalc
|
||||||
|
|
||||||
clean: subnetcalc
|
clean: subnetcalc
|
||||||
rm subnetcalc
|
rm subnetcalc
|
||||||
|
20
subnetcalc.c
20
subnetcalc.c
@ -9,26 +9,26 @@
|
|||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
int net;
|
int net;
|
||||||
double hosts;
|
long int hosts, addr;
|
||||||
double addr;
|
|
||||||
printf("Enter netmask in slash-notation without the slash: ");
|
printf("Enter netmask in slash-notation without the slash: ");
|
||||||
scanf("%2d", &net);
|
scanf("%2d", &net);
|
||||||
|
|
||||||
if (net >= 0 && net <= 32) // Sanity check the user input
|
if (net >= 0 && net <= 32) /* Sanity check the user input */
|
||||||
{
|
{
|
||||||
printf("Netmask bit: %d\n\n", net);
|
printf("Netmask bit: %d\n\n", net);
|
||||||
addr = pow(2, 32-net); // Calculate number of addresses
|
addr = pow(2, 32-net); /* Calculate number of addresses */
|
||||||
printf("%.0lf total addresses\n", addr);
|
printf("%ld total addresses\n", addr);
|
||||||
|
|
||||||
hosts = addr-2;
|
hosts = addr-2;
|
||||||
if (hosts < 0) // Check if number of usable hosts is a negative value
|
/* Check if number of usable hosts is a negative value */
|
||||||
|
if (hosts < 0)
|
||||||
{
|
{
|
||||||
hosts = 0; // Set usable hosts to zero if it was negative
|
hosts = 0; /* Set usable hosts to zero if it was negative */
|
||||||
}
|
}
|
||||||
printf("%.0lf usable addresses for hosts\n", hosts);
|
printf("%ld usable addresses for hosts\n", hosts);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
else // If the user entered anything else than 0-32
|
else /* If the user entered anything else than 0-32 */
|
||||||
{
|
{
|
||||||
printf("Only values between 0-32 are accepted\n");
|
printf("Only values between 0-32 are accepted\n");
|
||||||
return 1;
|
return 1;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user