[newbie] about command line arguments

I

iceBlue

Hello all

I was answering a 'Quiz on C' when i stumbled upon these questions on
CL-Arguments.I know the questions are stupid and wont be needed for any
serious coding..but iam curious to know the answers :)

*What is the maximum combined length of command line arguments including the
space between adjacent arguments?

*Are the variables argc and argv local to main?

*If we want that any wildcard characters in the command line arguments
should be appropriately expanded, are we required to make any special
provision? If yes, which?

*Does there exist any way to make the command line arguments available to
other functions without passing them as arguments to the function?

-iceBlue-
 
J

Joona I Palaste

iceBlue said:
Hello all
I was answering a 'Quiz on C' when i stumbled upon these questions on
CL-Arguments.I know the questions are stupid and wont be needed for any
serious coding..but iam curious to know the answers :)
*What is the maximum combined length of command line arguments including the
space between adjacent arguments?

Implementation-dependent, I should guess.
*Are the variables argc and argv local to main?

Yes. *All* function parameters are local to that function.
*If we want that any wildcard characters in the command line arguments
should be appropriately expanded, are we required to make any special
provision? If yes, which?

I don't understand what you mean by "any special provision", but I think
that yes you have to do it. C has no knowledge of "wildcards". You'll
have to implement the entire expansion algorithm yourself or use a
third-party solution.
*Does there exist any way to make the command line arguments available to
other functions without passing them as arguments to the function?

Store them in global variables.

--
/-- Joona Palaste ([email protected]) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"The large yellow ships hung in the sky in exactly the same way that bricks
don't."
- Douglas Adams
 
M

Materialised

iceBlue said:
Hello all

I was answering a 'Quiz on C' when i stumbled upon these questions on
CL-Arguments.I know the questions are stupid and wont be needed for any
serious coding..but iam curious to know the answers :)

*What is the maximum combined length of command line arguments including the
space between adjacent arguments?

*Are the variables argc and argv local to main?

*If we want that any wildcard characters in the command line arguments
should be appropriately expanded, are we required to make any special
provision? If yes, which?

*Does there exist any way to make the command line arguments available to
other functions without passing them as arguments to the function?

-iceBlue-
*sniff*
This wreaks of homework assignment!
lol


--
ø¤º°`°ø,¸¸,ø¤º°`°ø,¸¸,ø¤º°`°ø,¸¸,ø¤º°`°øø¤º°`°ø,¸¸,ø¤º°`°ø

Yahoo Messenger: Materialised
MSN Messenger: Walker123412 at hotmail dot com
GnomeMeeting: Materialised at ntlworld dot com

ø¤º°`°ø,¸¸,ø¤º°`°ø,¸¸,ø¤º°`°ø,¸¸,ø¤º°`°øø¤º°`°ø,¸¸,ø¤º°`°ø
 
J

Joona I Palaste

It depends on the system. Under linux wild cards and otrher such stuff
is handled by the shell..I think the same goes for windows and dos
though the functionality is different. A guess from me would say that in
the context of the quiz the answer is no. The C answer is that main
takes whatever is given to it and if whatever calls main does not handle
it you will have to.

Whether your answer is true depends on your definition of "command line
arguments". Are those those that the user types from his shell, or those
that the C program ends up receiving?
Note that even in Linux, it's possible to say something like
"./mygroovyprogram \*" and then argv[1] will be the string "*". You can
then expand it by hand if you want to. No one is forcing you to expand
it to every file (and directory) in the current directory. You can
expand it to the names of all people who live in Paris, for example.

--
/-- Joona Palaste ([email protected]) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"Nothing lasts forever - so why not destroy it now?"
- Quake
 
E

Eric Sosman

iceBlue said:
Hello all

I was answering a 'Quiz on C' when i stumbled upon these questions on
CL-Arguments.I know the questions are stupid and wont be needed for any
serious coding..but iam curious to know the answers :)

