Argument Processing

D

Daniel Rudy

Hello,

I'm trying to learn how command line arguments are handled in C. The
following code segment that I wrote as a test program compiles, but when
I try to run it, it core dumps. This is under a FreeBSD environment.
What am I doing wrong here?

/*

just echos the command line arguments onto the screen
tests the format of argument processing

*/

#include <stdio.h>
#include <string.h>


int main(int argc, char argv[])
{
int i; /* generic counter */

printf("argc = %d", argc);
for (i = 0; i <= argc; i++)
{
printf("argv[%d] = %s\n", i, argv);
}

/* return to operating system */
return(0);
}
 
R

Ravi Uday

"Daniel Rudy"
Hello,

I'm trying to learn how command line arguments are handled in C. The
following code segment that I wrote as a test program compiles, but when
I try to run it, it core dumps. This is under a FreeBSD environment.
What am I doing wrong here?

/*

just echos the command line arguments onto the screen
tests the format of argument processing

*/

#include <stdio.h>
#include <string.h>


int main(int argc, char argv[])
{
int i; /* generic counter */

printf("argc = %d", argc);
for (i = 0; i <= argc; i++)
{
printf("argv[%d] = %s\n", i, argv);
}

/* return to operating system */
return(0);
}



What are you passing to main from command line ?
You might try adding this before the first printf.

if ( argc < 3 )
{
printf ("Insufficient arguments to main\n");
return 0;
}


- Ravi
 
J

Jack Klein

Hello,

I'm trying to learn how command line arguments are handled in C. The
following code segment that I wrote as a test program compiles, but when
I try to run it, it core dumps. This is under a FreeBSD environment.
What am I doing wrong here?

/*

just echos the command line arguments onto the screen
tests the format of argument processing

*/

#include <stdio.h>
#include <string.h>


int main(int argc, char argv[])
{
int i; /* generic counter */

printf("argc = %d", argc);
for (i = 0; i <= argc; i++)

Replace "<=" with "<" in the loop. argv[argv] is a null pointer.
{
printf("argv[%d] = %s\n", i, argv);
}

/* return to operating system */
return(0);


Note that 'return' is a statement, not a function. Parentheses do no
harm, but have not been required since the first ANSI standard 15
years ago.
 
S

Satyajit

Daniel said:
#include <stdio.h>
#include <string.h>


int main(int argc, char argv[])
{
int i; /* generic counter */

printf("argc = %d", argc);
for (i = 0; i <= argc; i++)

Rewrite this as
for (i = 0; i < argc; i++)
{
printf("argv[%d] = %s\n", i, argv);
}

/* return to operating system */
return(0);
}


Satyajit
 
E

E. Robert Tisdale

Daniel said:
I'm trying to learn how command line arguments are handled in C.
The following code segment that I wrote as a test program compiles
but when I try to run it, it core dumps.
This is under a FreeBSD environment.
What am I doing wrong here?
[snip]

> cat main.c
/*
*
* just echos the command line arguments onto the screen
* tests the format of argument processing
*
* */

#include <stdio.h>
#include <string.h>


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

printf("argc = %d\n", argc);
for (int i = 0; i < argc; ++i) {
printf("argv[%d] = %s\n", i, argv);
}

/* return to operating system */
return 0;
}
> gcc -Wall -std=c99 -pedantic -o main main.c
> ./main arg1 arg2 arg3
argc = 4
argv[0] = ./main
argv[1] = arg1
argv[2] = arg2
argv[3] = arg3
> gcc --version gcc (GCC) 3.4.2
> uname -o Darwin
> man 3 getopt
GETOPT(3) BSD Library Functions Manual GETOPT(3)

NAME
getopt - get option character from command line argument list

LIBRARY
Standard C Library (libc, -lc)

SYNOPSIS
#include <unistd.h>

extern char *optarg;
extern int optind;
extern int optopt;
extern int opterr;
extern int optreset;

int
getopt(int argc, char * const *argv, const char *optstring);
.
.
.
HISTORY
The getopt() function appeared in 4.3BSD.
.
.
.
 
W

wangxiaopeng1976

I think the problem is the declareation of the main function,
should be :
int main(int argc, char* argv[])

also should modify the <= to <
 
M

Mike Wahler

"Daniel Rudy"
Hello,

I'm trying to learn how command line arguments are handled in C. The
following code segment that I wrote as a test program compiles, but when
I try to run it, it core dumps. This is under a FreeBSD environment.
What am I doing wrong here?

See below.
/*

just echos the command line arguments onto the screen
tests the format of argument processing

*/

#include <stdio.h>
#include <string.h>


int main(int argc, char argv[])

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

/* or */

int main(int argc, char **argv)
{
int i; /* generic counter */

printf("argc = %d", argc);
for (i = 0; i <= argc; i++)

for (i = 0; i < argc; i++)

{
printf("argv[%d] = %s\n", i, argv);
}

/* return to operating system */
return(0);
}



-Mike
 
C

Chris Dollin

no problem with the declartion it is fine

Context would have been useful. The original post had:

int main(int argc, char argv[])

which *is* wrong - it's `char *argv[]` or `char **argv` (I prefer
the latter, myself, but the former means the same thing as a
function argument declaration).
 
D

Daniel Rudy

At about the time of 11/4/2004 10:00 PM, Ravi Uday stated the following:
Hello,

I'm trying to learn how command line arguments are handled in C. The
following code segment that I wrote as a test program compiles, but when
I try to run it, it core dumps. This is under a FreeBSD environment.
What am I doing wrong here?

/*

just echos the command line arguments onto the screen
tests the format of argument processing

*/

#include <stdio.h>
#include <string.h>


int main(int argc, char argv[])
{
int i; /* generic counter */

printf("argc = %d", argc);
for (i = 0; i <= argc; i++)
{
printf("argv[%d] = %s\n", i, argv);
}

/* return to operating system */
return(0);
}




What are you passing to main from command line ?


Just test stuff to see how it works.
You might try adding this before the first printf.

if ( argc < 3 )
{
printf ("Insufficient arguments to main\n");
return 0;
}

I did as you suggested, now it comes up with argc = 4 and then core dumps.
 
D

Daniel Rudy

At about the time of 11/5/2004 12:28 AM, Mike Wahler stated the following:
Hello,

I'm trying to learn how command line arguments are handled in C. The
following code segment that I wrote as a test program compiles, but when
I try to run it, it core dumps. This is under a FreeBSD environment.
What am I doing wrong here?


See below.

/*

just echos the command line arguments onto the screen
tests the format of argument processing

*/

#include <stdio.h>
#include <string.h>


int main(int argc, char argv[])


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

/* or */

int main(int argc, char **argv)

{
int i; /* generic counter */

printf("argc = %d", argc);
for (i = 0; i <= argc; i++)


for (i = 0; i < argc; i++)


{
printf("argv[%d] = %s\n", i, argv);
}

/* return to operating system */
return(0);
}




-Mike


Thanks to everyone who replied. The code now looks like this:

strata:/home/dcrudy/c 1047 $$$ ->cat argtest.c
/*

just echos the command line arguments onto the screen
tests the format of argument processing

*/

#include <stdio.h>
#include <string.h>


int main(int argc, char *argv[])
{
int i; /* generic counter */

if (argc < 3)
{
printf("error: need more arguments.\n");
return(0);
}
printf("argc = %d\n", argc);
for (i = 0; i < argc; i++)
{
printf("argv[%d] = %s\n", i, argv);
}

/* return to operating system */
return(0);
}


So now when I run it, instead of core dumping, I get the following output:

strata:/home/dcrudy/c 1046 $$$ ->./argtest arg1 arg2 arg3
argc = 4
argv[0] = ./argtest
argv[1] = arg1
argv[2] = arg2
argv[3] = arg3

