search
Login

Categories


C Books Guide and List
C++ Books Guide and List
Best Java Books

3.8k questions

4k answers

117 comments

10.7k users

0 votes
49 views
asked Guru 24.6k points 29 51 76

1 Answer

0 votes
In C, we can supply arguments to 'main' function. The arguments that we pass to main ( ) at command prompt are called command line arguments. These arguments are supplied at the time of invoking the program.The main ( ) function can take arguments as: main(int argc, char *argv[]) { } The first argument argc is known as 'argument counter'. It represents the number of arguments in the  command line. The second argument argv is known as 'argument vector'. It is an array of char type pointers that  points tothe command line arguments. Size of this array will be equal to the value of argc.

Example: at the command prompt if we give:

C:\> fruit.exe apple mango

Then argc would contain value 3 argv [0] would contain base address of string " fruit.exe" which is the command name that invokes the program.

argv [1] would contain base address of string "apple"

argv [2] would contain base address of string "mango"

here apple and mango are the arguments passed to the program fruit.exe

Program:

#include <stdio.h>

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

int n;

printf("Following are the arguments entered in the command line");

for (n = 0; n < argc; n++) {

printf("\n %s", argv[n]);

}

printf("\n Number of arguments entered are\n %d\n", argc);

return 0;

}

Output:

Following are the arguments entered in the command line

C:\testproject.exe

apple

mango

Number of arguments entered are

3
answered Guru 39.1k points 10 21 32

Related questions

1 vote
1 answer 104 views
104 views
what is command line arguments of main function?
asked steve Sr Member 1.5k points 8 23 36
0 votes
1 answer 35 views
35 views
asked sara Guru 24.6k points 29 51 76
0 votes
1 answer 23 views
0 votes
1 answer 31 views
0 votes
1 answer 152 views
152 views
Write a program to accept three sides of a triangle and transfer them to a function to compute the area of a triangle.
asked anonymous
...