Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
208 views
in Technique[技术] by (71.8m points)

c++ - How to pass command line arguments to a c program

I've known how to write a program that accepts command line arguments ever since I learned to program. What I don't understand is how these parameters get their values. Hopefully I don't have these two mixed up but there is a difference between an argument and a parameter. An argument is the value given to the function when it is called such as: foo( a, b, c); where a, b, and c are the values. A parameter is the values that are inside the function while is being called.

So my question is how does a person pass command line arguments to a program? I understand how to read the arguments, that argc is the number of arguments, argv is a pointer to an array of strings containing the arguments, etc. etc. but I just don't know how to give those arguments a value..

I'm looking for information for both C and C++. I'm sort of a novice at this.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In a Windows environment you just pass them on the command line like so:

myProgram.exe arg1 arg2 arg3

argv[1] contain arg1 etc

The main function would be the following:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...