Now there is something about this that I do not understand. What
exactly is the nature of argv? I know it's an array, but is it an array
of char? A number of people pointed out that the * was required.
AFAIK, * is a pointer reference.

Thanks again.
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

Daniel said:
Now there is something about this that I do not understand. What
exactly is the nature of argv? I know it's an array, but is it an array
of char? A number of people pointed out that the * was required.
AFAIK, * is a pointer reference.

Thanks again.
argv is char *argv[] .A n array, of pointers to char.
The pointers actually points to the 1. element of a
nul terminated array of chars, but you can't tell from
that declaration though.

Think of it as an array of pointers to C strings.
 
M

Mike Wahler

"Daniel Rudy"
Thanks to everyone who replied. The code now looks like this:

strata:/home/dcrudy/c 1047 $$$ ->cat argtest.c
/*

just echos the command line arguments onto the screen
tests the format of argument processing

*/

#include <stdio.h>
#include <string.h>


int main(int argc, char *argv[])
{
int i; /* generic counter */

if (argc < 3)
{
printf("error: need more arguments.\n");
return(0);
}
printf("argc = %d\n", argc);
for (i = 0; i < argc; i++)
{
printf("argv[%d] = %s\n", i, argv);
}

/* return to operating system */
return(0);
}


So now when I run it, instead of core dumping, I get the following output:

strata:/home/dcrudy/c 1046 $$$ ->./argtest arg1 arg2 arg3
argc = 4
argv[0] = ./argtest
argv[1] = arg1
argv[2] = arg2
argv[3] = arg3

Now there is something about this that I do not understand. What
exactly is the nature of argv? I know it's an array,


Actually, it's not. Arrays cannot be passed to or returned
from functions.
but is it an array
of char?

No. It's a pointer (to an array of pointers (to char)).
This allows for arguments of varying lengths.
A number of people pointed out that the * was required.
AFAIK, * is a pointer reference.

:)

-Mike
 
M

Mike Wahler

Nils O. Selåsdal said:
Daniel said:
Now there is something about this that I do not understand. What
exactly is the nature of argv? I know it's an array, but is it an array
of char? A number of people pointed out that the * was required.
AFAIK, * is a pointer reference.

Thanks again.
argv is char *argv[] .A n array, of pointers to char.

No, 'argv' is *not* an array, it's a pointer.
The pointers actually points to the 1. element of a
nul terminated array of chars, but you can't tell from
that declaration though.

Think of it as an array of pointers to C strings.

No. It's a pointer to such an array.

-Mike
 
T

Tim Rentsch

Jack Klein said:
Note that 'return' is a statement, not a function. Parentheses do no
harm, but have not been required since the first ANSI standard 15
years ago.

Just a question - were parentheses EVER required on a 'return'
statement in C?
 
C

Chris Torek

Just a question - were parentheses EVER required on a 'return'
statement in C?

It is quite possible.

Note that the syntax for "struct" once used parentheses rather than
braces, too:

struct ( int a; int b; );

As late as Version 6 Unix, the op= operators were spelled the other
way around, initializers for static-duration objects were written
without an "=" sign, and there were other oddities:

int x 5;

int main(argc, argv) char **argv; {

printf(2, "this goes to stderr: argc = %d\n", argc);

argc =- 1; /* ignore program name */
argv =+ 1;

...

}

The "standard I/O" library was originally a separate option; to
use it you had to link with "-lS" (I think -- I never actually
*used* V6, much less anything earlier like V5).
 
T

Tim Rentsch

Chris Torek said:
Just a question - were parentheses EVER required on a 'return'
statement in C?

It is quite possible.

[numerous obsolete/obsolescent C-isms noted]

It might be good to ask the question this way - can any CLC-er
identify an old C compiler (or even remember using such a compiler)
where the syntax for 'return' required parentheses?
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top