Question

In: Computer Science

Please explain answer so I can understand. 1. Write a C program that prints out its...

Please explain answer so I can understand.


1. Write a C program that prints out its command line arguments, one to a line.

Discussion: A C program starts at a function called main. main has two arguments, conventionally named argc and argv. argc is of type int; it’s the argument count, an integer that contains the number of command line arguments (including the program name). argv is the argument vector; it’s an array of pointers to characters, In C this is described as char *argv[] (or, equivalently and confusingly, char **argv - because C doesn’t really have arrays.)

Imagine that the command line is

% myprog pizza burrito kale

Then what’s actually happening is that somewhere in memory, when the program begins execution, the shell (working with the C runtime library) has arranged that the strings “myprog”, “pizza”, “burrito”, “kale” are stored somewhere in memory, AND that a table of four addresses - the addresses of the first characters of those strings - is also stored somewhere in memory. These addresses are pointers. In principle these strings can be anywhere but in practice they occupy consecutive memory locations. The diagram on the next page shows a picture in which I have arbitrarily chosen location 1128 as the address of the ‘m’ in “myprog” and 2020 as the address of the (nameless) table where the pointers to the strings are stored. In this example, the value of argc is 4 and the value of argv is 2020. The value of *argv is 1128. (What is its type?)The value of **argv is ‘m’. (What is its type? What is the value of argv[1]?)

2. Modify your program to print the arguments in reverse order.

Solutions

Expert Solution

The C program along with the comments is given below.

#include <stdio.h>

//arguments to the main function: argc and argv
int main(int argc, char** argv)
{
    //display the contents of argv using for loop
    printf("Arguments in the same order as given:-\n");
    for (int i = 0; i < argc; i++)
        printf("%s\n", argv[i]);
      
    //display the contents of argv in the reverse order using for loop [Modification]
    printf("\nArguments in the reverse order as given:-\n");
    for(int i=argc-1; i>=0; i--)
        printf("%s\n", argv[i]);
      
    return 0;
}

If the indendations are not clear, then please refer the screenshots of the code given below.

Command line arguments given were as follows:-

Output obtained is also given below.

Explanation:-

  • Inorder to pass the command line arguments, the main function must be set to receive the two paramaters viz. argc and argv.
  • The argc is an integer variable that stores the total number of arguments passed. The argv is a vector containing character pointers to the strings.
  • The first argument i.e. argv[0] will be the program name. Rest of them will be followed by the given arguments.
  • Using a for loop, iterate a variable i from 0 to argc-1 and print the content of argv[i]. This will display the contents of the argv array.
  • To print the contents in the reverse order, iterate i from argc-1 to 0 using a for loop and print the argv[i].

Hope this helps. Doubts, if any, can be asked in the comments.


Related Solutions

Write a program in C++ that prints out the even numbers between 1 and 21 using...
Write a program in C++ that prints out the even numbers between 1 and 21 using WHILE loop. Also, find the sum AND product of these numbers and display the resulting sum and product.
Can you please write this in python and comment as well so I can understand what...
Can you please write this in python and comment as well so I can understand what yo are doing. Thank you. 1)Loop through list A, at each iteration, show the square root result of that element. Add proper text to your print function. A = [-4, 1, -16, 36, -49, 64, -128] 2)Create a counter variable, name it i and initialize it to 0. Using a for loop, count how many numbers are divisible by 3 in range of 1...
Write a C program that creates and prints out a linked list of strings. • Define...
Write a C program that creates and prints out a linked list of strings. • Define your link structure so that every node can store a string of up to 255 characters. • Implement the function insert_dictionary_order that receives a word (of type char*) and inserts is into the right position. • Implement the print_list function that prints the list. • In the main function, prompt the user to enter strings (strings are separated by white-spaces, such as space character,...
Write a C++ program that prints out all of the command line arguments passed to the...
Write a C++ program that prints out all of the command line arguments passed to the program. Each command line argument should be separated from the others with a comma and a space. If a command line argument ends in a comma, then another comma should NOT be added
C program, please Write a program that reads a sequence of 10 integer inputs and prints...
C program, please Write a program that reads a sequence of 10 integer inputs and prints the smallest and largest of the inputs and the number of even and odd inputs. for a beginner please, you could use a while loop,if-else,
1) Determine whether these statements are true or false. Please explain why so I can understand...
1) Determine whether these statements are true or false. Please explain why so I can understand where the answers came from a) ∅ ∈ {∅} b) ∅ ∈ {∅, {∅}} c) {∅} ∈ {∅} d) {∅} ∈ {{∅}} e) {∅} ⊂ {∅, {∅}} f ) {{∅}} ⊂ {∅, {∅}} g) {{∅}} ⊂ {{∅}, {∅}} 2) Let A = {a, b, c}, B = {x, y}, and C = {0, 1}. Find a) A x B x C d) B x...
Can you explain and answer part e and part f please? I already understand parts c...
Can you explain and answer part e and part f please? I already understand parts c and d Firm 1 and Firm 2 are functioning in a market as competitors. The inverse market demand for chicken is given by P (Y ) = 100 − 2Y , and the total cost function for any firm in the industry if given by TC(y) = 4y. (c) Suppose that two Cournot firms operated in the market and the reaction firm for Firm...
Please explain to me the answers so that I can understand the concept. Thank you! You...
Please explain to me the answers so that I can understand the concept. Thank you! You must evaluate a proposal to buy a new milling machine. The purchase price of the milling machine, including shipping and installation costs, is $126,000, and the equipment will be fully depreciated at the time of purchase. The machine would be sold after 3 years for $85,000. The machine would require a $3,500 increase in net operating working capital (increased inventory less increased accounts payable)....
(PLEASE ANSWER ALL PARTS OF THE QUESTION AND PROVIDE VARIOUS EXAMPLES SO I CAN UNDERSTAND, THANK...
(PLEASE ANSWER ALL PARTS OF THE QUESTION AND PROVIDE VARIOUS EXAMPLES SO I CAN UNDERSTAND, THANK YOU SO MUCH FOR YOUR HELP!!!) A critical difference between the Absorption Approach and the Elasticities Approach to the effect of a devaluation on the Current Account is the addition of income effects. How will income effects alter the effect of a devaluation, and does this tend to make it more or less difficult for a devaluation to strengthen the current account under the...
(Please show work so I can understand how you got to the answer - Thank you...
(Please show work so I can understand how you got to the answer - Thank you very much ) Via Gelato is a popular neighborhood gelato shop. The company has provided the following data concerning its operations: Fixed Element per Month Variable Element per Liter Actual Total for June Revenue $ 13.00 $ 72,540 Raw materials $ 4.75 $ 30,330 Wages $ 5,700 $ 1.50 $ 14,560 Utilities $ 1,730 $ 0.30 $ 3,800 Rent $ 2,700 $ 2,700 Insurance...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT