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

command line

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

command line

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

what is Command line arguments

DEFINITION

Instead of invoking the input statement from


inside the program, it is possible to pass data
from the command line to the main() function
when the program is executed. These values are
called command line arguments.
what is Command line arguments

• Command line arguments in C allow you to pass


information to your program when you run it
from the command line.
• These arguments are typically used to influence
the behavior of the program.
• Command line arguments are parameters
supplied to a program when it is invoked. They
are used to pass information to the program at
runtime.
• Command-line arguments are the values given after the
name of the program in the command-line shell of
Operating Systems.
• Command-line arguments are handled by the main()
function of a C program.
Properties of Command Line Arguments in C
• They are passed to the main() function.
• They are parameters/arguments supplied to the program
when it is invoked.
• They are used to control programs from outside instead of
hard coding those values inside the code.
• argv[argc] is a NULL pointer.
• argv[0] holds the name of the program.
• argv[1] points to the first command line argument and
argv[argc-1] points to the last argument.
where we will use
• Real-World Application
• Consider a program that requires
specific user inputs, such as file
processing or simple calculations:

• File Processing
• Error Handling
The main function can accept two arguments:

• argc

• argv
argv

• argv (Argument Vector): It is an array of


character pointers (strings) listing all the
arguments. argv[0] is the name of the program,
argv[1] is the first argument, argv[2] is the
second argument, and so on.

• argv (argument vector): It is an array of


character pointers listing all the arguments.
argc

• argc (Argument Count): It stores the number


of command line arguments passed, including
the program's name itself. So if there are three
command line arguments, argc will be 4 (the
program name + three arguments).
• argc (argument count): It represents the
number of command line arguments passed.
example

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


• {
• // Your code here
• }
This program prints all the command line arguments
passed to it:
• #include <stdio.h>
• int main(int argc, char *argv[])
• {
• int i;
• printf("Number of arguments: %d\n",
argc);
• printf("Arguments:\n");
• for (i = 0; i < argc; i++)
• { printf("argv[%d]: %s\n", i,
argv[i]);
• }
• return 0; }
run or compile

• ./program_name arg1 arg2 arg3

output
• Number of arguments: 4
• Arguments:
• argv[0]: ./program_name
• argv[1]: arg1
• argv[2]: arg2
• argv[3]: arg3
Handling Arguments

• Converting Arguments:

• Command line arguments are passed as


strings. To use them as numbers, you need to
convert them. The atoi() function can be used
for converting a string to an integer:
Example 2: Adding Two Numbers
• #include <stdio.h> #include <stdlib.h>
• int main(int argc, char *argv[])
• {
• if (argc != 3)
• { printf("Usage: %s num1 num2\n",
argv[0]);
• return 1; }
• int num1 = atoi(argv[1]);
• int num2 = atoi(argv[2]);
• int sum = num1 + num2;
• out put
• Sum: 15

You might also like