SlideShare a Scribd company logo
2
Most read
7
Most read
8
Most read
Command Line Arguments
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.
Command line arguments are important for your program, especially when
you want to control your program from outside, instead of hard coding those
values inside the code.
Let us suppose you want to write a C program "hello.c" that prints a "hello" message
for a user.
Instead of reading the name from inside the program with scanf(), we wish to pass the
name from the command line as follows −
C:usersuser>hello Prakash
The string will be used as an argument to the main() function and then the "Hello Prakash" message
should be displayed.
argc and argv
To facilitate the main() function to accept arguments from the command line, you
should define two arguments in the main() function – argc and argv[].
argc refers to the number of arguments passed and argv[] is a pointer array that
points to each argument passed to the program.
int main(int argc, char *argv[]) {
...
...
return 0;
}
The argc argument should always be non-negative. The argv
argument is an array of character pointers to all the arguments,
argv[0] being the name of the program. After that till "argv [argc
- 1]", every element is a command-line argument.
#include <stdio.h>
int main(int argc, char* argv[])
{
printf("Program name is: %s" , argv[0]);
if (argc == 1)
printf("nNo Extra Command Line Argument Passed "
"Other Than Program Name" );
if (argc >= 2) {
printf("nNumber Of Arguments Passed: %d" , argc);
printf("n----Following Are The Command Line "
"Arguments Passed----" );
for (int i = 0; i < argc; i++)
printf("nargv[%d]: %s" , i, argv[i]);
}
return 0;
}
Terminal Input $ ./a.out First Second Third
Output
Program Name Is: ./a.out
Number Of Arguments Passed: 4
----Following Are The Command Line Arguments Passed----
argv[0]: ./a.out
argv[1]: First
argv[2]: Second
argv[3]: Third

More Related Content

Similar to Command Line Arguments in Computer Programming (20)

DOCX
1 CMPS 12M Introduction to Data Structures Lab La.docx
tarifarmarie
 
PDF
15 2. arguement passing to main
웅식 전
 
PPT
L8 file
mondalakash2012
 
DOCX
What is the general format for a Try-Catch block Assume that amt l .docx
ajoy21
 
PPT
file.ppt
DeveshDewangan5
 
PDF
File Handling in C Programming
RavindraSalunke3
 
DOCX
#include string.h#include stdlib.h#include systypes.h.docx
AASTHA76
 
PPTX
Raspberry pi Part 6
Techvilla
 
PPT
Chtp414
giovanniveitch
 
PPTX
Introduction to Basic C programming 02
Wingston
 
PDF
Command line-arguments-in-java-tutorial
Kuntal Bhowmick
 
PDF
File_Management_in_C
NabeelaNousheen
 
PPT
File handling(some slides only)
Kamlesh Nishad
 
PPT
CInputOutput.ppt
mohammadsajidansari4
 
PPTX
C basics
thirumalaikumar3
 
PPTX
C Programming Unit-5
Vikram Nandini
 
PPTX
basic of c programming practicals.......
anjanasharma77573
 
PPTX
Chapter 1: Introduction
Eric Chou
 
PPT
Command line arguments.21
myrajendra
 
DOCX
BACKGROUND A shell provides a command-line interface for users. I.docx
wilcockiris
 
1 CMPS 12M Introduction to Data Structures Lab La.docx
tarifarmarie
 
15 2. arguement passing to main
웅식 전
 
What is the general format for a Try-Catch block Assume that amt l .docx
ajoy21
 
file.ppt
DeveshDewangan5
 
File Handling in C Programming
RavindraSalunke3
 
#include string.h#include stdlib.h#include systypes.h.docx
AASTHA76
 
Raspberry pi Part 6
Techvilla
 
Introduction to Basic C programming 02
Wingston
 
Command line-arguments-in-java-tutorial
Kuntal Bhowmick
 
File_Management_in_C
NabeelaNousheen
 
File handling(some slides only)
Kamlesh Nishad
 
CInputOutput.ppt
mohammadsajidansari4
 
C Programming Unit-5
Vikram Nandini
 
basic of c programming practicals.......
anjanasharma77573
 
Chapter 1: Introduction
Eric Chou
 
Command line arguments.21
myrajendra
 
BACKGROUND A shell provides a command-line interface for users. I.docx
wilcockiris
 

Recently uploaded (20)

PPTX
fatigue in aircraft structures-221113192308-0ad6dc8c.pptx
aviatecofficial
 
PDF
Set Relation Function Practice session 24.05.2025.pdf
DrStephenStrange4
 
