gets not a C11 standard, superceded by fgets

This commit is contained in:
Krishna Vedala 2020-04-08 09:45:12 -04:00
parent 1b826807ed
commit 1d8d07efc9
No known key found for this signature in database
GPG Key ID: BA19ACF8FC8792F7

View File

@ -4,81 +4,82 @@
int main() int main()
{ {
char hex[17]; #define MAX_STR_LEN 17
char hex[MAX_STR_LEN];
long long octal, bin, place; long long octal, bin, place;
int i = 0, rem, val; int i = 0, rem, val;
/* Input hexadecimal number from user */ /* Input hexadecimal number from user */
printf("Enter any hexadecimal number: "); printf("Enter any hexadecimal number: ");
gets(hex); fgets(hex, MAX_STR_LEN, stdin);
octal = 0ll; octal = 0ll;
bin = 0ll; bin = 0ll;
place = 0ll; place = 0ll;
/* Hexadecimal to binary conversion */ /* Hexadecimal to binary conversion */
for(i=0; hex[i]!='\0'; i++) for (i = 0; hex[i] != '\0'; i++)
{ {
bin = bin * place; bin = bin * place;
switch(hex[i]) switch (hex[i])
{ {
case '0': case '0':
bin += 0; bin += 0;
break; break;
case '1': case '1':
bin += 1; bin += 1;
break; break;
case '2': case '2':
bin += 10; bin += 10;
break; break;
case '3': case '3':
bin += 11; bin += 11;
break; break;
case '4': case '4':
bin += 100; bin += 100;
break; break;
case '5': case '5':
bin += 101; bin += 101;
break; break;
case '6': case '6':
bin += 110; bin += 110;
break; break;
case '7': case '7':
bin += 111; bin += 111;
break; break;
case '8': case '8':
bin += 1000; bin += 1000;
break; break;
case '9': case '9':
bin += 1001; bin += 1001;
break; break;
case 'a': case 'a':
case 'A': case 'A':
bin += 1010; bin += 1010;
break; break;
case 'b': case 'b':
case 'B': case 'B':
bin += 1011; bin += 1011;
break; break;
case 'c': case 'c':
case 'C': case 'C':
bin += 1100; bin += 1100;
break; break;
case 'd': case 'd':
case 'D': case 'D':
bin += 1101; bin += 1101;
break; break;
case 'e': case 'e':
case 'E': case 'E':
bin += 1110; bin += 1110;
break; break;
case 'f': case 'f':
case 'F': case 'F':
bin += 1111; bin += 1111;
break; break;
default: default:
printf("Invalid hexadecimal input."); printf("Invalid hexadecimal input.");
} }
place = 10000; place = 10000;
@ -87,36 +88,36 @@ int main()
place = 1; place = 1;
/* Binary to octal conversion */ /* Binary to octal conversion */
while(bin > 0) while (bin > 0)
{ {
rem = bin % 1000; rem = bin % 1000;
switch(rem) switch (rem)
{ {
case 0: case 0:
val = 0; val = 0;
break; break;
case 1: case 1:
val = 1; val = 1;
break; break;
case 10: case 10:
val = 2; val = 2;
break; break;
case 11: case 11:
val = 3; val = 3;
break; break;
case 100: case 100:
val = 4; val = 4;
break; break;
case 101: case 101:
val = 5; val = 5;
break; break;
case 110: case 110:
val = 6; val = 6;
break; break;
case 111: case 111:
val = 7; val = 7;
break; break;
} }
octal = (val * place) + octal; octal = (val * place) + octal;