Question 3. In the following code, answer these questions:
#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);
}
a. Analysis of the code:
b. We can say the code has been overwritten because of below reasons:
Please look at the comments of the program for more clarity.
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
int main(int argc, char **argv) // Here basically this program is accepting parameters as arguments
{
int changed = 0; // Initializing the variable
char buff[8]; // Initializing a char array of size 8
while (changed == 0) // Staring of while loop
{
/*
This loop will continue running if changed == 0.
if the value of "changed" is not changing then this while loop will run infinite time.
*/
gets(buff); // Taking input in buff
if (changed !=0)
{
/*
The program will enter this "if" statement when the value of "changed" is not equal to 0
This is never going to happen as when "changed" is something other than 0, it will not even enter the while loop.
If it will not enter the while loop, how can it enter the if statement.
*/
break;
}
else{
printf("Enter again: "); // It will enter when changed == 0, so it will always execute.
continue;
}
}
printf("the 'changed' variable is modified\n %d", changed); // If "while loop" ends then the program will come at this point and this print statement will execute.
}
Please let me know in the comments in case of any confusion. Also, please upvote if you like.
Get Answers For Free
Most questions answered within 1 hours.