Question: You are allowed to use any C library function, such as printf(), putchar(), etc. Your program will be a variant of the Unix echo program.

You are allowed to use any C library function, such as printf(), putchar(), etc. Your program will be a variant of the Unix echo program.

Background: Recall that when a program is run on the command line, argv[0] points to the first token of the command line (which is the name of the program itself), argv[1] points to the second token on the command line, and so on. So if you run cal 11 2019, then argv[0] points to (the start of) a string "cal", argv[1] points to a string "11", and argv[2] points to a string "2019". The total number of argument tokens on the command line is the integer argc, so the last token is argv[argc-1].

All the strings pointed to by argv[k] (0

The Unix echo program echoes back all its arguments on the command line starting from argv[1]; it skips the program name itself, argv[0]. A simple C code for the echo program could be:

You are allowed to use any C library function, such as printf(),

Assignment 1: Write a C program called xecho, which is like echo, but which:

  1. Prints the argc value first on a line by itself;
  2. Prints each argument on a line by itself;
  3. Does not skip argv[0];
  4. Expresses each token both as a C string and as a C array (see sample output below).

Your program should be contained in a single C source code file called xecho.c, and can make use of any C library functions using header files such as stdio.h. Compile it on the shell command line using:

putchar(), etc. Your program will be a variant of the Unix echo

#include int main(int argc, char *argv[]) { int k; for(k 1; k 1) { /* Separate multiple args by spaces */ putchar(' '); } printf("%s", argv[k]); if (k == argc - 1) { /* Add terminating newline */ putchar(' '); } return 0; } gcc - xecho.c gcc -o xecho xecho.o ls When run with various command line arguments, your program's output should be as follows: $ ./xecho argc == 1 argv[0]: "./xecho" = {'.', '/', 'x', 'e', 'c', 'h', 'o', '\0'} $ ./xecho abc y argc == 3 argv[0]: ./xecho" == {'.', '/', 'x', '1', 'c', 'h', '0', '\0'} argv[1]: "abc" == {'a', 'b', 'c', '10'} argv[2]: "y" = {'y', '10'} == $ ./xecho "abc y" argc == 2 argv[0]: "./xecho" argv[1]: "abcy" == {'.', '/', 'x', {'a', 'b', 'c', 'c', 'h', 'o', '\0'} , 'y', '\0'} $ ./xecho argc == 2 argv[0]: argv[1]: 10 {'.', '/', 'x', 'e', 'c', 'h', 'o', '\0'} ./ xecho" == {'\0'} ==

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!