Learn C Language from the Best Tutors
Search in
You might be familiar with the following simple way to define the main function:
int main() {
/* blah blah */
}
but, the more general way to define main is as followsint main(int argc, char* argv[]) {
/* blah blah */
}
Suppose you compile this file into say a.out
, and then if you execute the program from the terminal (command prompt) as follows> ./a.out Hello World
the words Hello, and World are called as command-line arguments, and a.out is the executable (or program).
When executed as mentiond above, the values of argc and argv that the main
function recieves would be as follows:
argc
gets a value of 3 (the length of the argv
array)
argv[0]
points to a string with contents "./a.out"
argv[1]
points to a string with contents "Hello"
and argv[2]
points to a string with contents "World"
Within the main
function, you can program different behaviours depending on these variables.
Compile the following and execute it with different command-line arguments to see it in action:
read less#include <stdio.h>
int main(int argc, char* argv[]) {
int i = 0;
for(i = 0; i < argc; i++)
printf("%s\n", argv[i]);
return 0;
}
Related Questions
Now ask question in any of the 1000+ Categories, and get Answers from Tutors and Trainers on UrbanPro.com
Ask a QuestionRecommended Articles
Lasya Infotech: An educational Training...
Lasya Infotech is a Hyderabad based IT training institute founded in 2016 by O Venkat. Believing in his innovation, passion and persistence and with a diverse blend of experience, he started his brainchild to deliver exemplary professional courses to aspiring candidates by honing their skills. Ever since the institute envisions...
Brilliant Academy, a reputed B. Tech Tuition...
Brilliant Academy is one of the reputed institutes for B.Tech tuition classes. This institute is specialised in delivering quality tuition classes for B.E, Engineering - all streams and Engineering diploma courses. Incorporated in 2012, Brillant Academy is a brainchild of Mr Jagadeesh. The main motto of the academy is to...
8 Hottest IT Careers of 2014!
Whether it was the Internet Era of 90s or the Big Data Era of today, Information Technology (IT) has given birth to several lucrative career options for many. Though there will not be a “significant" increase in demand for IT professionals in 2014 as compared to 2013, a “steady” demand for IT professionals is rest assured...
Make a Career as a BPO Professional
Business Process outsourcing (BPO) services can be considered as a kind of outsourcing which involves subletting of specific functions associated with any business to a third party service provider. BPO is usually administered as a cost-saving procedure for functions which an organization needs but does not rely upon to...
Looking for C Language Classes?
Learn from the Best Tutors on UrbanPro
Are you a Tutor or Training Institute?
Join UrbanPro Today to find students near youThe best tutors for C Language Classes are on UrbanPro
The best Tutors for C Language Classes are on UrbanPro