home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!ferkel.ucsb.edu!taco!gatech!swrinde!zaphod.mps.ohio-state.edu!howland.reston.ans.net!spool.mu.edu!hri.com!ukma!cs.widener.edu!widener!nobody
- From: wallace@cs.widener.edu (Dennis Wallace)
- Newsgroups: comp.lang.c
- Subject: Re: Could someone answer my question....thanks
- Message-ID: <1k3mo5INNnc9@lucy.cs.widener.edu>
- Date: 26 Jan 93 15:52:37 GMT
- References: <1993Jan26.000105.10930@njitgw.njit.edu> <1993Jan26.023236.17822@news.columbia.edu>
- Organization: Widener University CS Department, Chester PA
- Lines: 55
- NNTP-Posting-Host: lucy.cs.widener.edu
-
-
- >Following is a C program in which I tried to simulate command
- >line arguments (argc, argv). I am not getting the expected
- >output which should be the same as when argc, argv are used.
- >
- >
- >
- >#include <stdio.h>
- >
- >main(arg1,arg2)
- >int arg1;
- >char *arg2[];
- >{
- > int i;
- > for(i=0;i<arg1;i++)
- > puts("%d - %s",i,arg2[i]);
- >}
- >
- >
- >The following is the output when I tested the program for diff no.
- >of arguments. The 'for' loop iterates exactly the number of arguments,
- >but when I tried to print the number of args, as u can see from the
- >output, I am getting it wrong...
- >
- >6:45pm C-> a.out arg2
- >%d - %s
- >%d - %s
- >6:45pm C-> a.out arg2 arg3
- >%d - %s
- >%d - %s
- >%d - %s
- >6:46pm C-> a.out arg2 arg3 arg4
- >%d - %s
- >%d - %s
- >%d - %s
- >%d - %s
- >6:46pm C-> a.out
- >%d - %s
- >
- >
- >Could somebody suggest me in this regard ? Thanks in advance.
- >
- >Sekhar Chitti
- >
- :
- :Well, as far as I know, puts() does not recognize any format
- :specifiers (like %d, %s, etc). It just takes a string as an argument
- :and prints it out on the sdout. That's why you get these results (it
- :prints just the string, i.e. whatever is inside the quotation marks).
- :
-
- One other thing. There is ALWAYS an extra argumentwhen you read in the
- command line. The argument *arg2[0] is almost always the name of the
- program.
- DW
-