Question

In: Computer Science

explain the code for a beginner in c what each line do Question 3. In the...

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);

}

Solutions

Expert Solution

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.


Related Solutions

. What does each line of the following code do? fsrPin = ‘A4’; ledPin = ‘D4’;...
. What does each line of the following code do? fsrPin = ‘A4’; ledPin = ‘D4’; anArduinoObj = arduino(); forceVoltage = readVoltage(anArduinoObj, fsrPin); if forceVoltage > 4.0 writeDigitalPin(anArduinoObj, ledPin, 1); end Based on the Adafruit pages related to their round FSR sensor, what is the required force to read a voltage greater than 4V?
Can you please explain in detail what each line of code stands for in the Main...
Can you please explain in detail what each line of code stands for in the Main method import java.util.Scanner; public class CashRegister { private static Scanner scanner = new Scanner(System.in); private static int dollarBills[] = {1, 2, 5, 10, 20, 50, 100}; private static int cents[] = {25, 10, 5, 1}; public static void main(String[] args) { double totalAmount = 0; int count = 0; for (int i = 0; i < dollarBills.length; i++) { count = getBillCount("How many $"...
4. Explain what is happening on each line of the following AVR assembly code. If you...
4. Explain what is happening on each line of the following AVR assembly code. If you were to execute this code what would be the final decimal values in R20, R21 and SREG registers? BCLR 0 BCLR 1 BCLR 2 BCLR 3 BCLR 4 BCLR 5 BCLR 6 BCLR 7 LDI ​R19, 0x02 LDI​R20, 0x74 LDI​R21, 0x04 LDI​R22, 0x22 ADD​R20, R22 SUB​R22, R21 ADD​R20, R21 MOV​R20, R21 JMP​DONE ADD​R21, R20 SUB​R21, R22 DONE:​SUB​R20, R21 -embedded system-
QUESTION 6 A single line comment in C++ language source code can begin with _____   ...
QUESTION 6 A single line comment in C++ language source code can begin with _____        a) // b) ; c) : d) /* QUESTION 7 What is the output of the following program? #include<iostream> using namespace std; class abc { public: int i; abc(int i) { i = i; } }; main() { abc m(5); cout<<m.i; } a)Garbage b)5        c)Compile Error d)None of the answers QUESTION 8 The following operator can be used to calculate the...
analyze the assembly code and explain what each line is doing 000000000000063a <main>: 63a: 55 push...
analyze the assembly code and explain what each line is doing 000000000000063a <main>: 63a: 55 push ebp 63b: 48 89 e5 mov ebp,esp 63e: 48 83 ec 10 sub esp,0x10 642: c7 45 fc 00 00 00 00 mov DWORD PTR [ebp-0x4],0x0 649: eb 16 jmp 661 <main+0x27> 64b: 83 7d fc 09 cmp DWORD PTR [ebp-0x4],0x9 64f: 75 0c jne 65d <main+0x23> 651: 48 8d 3d 9c 00 00 00 lea edi,[eip+0x9c] # 6f4 <_IO_stdin_used+0x4> 658: e8 b3 fe...
C# programming. Comment/Explain the below code line by line. I am having a hard time following...
C# programming. Comment/Explain the below code line by line. I am having a hard time following it. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Nth_prime {     class Program     {         public static bool isPrime(int number)         {             int counter = 0;             for (int j = 2; j < number; j++)             {                 if (number % j == 0)                 {                     counter = 1;                     break;                 }             }             if (counter == 0)             {                 return true;             }             else             {                 return false;             }         }...
Comment the code explaining what each line is doing. The first line has been done for...
Comment the code explaining what each line is doing. The first line has been done for you. x=[0 0 -1 2 3 -2 0 1 0 0]; %input discrete-time signal x dtx= -2:7; y=[1 -1 2 4]; dty=8:11; z=conv(x,y); dtz=6:18; subplot(1,3,1), stem(dtx,x) subplot(1,3,2), stem(dty,y) subplot(1,3,3), stem(dtz,z)
JAVA CODE BEGINNER , Please use comments to explain, please Repeat the calorie-counting program described in...
JAVA CODE BEGINNER , Please use comments to explain, please Repeat the calorie-counting program described in Programming Project 8 from Chapter 2. This time ask the user to input the string “M” if the user is a man and “W” if the user is a woman. Use only the male formula to calculate calories if “M” is entered and use only the female formula to calculate calories if “W” is entered. Output the number of chocolate bars to consume as...
Python 3 can someone explain in very simple terms what EVERY line of code does, including...
Python 3 can someone explain in very simple terms what EVERY line of code does, including what j and i do def longest(string): start=0;end=1;i=0; while i<len(string): j=i+1 while j<len(string) and string[j]>string[j-1]: j+=1 if end-start<j-i: end=j start=i i=j; avg=0 for i in string[start:end]: avg+=int(i) print('The longest string in ascending order is',string[start:end]) print('The average is',avg/(end-start)) s=input('Enter a string ') longest(s)
Python 3 can you explain what every single line of code does in simple terms please...
Python 3 can you explain what every single line of code does in simple terms please so basically # every single line with an explanation thanks def longest(string): start = 0;end = 1;i = 0; while i<len(string): j = i+1 while j<len(string) and string[j]>string[j-1]: j+=1 if end-start<j-i: end = j start = i i = j; avg = 0 for i in string[start:end]: avg+=int(i) print('The longest string in ascending order is',string[start:end]) print('The average is',avg/(end-start)) s = input('Enter a string ')...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT