C/explain code pls!!1
Expert: Joydeep Bhattacharya - 9/25/2007
QuestionThis programme allows the user to add, subtract, multiply, or divide, but does not use a menu. Instead, it uses a command-based interface. First, what is the difference between a command based interface and a menu interface?
CODE:
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
int main(void)
{
char command[80], temp[80];
int i, j;
for(; ;) { /* line 1*/
printf("Operation? ");
gets(command); /* line 2*/
if(!strcmp(command, "quit")) break; /*line 3*/
printf(" enter first number : ");
gets(temp);
i = atoi(temp);
printf(" enter second number :");
gets(temp);
j = atoi(temp);
if(!strcmp(command, "add"))
printf("%d
" , i + j);
else if(!strcmp(command , "subtract"))
printf(%d
" , i - j);
else if(!strcmp(command, " divide")) {
if(j) printf("%d
" , i/j); /*line 4*/
}
else if(!strcmp(command, "multiply"))
printf("%d
" , i*j);
else printf("unknown command.
");
}
return 0;
}
QUESTIONS
Carefully look at the given line codes. My questions are from each line codes e.g lines 1,2,3 and 4.
Line 1 : why the use of an infinite for loop?
Line 2 : I use a linux compiler and each time i use the c liberary function gets(), the compiler return an error message saying : the use of gets() is dangerous!!. why does it say this? Is it that the gets() function is not in use anymore?
Line 3: If you ask me to interprete this code: if(!strcmp(command, "anything"), i would say it means that so long the comparison are not the same, that is anything is not the same as comand, then execute a comand but in this programme, i don't think that is exactly what that line of code means. Could you explain explicitly, what this particular code means because there are alot of it in this programme and it makes it difficult for me to understand the programme?
Line 4: What is the function of the statement : if (j) in this programme?
I hope you understood my questions and am looking forward to a detailed explanation to help me understand every line of codes that i have found mysterious in this programe?
Thanks alot for your assistance and time.
AnswerHi Henry
Line 1: Its infinite loop coz the programmer wanna do this operation infinites times as long as the user types quit for the command.
Line 2: Better use scanf as the substitute of gets. Its generally avoided because of buffer overrun.
Line 3: strcmp returns 0 when two string matches .... right that means when if(strcmp(command,"anthing")) that means this gets substituted by if(0) which means the condition is not satisfied which means if(false) so if we put a not in front of this false i.e. if(!false) means if(true) that is why it has been done like that or you can rather substitute it to if(strcmp(command,"anthing")==0) which is same as if(!strcmp(command,"anthing"))
last Doubt: The if(j) => printf(" enter second number :"); gets(temp); j = atoi(temp); have a look at this code where j is an int. the user takes the input as a string and then converting the same to int with atoi ... now suppose someone has entered some text in this case so it would fail to convert to so atoi will return 0 or null which is tested here.
In case of any doubt please feel free to get back to me.
regards
Joydeep Bhattacharya
http://www.scodz.com