Since it's a "Quiz on C" I'll provide C answers.
*What is the maximum combined length of command line arguments including the
space between adjacent arguments?

Implementation-dependent; C imposes no maximum and requires
no minimum. (And as far as I can tell, the implementation is
not required to document whatever limits it may impose.)
*Are the variables argc and argv local to main?

No. For example, consider this program:

double argc;
int argv;
int main(int x, char**y) { return 0; }

Clearly, the variables argc and argv are not local to main().
*If we want that any wildcard characters in the command line arguments
should be appropriately expanded, are we required to make any special
provision? If yes, which?

Yes, special provisions are required. One such might be
to invoke the C program from a helper program (sometimes called
a "shell") that takes care of the expansion.
*Does there exist any way to make the command line arguments available to
other functions without passing them as arguments to the function?

Yes. For example,

#include <stdio.h>
char **argv_copy;

int func(void) {
return *argv_copy > 0;
}

int main(int argc, char **argv) {
argv_copy = argv;
if (func) puts(argv[0]);
return 0;
}
 
J

Joona I Palaste

No. For example, consider this program:
double argc;
int argv;
int main(int x, char**y) { return 0; }
Clearly, the variables argc and argv are not local to main().

Crap. I missed that in my original reply. I am so used to naming the
parameters argc and argv I immediately associated the names to main()'s
parameters. Now I feel foolish.

--
/-- Joona Palaste ([email protected]) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"He said: 'I'm not Elvis'. Who else but Elvis could have said that?"
- ALF
 
E

Eric Sosman

Joona said:
Crap. I missed that in my original reply. I am so used to naming the
parameters argc and argv I immediately associated the names to main()'s
parameters. Now I feel foolish.

I've often thought that

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

.... would make a good beginning for an IOCCC entry.
 
T

Thomas Stegen

Joona said:
I don't understand what you mean by "any special provision", but I think
that yes you have to do it. C has no knowledge of "wildcards". You'll
have to implement the entire expansion algorithm yourself or use a
third-party solution.

It depends on the system. Under linux wild cards and otrher such stuff
is handled by the shell..I think the same goes for windows and dos
though the functionality is different. A guess from me would say that in
the context of the quiz the answer is no. The C answer is that main
takes whatever is given to it and if whatever calls main does not handle
it you will have to.
 
D

Darrell Grainger

Hello all

I was answering a 'Quiz on C' when i stumbled upon these questions on
CL-Arguments.I know the questions are stupid and wont be needed for any
serious coding..but iam curious to know the answers :)

*What is the maximum combined length of command line arguments including the
space between adjacent arguments?

This would be implementation defined. It is not really about C language.
It is about the command line environment you are running a program in.
*Are the variables argc and argv local to main?

Assuming you define main as:

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

then yes, they are local to main. Something the person creating the
questions might not have consider would be:

int main(int weird, char *difference[])

is perfectly valid. This means, if argc and argv exist they don't have to
be local to main. Additionally, there is nothing saying there is only one
instance of argc and argv. I could have:

void func(void)
{
int argc, argv;
/* stuff */
}

as well as the usual main function.
*If we want that any wildcard characters in the command line arguments
should be appropriately expanded, are we required to make any special
provision? If yes, which?

This is all implementation defined/command line environment specific
information. Just like the first question, this is not really about C
language.
*Does there exist any way to make the command line arguments available to
other functions without passing them as arguments to the function?

Create global variables and assign the argc and argv variables to those
global variables.
 
A

Alan Balmer

It depends on the system. Under linux wild cards and otrher such stuff
is handled by the shell..I think the same goes for windows and dos
though the functionality is different. A guess from me would say that in
the context of the quiz the answer is no.
Without knowing the context of the quiz, I wouldn't try to guess. In
DOS (and probably Windows, though I'm not sure) the standard shell
does not expand wildcards, and many C implementations provided a
library function to do it.
The C answer is that main
takes whatever is given to it and if whatever calls main does not handle
it you will have to.

Yup.
 
G

Gordon Burditt

I was answering a 'Quiz on C' when i stumbled upon these questions on
CL-Arguments.I know the questions are stupid and wont be needed for any
serious coding..but iam curious to know the answers :)

