Question

#include <stdio.h> int main(int argc, char *argv[]) { int i; for (i = argc - 1;...

#include <stdio.h>

int main(int argc, char *argv[]) {

int i;

for (i = argc - 1; i > 0; i--)

printf("%s ", argv[i]);

printf("\n");

return 0;

}

can you explain this code in c and why use this function  

1 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the answer for your question in C Programming Language

Kindly upvote if you find the answer helpful.

Explanation :

  • int main(int argc, char *argv[]) - 'main' - the function though which the compiler starts compilation and execution of a C code. Without main method C program doesn't work at all 'int' before main indicates the return type of the main() method to the compiler.It returns an integer after the method is executed.   
  • int argc - 'argc' is a variable, it contains number of arguments passed through the main method (i.e., arguments passed while compiling the code via command line). As is an number, its respective data type is 'int'.              
  • char* argv[] - It is an array of strings, each string is an argument passed to the main method (i.e., it is a collection of arguments passed via command line). The first string of this array would be the path of the current C file.
  • int i - Declaration of a varable named 'i' which is of type integer
  • for (i = argc - 1; i > 0; i--) - for loop that loops through (total number of arguments - 2) times (i.e (argc - 1) removes last element, and i > 0 doesn't reach 0th element, hence two elements from the array of arguments cannot be traced). It prints arguments from last to first as it is starting from last argument
  • printf("%s ", argv[i]); -Prints the value of argument string at ith position in tha array argv[], %s specifies that the argument passed to this printf method is a string.
  • printf("\n"); - After printing all the strings prints next line

NOTE : I have used dev-c++ to execute this code, in this compiler we can pass arguments via parameters option. By default the first parameter will be the path of the file, and the parameters we pass will be displayed via this program.

CODE :

#include <stdio.h>

/*
   int main(int argc, char *argv[]) - 'main' - the function though which the compiler starts
                                   compilation and execution of a C code.
                                   Without main method C program doesn't work at all
                                   'int' before main indicates the return type of the
                                   main() method to the compiler.
                                   It returns an integer after the method is executed                                  
   int argc - 'argc' is a variable, it contains number of arguments passed through the main method
               (i.e., arguments passed while compiling the code via command line)
               As is an number, its respective data type is 'int'
              
   char* argv[] - It is an array of strings, each string is an argument passed to the main method
                   (i.e., it is a collection of arguments passed via command line)
                   The first string of this array would be the path of the current C file
*/
int main(int argc, char *argv[]) {
   //int i - Declaration of a varable named 'i' which is of type integer
   int i;  
  
   /*for loop that loops through (total number of arguments - 2) times (i.e
       (argc - 1) removes last element, and i > 0 doesn't reach 0th element,
       hence two elements from the array of arguments cannot be traced)

It prints arguments from last to first as is starting from last argument
   */
   for (i = argc - 1; i > 0; i--)  
      
       //Prints the value of argument string at ith position in tha array argv[]
       // %s specifies that the argument passed to this printf method is a string
       printf("%s ", argv[i]);  
      
       //After printing all the strings prints next line
       printf("\n");  
      
   //Returns 0
   return 0;
}

SCREENSHOTS :

Please see the screenshots of the code below for the indentations of the code.

int arg 1 #include <stdio.h> 2 /*int main(int args, char *argv[]) - main - the function though which the compiler starts 3Passing parameters in dev-c++

C:\Users\.. vesktop\Sample.c-Dev-C++ 5.11 File Edit Search View Project Execute Tools Style Window Help Compile F9 TDM-GCC 4.

Parameters we pass here will be displayed by the program in reverse order of strings(Each word seperated by space is an argument)

Parameters x Parameters to pass to your program: Welcome To C Programming Host Application: OK X Cancel

OUTPUT :

Programming C To Welcome Process exited after 0.1324 seconds with return value o Press any key to continue . .

Any doubts regarding this can be explained with pleasure :)

