[comp.lang.c] Args to C progs??

mitchemt@silver.bacs.indiana.edu (03/05/89)

     I am writing a program in Turbo C and would like to know how to pass
arguments to it from the command line after it is compiled to a .exe file.

I have tried:

main(arg1)
char *arg1;
{
......
 /* do something with arg1 here */
.....
}

Unfortunately, the program doesn't seem to recognize arg1. If I pass it a 
string arg1 is just empty. Any help is appreciated. I just want to pass a 
string to the program.  
                       Thanks in advance,
                                 TM

chan@galaxy.ee.rochester.edu (Daniel Chan) (03/05/89)

>From: mitchemt@silver.bacs.indiana.edu
>Date: 4 Mar 89 22:57:00 GMT
>Organization: Indiana University CSCI, Bloomington


>     I am writing a program in Turbo C and would like to know how to pass
> arguments to it from the command line after it is compiled to a .exe file.

>I have tried:

>main(arg1)
>char *arg1;
>{
>......
> /* do something with arg1 here */
>.....
>}

>Unfortunately, the program doesn't seem to recognize arg1. If I pass it a 
>string arg1 is just empty. Any help is appreciated. I just want to pass a 
>string to the program.  
>                       Thanks in advance,
>                                 TM

Have you tried the following:

main (argc, argv)
int argc;	/* total number of arguements including program name itself */
char *argv[];   /* an array of strings */
{
 ...
}

The command line arguments can then be accessed by argv[1], argv[2], ...,
 argv[argc-1].

Hope that will work in Turbo C.

-dan.

carlp@iscuva.ISCS.COM (Carl Paukstis) (03/05/89)

In article <9600003@silver> [name deleted to protect the guilty] writes:
>
>     I am writing a program in Turbo C and would like to know how to pass
>arguments to it from the command line after it is compiled to a .exe file.

[simple mistake deleted]

Did a new semester just start?  I know, I'm just as guilty of wasting
bandwidth with this followup, but I just can't help it.  Seems like an
unusually large bunch of these types of articles lately.  I'm especially
diamayed by this in comp.lang.c, which has generally been remarkably free
of blatant beginner's mistakes.

ATTENTION NEW USERS OF THE NET:  Please read "news.anounce.newusers", and
please contemplate reading a manual or textbook before posting.  Answers to
elementary questions are best found in resources at hand, rather than on a
worldwide network.

There, I've got that out of my system now.  Thanks for your indulgence.
(By way of atonement for my own sins, I sent a pleasant response via email
to the party above.  S/He probably knows more about argc,argv now than s/he
originally wanted to know.))
-- 
Carl Paukstis    +1 509 927 5600 x5321  |"The right to be heard does not
                                        | automatically include the right
UUCP:     carlp@iscuvc.ISCS.COM         | to be taken seriously."
          ...uunet!iscuva!carlp         |                  - H. H. Humphrey

c08_d103@jhunix.HCF.JHU.EDU (Ex-God) (03/06/89)

In article <9600003@silver> mitchemt@silver.bacs.indiana.edu writes:
>
>     I am writing a program in Turbo C and would like to know how to pass
>arguments to it from the command line after it is compiled to a .exe file.
>
>I have tried:
>
>main(arg1)
>char *arg1;

     Well, I don't know Turbo C, but if it's reasonably standard, it
should pass an integer and an array of strings.  Try

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

Then argv[0] will hold the command name, argv[1] will hold the first
argument, etc.  As far as I know, there's no definition for what makes
an argument, but in Unix, it's a word separated by white space.  Oh,
argc will hold the number of arguments (including the command name).

For more information, check K&R1 5.11 (p110-114) or K&R2 5.10
(p114-118).  If someone knows Turbo C better than I do, tell me if
that will work....

     -- Andrew Barnert (Andy Social/Andy Christ/Andy Matter/ex-God)
        ins_balb@jhunix/ins_balb@jhuvms/c08_d103@jhunix
The opinions expressed in this message are yours.
"If you can't stand the Big Chill, burn down the freezer." -- Jello Biafra

bkbarret@sactoh0.UUCP (Brent K. Barrett) (03/07/89)

In article <993@jhunix.HCF.JHU.EDU>, c08_d103@jhunix.HCF.JHU.EDU (Ex-God) writes:
> In article <9600003@silver> mitchemt@silver.bacs.indiana.edu writes:
> >
> >     I am writing a program in Turbo C and would like to know how to pass
> >arguments to it from the command line after it is compiled to a .exe file.
 [Stuff removed]
>      Well, I don't know Turbo C, but if it's reasonably standard, it
> should pass an integer and an array of strings.  Try
> 
> main (argc, argv)
> int argc;
> char *argv[]
> 
> For more information, check K&R1 5.11 (p110-114) or K&R2 5.10
> (p114-118).  If someone knows Turbo C better than I do, tell me if
> that will work....
 
 Well, I know Turbo C pretty well. Yes, it's a fairly good ANSI C,
so what you stated will work and is true for Turbo C.
 
 To the previous poster, though, why didn't you consult your
reference manual? It's clearly stated how to access parameters
passed on the command line via the argc/argv combo.
    
 
 [ignore]
 
 

-- 
 "Somebody help me! I'm trapped in this computer!"
  
 Brent Barrett ..pacbell!sactoh0!bkbarret GEMAIL: B.K.BARRETT