PDF
Viol_Alessandro_Presentazione_prelaurea.pdf
dsecqyvhbowrzxshhf
 
PDF
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
PDF
Zilliz Cloud Demo for performance and scale
Zilliz
 
PPTX
Green Building & Energy Conservation ppt
Sagar Sarangi
 
PPTX
265587293-NFPA 101 Life safety code-PPT-1.pptx
chandermwason
 
DOC
MRRS Strength and Durability of Concrete
CivilMythili
 
PPTX
Depth First Search Algorithm in 🧠 DFS in Artificial Intelligence (AI)
rafeeqshaik212002
 
PPTX
Lecture 1 Shell and Tube Heat exchanger-1.pptx
mailforillegalwork
 
PPTX
原版一样(Acadia毕业证书)加拿大阿卡迪亚大学毕业证办理方法
Taqyea
 
PDF
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
PPTX
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
PPTX
Element 7. CHEMICAL AND BIOLOGICAL AGENT.pptx
merrandomohandas
 
PDF
Electrical Engineer operation Supervisor
ssaruntatapower143
 
PPTX
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
PPT
Carmon_Remote Sensing GIS by Mahesh kumar
DhananjayM6
 
PDF
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
PPTX
Hashing Introduction , hash functions and techniques
sailajam21
 
PPTX
Shinkawa Proposal to meet Vibration API670.pptx
AchmadBashori2
 
fatigue in aircraft structures-221113192308-0ad6dc8c.pptx
aviatecofficial
 
Set Relation Function Practice session 24.05.2025.pdf
DrStephenStrange4
 
Viol_Alessandro_Presentazione_prelaurea.pdf
dsecqyvhbowrzxshhf
 
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
Zilliz Cloud Demo for performance and scale
Zilliz
 
Green Building & Energy Conservation ppt
Sagar Sarangi
 
265587293-NFPA 101 Life safety code-PPT-1.pptx
chandermwason
 
MRRS Strength and Durability of Concrete
CivilMythili
 
Depth First Search Algorithm in 🧠 DFS in Artificial Intelligence (AI)
rafeeqshaik212002
 
Lecture 1 Shell and Tube Heat exchanger-1.pptx
mailforillegalwork
 
原版一样(Acadia毕业证书)加拿大阿卡迪亚大学毕业证办理方法
Taqyea
 
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
Element 7. CHEMICAL AND BIOLOGICAL AGENT.pptx
merrandomohandas
 
Electrical Engineer operation Supervisor
ssaruntatapower143
 
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
Carmon_Remote Sensing GIS by Mahesh kumar
DhananjayM6
 
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
Hashing Introduction , hash functions and techniques
sailajam21
 
Shinkawa Proposal to meet Vibration API670.pptx
AchmadBashori2
 
Ad

Command Line Arguments in Computer Programming

  • 2. 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. Command line arguments are important for your program, especially when you want to control your program from outside, instead of hard coding those values inside the code.
  • 3. Let us suppose you want to write a C program "hello.c" that prints a "hello" message for a user. Instead of reading the name from inside the program with scanf(), we wish to pass the name from the command line as follows − C:usersuser>hello Prakash The string will be used as an argument to the main() function and then the "Hello Prakash" message should be displayed.
  • 4. argc and argv To facilitate the main() function to accept arguments from the command line, you should define two arguments in the main() function – argc and argv[]. argc refers to the number of arguments passed and argv[] is a pointer array that points to each argument passed to the program.
  • 5. int main(int argc, char *argv[]) { ... ... return 0; }
  • 6. The argc argument should always be non-negative. The argv argument is an array of character pointers to all the arguments, argv[0] being the name of the program. After that till "argv [argc - 1]", every element is a command-line argument.
  • 7. #include <stdio.h> int main(int argc, char* argv[]) { printf("Program name is: %s" , argv[0]); if (argc == 1) printf("nNo Extra Command Line Argument Passed " "Other Than Program Name" ); if (argc >= 2) { printf("nNumber Of Arguments Passed: %d" , argc); printf("n----Following Are The Command Line " "Arguments Passed----" ); for (int i = 0; i < argc; i++) printf("nargv[%d]: %s" , i, argv[i]); } return 0; }
  • 8. Terminal Input $ ./a.out First Second Third Output Program Name Is: ./a.out Number Of Arguments Passed: 4 ----Following Are The Command Line Arguments Passed---- argv[0]: ./a.out argv[1]: First argv[2]: Second argv[3]: Third