0% found this document useful (0 votes)
22 views9 pages

Unit-IV Command Line Arguments

Uploaded by

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

Unit-IV Command Line Arguments

Uploaded by

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

Command Line Arguments

Unit-IV
Command Line Arguments
• The arguments passed from command line are called command line arguments.
• Command line arguments are simply arguments that are specified after the name
of the program in the system’s command line and these argument values are
passed on to the program during execution.
• These arguments are handled by main() function.
• int main(int argc, char *argv[] )
• Here, argc counts the number of arguments. It counts the file name as the first argument.
• The argv[] contains the total number of arguments. The first argument is the file name always.
Components of Command Line Arguments
• There are 2 components of Command Line Arguments in C:
1. argc:
• It refers to the “argument count” i.e the first parameter in the main function which is used to
store the number of command line argument supplied.
• Value of argc is greater than or equal to 0 (Zero).
2. argv:
• it refers to “argument vector”.
• It is basically an array of character pointer which is used to store all the command line
arguments.
Properties of Command Line Arguments
• They are used to control program 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
Syntax :
int main(int argc, char *argv[])
{
//body of main function
}
Example-1
#include<stdio.h>
int main(int argc, char *argv[])
{
int i;
printf("\n Your have supplied %d number of arguments.", argc);
for(i=0;i<argc;i++)
printf("\n%s",argv[i]);
return 0;
}
Example-1 : Execute
Compile:
#gcc cmdline.c
Run:
#./a.out Mr. Srinivas vlr Mehta
Output:
Your have supplied 5 number of arguments.
./a.out
Mr.
Srinivas
vlr
Mehta
Example-2
Program to display “Even” numbers from the Command Line Arguments
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char *argv[])
{
int i,n;
printf("\nYour have supplied %d number of arguments.", argc);
for(i=1;i<argc;i++)
printf("\n%s",argv[i]);
printf("\nEven Numbers are : ");
for(i=1;i<argc;i++)
{
n=atoi(argv[i]);
if(n%2==0)
printf("\n%d",n);
}
printf("\n");
return 0;
}
Example-2 : Execute
Compile:
#gcc cmdlineeven.c
Run:
#./a.out 11 22 33 44 55 66 56 77 8 34 61 23
Output:
Your have supplied 9 number of arguments.
11 • The atoi() function converts an integer value from
22
33 a string of characters.
55 • The input string is a character string that may be
66
8 turned into a return type numeric value.
34 • The function is specified in the header file stdlib.h
61
Even Numbers are :
22
66
8
34
Thank You
?

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy