0% found this document useful (0 votes)
10 views

Cmd

Command-line arguments are parameters passed to a program during execution, allowing for user input without interaction. The main function can accept these arguments through parameters argc (argument count) and argv (argument vector), where argc indicates the number of arguments and argv stores their values. A C program example demonstrates how to use command-line arguments to display the number of arguments entered and their values.

Uploaded by

247r1a66k2
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Cmd

Command-line arguments are parameters passed to a program during execution, allowing for user input without interaction. The main function can accept these arguments through parameters argc (argument count) and argv (argument vector), where argc indicates the number of arguments and argv stores their values. A C program example demonstrates how to use command-line arguments to display the number of arguments entered and their values.

Uploaded by

247r1a66k2
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Command-line arguments are simple parameters that are given on the system's

command line, and the values of these arguments are passed on to your program
during program execution. When a program starts execution without user
interaction, command-line arguments are used to pass values or files to it.

 When the main function of a program contains arguments, then these


arguments are known as Command Line Arguments.
 The main function can be created with two methods: first with no parameters
(void) and second with two parameters.
 The parameters are argc and argv, where argc is an integer and the argv is a
list of command line arguments.
 argc denotes the number of arguments given, while argv[] is a pointer array
pointing to each parameter passed to the program.
 If no argument is given, the value of argc will be 1.
 The value of argc should be non-negative.

* Main function without arguments:

int main()

* Main function with arguments:

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

here argc means argument count i.e :total number of arguments passed from
command prompt to your program.

Argv[]-----argument vector stores the values of arguments.

Always argv[0] will be our file name.

 Command line arguments in C are passed to the main function


as argc and argv.
 Command line arguments are used to control the program from the outside.
 argv[argc] is a Null pointer.
 The name of the program is stored in argv[0], the first command-line
parameter in argv[1], and the last argument in argv[n].

//C program to demonstrate command line arguments.

#include <stdio.h>

// defining main with arguments

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

{ int i;

printf("You have entered %d arguments:\n", argc);

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

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

return 0;

How to execute:

1.Go to run and type cmd.

2.In command prompt enter your file path.

3.Enter your file name & arguments like below.

C:\Users\Admin>cd Desktop

C:\Users\Admin\Desktop>example 10 20 30
You have entered 4 arguments:

example

10

20

30

You might also like