Add a comment
Know the answer?
Add Answer to:
#include <stdio.h> int main(int argc, char *argv[]) { int i; for (i = argc - 1;...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The operating system is Ubuntu 18.04 hello.c #include <stdio.h> int main(int argc, char *argv[]) { printf("Hello...

    The operating system is Ubuntu 18.04 hello.c #include <stdio.h> int main(int argc, char *argv[]) { printf("Hello world!\n"); return 0; } syscall-hello.c #include <unistd.h> #include <sys/syscall.h> char *buf = "Hello world!\n"; int main(int argc, char *argv) { size_t result; /* "man 2 write" to see arguments to write syscall */ result = syscall(SYS_write, 1, buf, 13); return (int) result; }Download and compile hello.ce and syscall-hello.com. Compile them statically and dynamically. How do the library and system calls produced by them compare...

  • #include <stdio.h> int main(int argc, char *argv[]) {      char a, *pc, c[9];      int i, *pk, k[9];      a='...

    #include <stdio.h> int main(int argc, char *argv[]) {      char a, *pc, c[9];      int i, *pk, k[9];      a='z';      pc=&(c[8]);      pk=&(k[0]);      for (i=0; i<9; i++)     {           *pc=a-(char)i;           pc--;           *pk=(int)a-i;           pk++;           }                     return 0; }//end of main Answer the below questions with the above code Write out the memory map for the above C code in the following table. For array variables, list the address range for the entire array. Assume the memory address starts from 100, that is, the address for a...

  • #include <stdlib.h> #include <stdio.h> #include "main.h" #define MAX_NUM_LENGTH 11 void usage(int argc, char** argv) { if(argc...

    #include <stdlib.h> #include <stdio.h> #include "main.h" #define MAX_NUM_LENGTH 11 void usage(int argc, char** argv) { if(argc < 4) { fprintf(stderr, "usage: %s <input file 1> <input file 2> <output file>\n", argv[0]); exit(EXIT_FAILURE); } } /* This function takes in the two input file names (stored in argv) and determines the number of integers in each file. If the two files both have N integers, return N, otherwise return -1. If one or both of the files do not exist, it...

  • #include<stdio.h> #include<stdio.h> int main(){ int i; //initialize array char array[10] = {“Smith”, “Owen”, “Kowalczyk”, “Glass”, “Bierling”,...

    #include<stdio.h> #include<stdio.h> int main(){ int i; //initialize array char array[10] = {“Smith”, “Owen”, “Kowalczyk”, “Glass”, “Bierling”, “Hanenburg”, “Rhoderick”, “Pearce”, “Raymond”, “Kamphuis”}; for(int i=0; i<8;i++){ for(int j=0; j<9; j++){ if(strcmp(array[j],array[j+1])>0){ char temp[20]; strcpy(temp,array[j]); strcpy(array[j],array[j+1]); strcpy(array[j+1],temp); } } } printf(“---------File Names---------\n”); for(inti=0; i<9; i++){ printf(“\t%s\n”,array[i]); } printf(-------5 Largest Files according to sorting----\n”); for(int i=0;i>=5;i--) { printf(“\t%s\n”,array[i]); } return0; } Consider the "sort" program (using with void* parameters in the bubblesort function) from the week 10 "sort void" lecture. Modify it as follows...

  • PLease explain output of these two programs: 1. #include <stdio.h> typedef struct { char *name; int...

    PLease explain output of these two programs: 1. #include <stdio.h> typedef struct { char *name; int x, y; int h, w; } box; typedef struct { unsigned int baud : 5; unsigned int div2 : 1; unsigned int use_external_clock : 1; } flags; int main(int argc, char** argv){ printf("The size of box is %d bytes\n", sizeof(box)); printf("The size of flags is %d bytes\n", sizeof(flags)); return 0; } 2. #include <stdio.h> #include <string.h> /* define simple structure */ struct { unsigned...

  • Combine two codes (code 1) to get names with(code 2) to get info: Code 1: #include<unistd.h>...

    Combine two codes (code 1) to get names with(code 2) to get info: Code 1: #include<unistd.h> #include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> #include<dirent.h> #include<stdio.h> #include<stdlib.h> void do_ls(char []); int main(int argc,char *argv[]) { if(argc == 1) do_ls("."); else while(--argc){ printf("%s:\n",*++argv); do_ls(*argv); } } void do_ls(char dirname[]) { DIR *dir_ptr; struct dirent *direntp; if((dir_ptr = opendir(dirname)) == NULL) fprintf(stderr,"ls1:cannot open %s\n",dirname); else { while((direntp = readdir(dir_ptr)) != NULL) printf("%s\n",direntp->d_name); closedir(dir_ptr); } } ____________________________ code 2: #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> void show_stat_info(char *,...

  • #include <stdio.h> #include <stdint.h> int Pointers_main (int arge, char *argv[]) int16_t a; // Declare a int32_t...

    #include <stdio.h> #include <stdint.h> int Pointers_main (int arge, char *argv[]) int16_t a; // Declare a int32_t int16_t *a P; // Declare a pointer to a int32_t (use *) printf ("a) Assigning a pointer: \n"); a = 1234; ap = &a; // Assign to be the address of a (use &) printf ("wwwa__,8d, a P_=_$d, a P_=_&p_da_=_&p\n", a, *a P, a P, ca); printf ("b) Dereferencing_a_pointer: \n"); *a p = 5678; // Set value of a by dereferencing pointer to a...

  • #include <stdio.h> #include<string.h> int main() { char strText[100] ="Start"; char i; int nTextASCIISum = strText[0] +...

    #include <stdio.h> #include<string.h> int main() { char strText[100] ="Start"; char i; int nTextASCIISum = strText[0] + strText[1] + strText[2]; int nTextLen = strlen(strText); int count = 0; printf("Welcome to token generator!\n"); printf("Enter a word to use in the token generator.\n You may enter as many words as you like. \n Press q and key when finished.\n"); scanf("%c", &strText[i]); //compute when to stop loop //check nTextLen == 1 and strText[0] == 'q' for(i=0;i< nTextLen;i++) if ((strlen(strText) != 1) || (strText[0] !=...

  • #include <stdio.h> .. int main(int argc, char *argv[]) { int base, power; printf("enter base and power:...

    #include <stdio.h> .. int main(int argc, char *argv[]) { int base, power; printf("enter base and power: "); scanf("%d %d", &base, &power); while (base != -100){ double res = pow(base, power); double res2 = my_pow(base, power); printf("pow: %.4f\n", res); printf("my_pow: %.4f\n", res2); .... } return 0; } // this function should be RECURSIVE // should not use any loop here double my_pow(double base, double p) { } lab4pow.c file contains: 2.1 Specification Write an ANSI-C program that reads input from the...

  • what is the output of the following program? #include<stdio.h> #include<string.h> int main(void){ char word[20]; int i...

    what is the output of the following program? #include<stdio.h> #include<string.h> int main(void){ char word[20]; int i =0    strcpy(word, "ORGANISE"); while(word[i] !='\0'){ if(i%2 ==1) word[i] = 'C'; i++; } printf("%s",word); return 0; }

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT