In: Computer Science
explain the code for a beginner in c what each line do Question 3. In the following code, answer these questions: Analyze the code and how it works? How can we know if this code has been overwritten? Justify how?
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
int main(int argc, char **argv)
{
int changed = 0;
char buff[8];
while (changed == 0){
gets(buff);
if (changed !=0){
break;}
else{
printf("Enter again: ");
continue;
}
}
printf("the 'changed' variable is modified\n %d", changed);
}
The explanation and complete solution is given below for the above problem and if you feel any problem then feel free to ask.
1. Explanation of the code line by line:-
In the first line of code "#include <stdlib.h>", the stdlib.h header file is included which is used for using the functions for memory allocation, process control, conversions, and others. But in the given code no function of this library or header file is used.
In second line "#include <unistd.h>", unistd.h header file is included which is used to provide the access to the POSIX operating system API, means this header file lets our code to use the function that can manipulate or work with our operating system.
In next line stdio.h header file is included which is used in almost every C program as it is the header file which support the input/output functions in the code.
In the next line the main() function is defined with command line arguments that are not used in this code. The command line arguments are those arguments that we pass while execution of the program from command line argv is the array of arguments we pass and argc is the number of arguments.
Now, in the first line of main() the variable "changed" is defined of type integer with value 0 and in the second line of main() a character array "buff" of size 8 is declared.
In the next line the while loop is used with condition as changed==0, means the loop will terminate if the value of changed variable is not 0.
Now inside the body of while loop the gets(buff) statement taking the input in "buff" from the user(gets() is mainly used for getting character array as input from console/user).
In the next line if-statement is used with condition changed!=0, which means the body of if-statement is executed when the value of "changed" is not 0 and in the body of if-statement break is written which will break the loop in which it is written and control will come out of the loop, in this case the break statement will cause the while loop to end and if break is encountered then while gets ended no matter whether the condition in while is true.
After the body of if-statement there is else statement in the next line in the body of while in which printf("Enter again") statement is used to display the message on console as "Enter again" after printf() there is a "continue" statement is present which causes the next iteration of code leaving the execution of the rest of body of loop after "continue".
After the body of while in the next line one more time printf() is used for displaying a message to console and the code ends after this line with closing braces as required.
2. Working:-
The working of this code is mainly from while statement the while loop will run until the value of "changed" remains 0 and in the whole program there is no such statement which is changing the value from 0. Therefore, the while loop never terminates itself now the code will take input in "buff" from user and go to the if statement which remains false inside while so the else statement executes and result in an "Enter Again" message and again going to execute while from start due to "continue" and this will remain executing until user do not terminate the program. The code after the while loop never runs as the while loop never ends itself in this case.
3. How to determine if this code is overwritten or not:-
To determine the overwritten behavior you just need to look at the libraries and functionality of the code. In this case two header files "stdlib.h" and "unistd.h" are included which have no use for the existing code and also the main function for this code can be written without the command line arguments as there is no use of command line argument in the code. All of these things depicting that the code is overwritten also the actual code might be written to interact with the operating system.