(a) Write a C program that will print out all command line arguments, in reverse
ID: 3852323 • Letter: #
Question
(a) Write a C program that will print out all command line arguments, in reverse order, one per line. Prefix each line with its index. (b) Consider this C code snippet: int a= 100: int b= 42: int* p= &a;: int* q= &b;: *p= *q+10: p = q: printf ("%d %d ", a, *p): When this code is executed, what numbers will it print? (c) Consider this C program int main(int argc, char** argv) { char* target = "Apple": char word [100]: printf ("Enter a word: "}: scanf ("%s", &line;): if(word==target){ printf("same "): } else { printf("different "): } return 0: } This program does not work as expected. Explain why, and suggest how it can be fixed (d) During your pracwork you have used the C string-duplicate function, whose signature is: char* strdup(char* source) Write an implementation of strdup (e) The questions in this part are about the C pre-processor. i. What is the output of this program? ii. Conditional compilation controls which parts of a source file will be compiled. In the following program code, is the file program.h included twice in program.c? Explain why, or why not. dup2(old_fds[0], 0) close(old_fds[0]) have to be done in this order or can they be swapped? Explain your answer. (d) Consider the final "if statement" in the listing. Why is there no need to close the file descriptors if there is only a single command? (e) Consider the fork. At that point both the parent and the child have the same code that implements this pseudo-code. What stops the child process executing the code to the point where it - itself - forks another instance of this process with the same code (and, of course, so on ellipsis)? (f) The above could be construed as saying "the child never forks". This isn't true - why?Explanation / Answer
Qestion 1.
A.
#include <stdio.h>
int main(int argc, char **argv)
{
for (int ctr=argc; ctr; ctr--)
cout << argv[ctr-1] << " "; // i just had to add -1 to ctr
return 0;
}
B.
The output is
52 42
This is because
q is storing the address of b hence when its printed its only shows tha value of b
p has the value of b plus 10 added with it
C.
The program doesnot work.First off all the variable line is not declared.So we have to use word .And secondly as it is charecter array we have to check with the each charecter from target and word varuable.
D.
#include<stdio.h>
#include<string.h>
void strdup(char*, char*);
int main()
{
char inp[100], tar[100];
printf("Enter source string ");
gets(inp);
strdup(tar, inp);
printf("Target string is "%s" ", tar);
return 0;
}
void strdup(char *target, char *source)
{
while(*source)
{
*target = *source;
source++;
target++;
}
*target = '';
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.