*What is the maximum combined length of command line arguments including the
space between adjacent arguments?

//STEPNAME EXEC PGM=CC,PARM='-o,xyz,xyz.c'

*WHAT* spaces between adjacent arguments?

Nobody said there is a limit on the length of command-line arguments
including the space. The limit might be, for example, $25.00 and
100 yen, each argument costing $1.00 and 1 yen, and each character
in each argument NOT including the space costing $0.01, except
quotes, which cost $0.05 and 2 yen. You are NOT allowed to exchange
yen for dollars or vice versa.
*Are the variables argc and argv local to main?

Assuming you mean the arguments to main(), yes.
*If we want that any wildcard characters in the command line arguments
should be appropriately expanded, are we required to make any special
provision? If yes, which?

ANSI C makes no guarantees that there are wildcards. If there are,
it's up to whatever you use to invoke the program. For example,
the UNIX exec() function doesn't expand wildcards. Most UNIX shells
do.
*Does there exist any way to make the command line arguments available to
other functions without passing them as arguments to the function?

ANSI C doesn't guarantee that division by zero WON'T do this.
There is no standard way to do this.

Gordon L. Burditt
 
R

Russ Bobbitt

Joona said:
Crap. I missed that in my original reply. I am so used to naming the
parameters argc and argv I immediately associated the names to main()'s
parameters. Now I feel foolish.

I've often thought that

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

... would make a good beginning for an IOCCC entry.

I've maintained some code where the programmer did exactly that. :-/
 
M

Micah Cowan

iceBlue said:
Hello all

I was answering a 'Quiz on C' when i stumbled upon these questions on
CL-Arguments.I know the questions are stupid and wont be needed for any
serious coding..but iam curious to know the answers :)

*What is the maximum combined length of command line arguments including the
space between adjacent arguments?

There is no answer for this. In C, command line arguments don't have
"space between them" -- they are passed via a vector of pointers-to-char.
*Are the variables argc and argv local to main?
Yes.

*If we want that any wildcard characters in the command line arguments
should be appropriately expanded, are we required to make any special
provision? If yes, which?

"Appropriately expanded"? Generally, this is expected of the command
shell, not of the program.
*Does there exist any way to make the command line arguments available to
other functions without passing them as arguments to the function?

You can of course store their values in globals, but I'm not crazy
about that.

-Micah
 
D

Dan Pop

In said:
It depends on the system. Under linux wild cards and otrher such stuff
is handled by the shell..I think the same goes for windows and dos
though the functionality is different.

Nope. On DOS implementations, it is the C runtime that does (or does not)
expand them (you can usually control this behaviour via a global
variable or by linking a certain object module to your application).
A guess from me would say that in
the context of the quiz the answer is no.

The problem is that we don't know the context of the quiz. Is it about
Unix programming, DOS programming, a certain implementation? Some of the
questions simply make no sense in an implementation-independent context...

Dan
 
D

Dan Pop

In said:
If this is homework they should consider a different course. These are
some pretty useless questions.

Agreed. Especially considering that some of the answers are highly
platform specific.

Dan
 
D

Dan Pop

In said:
There is no answer for this. In C, command line arguments don't have
"space between them" -- they are passed via a vector of pointers-to-char.

They usually have null characters between them :)

Dan
 
P

Peter Shaggy Haywood

Groovy hepcat iceBlue was jivin' on 26 Jun 2003 17:30:50 GMT in
comp.lang.c.
[newbie] about command line arguments's a cool scene! Dig it!
I was answering a 'Quiz on C' when i stumbled upon these questions on
CL-Arguments.I know the questions are stupid and wont be needed for any
serious coding..but iam curious to know the answers :)

And what will you give us for doing your homework for you?
*What is the maximum combined length of command line arguments including the
space between adjacent arguments?

Mu. There is no space between adjacent command line arguments, as
far as C is concerned.
*Are the variables argc and argv local to main?

Mu. What are argc and argv? It is comman practice to call main()'s
parameters argc and argv respectively. But this is not universal. Nor
is it unheard of to give other variables or function parameters those
names. You could even have macros or functions with those names.
*If we want that any wildcard characters in the command line arguments
should be appropriately expanded, are we required to make any special
provision? If yes, which?

Mu. What your program understands to be a wildcard character is
entirely up to your program, as is the action taken when such a
character is encountered. As far as C is concerned, there is no such
thing as a wildcard character.
*Does there exist any way to make the command line arguments available to
other functions without passing them as arguments to the function?

Oh, really! Can't you fire up one lousy brain cell and answer this
one yourself? It's ludicrously simple. (Although, I don't generally
approve of the solution. But that's beside the point.)

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 
R

Richard Bos

John Smith said:
Russ Bobbitt said:
Joona I Palaste wrote:

Eric Sosman <[email protected]> scribbled the following:
iceBlue wrote:
*Are the variables argc and argv local to main?

No. For example, consider this program:

double argc;
int argv;
int main(int x, char**y) { return 0; }

Clearly, the variables argc and argv are not local to main().

Crap. I missed that in my original reply. I am so used to naming the
parameters argc and argv I immediately associated the names to main()'s
parameters. Now I feel foolish.

I've often thought that

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

... would make a good beginning for an IOCCC entry.

I've maintained some code where the programmer did exactly that. :-/

OK smarties, enlighten the rest of us...

main() is (almost) an ordinary function. Because of this, it does not
matter a whit, to the compiler, what you name its arguments. It is
customary to define main() as

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

or similar when one uses command line arguments. Most people stick to
this custom, and that is a good thing, because it keeps down confusion.
However, _from the implementation's POV_, it is completely immaterial
what the names of those parameters are, as long as you use them
consistently. Thus,

int main(int number_of_arguments, char **text_of_arguments)

is just as legal as the canonical definition. So is

int main(int x, char **y)

and so is Russ' definition. Compare his definition to the one I gave.
Closely. Legal, but very, very unwise...

Richard
 
Z

Zoran Cutura

Nisse Engstrom said:
Sure.

<code>

gets (){}main(const
puts ,char**
getc ){static
putc ;static char**
gets ;return!
gets ?(
putc =
puts ,
gets =
getc ,un\
getc ()):!
getc ?
putc :
puts <!
gets ?(*
getc =*(
gets -
puts -!!
puts ),!
getc ):!
gets ;}un\
getc (){auto
putc ,
getc ;char*
gets ;
putc =main(!&
putc ,!&
getc );for(
getc =!&
gets ;
getc <
putc ;
getc ++)main(-
getc -!!un\
getc ,&
gets ),
puts (
gets );return!
puts ;}

</code>

Here is an unbfuscated version of this code:
#include <stdio.h>

int func(void);

int main(const int argc, char **argv)
{
static st_argc;
static char** st_argv;

if (st_argv == 0) { /* at startup remember argc and argv */
st_argc = argc;
st_argv = argv;
} else {

/* assume that am called from within program! */
if (argv == 0) /* initially called back from function */
return st_argc; /* return initial argc */

if (argc > 0) {
*argv = st_argv[argc-1];
}

return 0;
}

func();

/* here do all the other stuff main needs to do */

return 0;
}

int func(void)
{
int main_argc, i;
char *nextarg;

main_argc = main(0, 0); /* initial call needs to pass 0 in argv!! */

for(i = 0; i < main_argc; i++) {
main(i+1, nextarg);
puts(nextarg); /* here is where the arguments can be used! */
}

return 0;
}

I think it assumes that argc cannot be 0 which is not guarenteed by the
standard, but it should be easy to add a check for that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top