home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / c / 20224 < prev    next >
Encoding:
Internet Message Format  |  1993-01-27  |  1.8 KB

  1. 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
  2. From: wallace@cs.widener.edu (Dennis Wallace)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Could someone answer my question....thanks
  5. Message-ID: <1k3mo5INNnc9@lucy.cs.widener.edu>
  6. Date: 26 Jan 93 15:52:37 GMT
  7. References: <1993Jan26.000105.10930@njitgw.njit.edu> <1993Jan26.023236.17822@news.columbia.edu>
  8. Organization: Widener University CS Department, Chester PA
  9. Lines: 55
  10. NNTP-Posting-Host: lucy.cs.widener.edu
  11.  
  12.  
  13. >Following is a C program in which I tried to simulate command
  14. >line arguments (argc, argv). I am not getting the expected
  15. >output which should be the same as when argc, argv are used.
  16. >
  17. >
  18. >
  19. >#include <stdio.h>
  20. >
  21. >main(arg1,arg2)
  22. >int arg1;
  23. >char *arg2[];
  24. >{
  25. >       int i;
  26. >       for(i=0;i<arg1;i++)
  27. >               puts("%d - %s",i,arg2[i]);
  28. >}
  29. >
  30. >
  31. >The following is the output when I tested the program for diff no.
  32. >of arguments. The 'for' loop iterates exactly the number of arguments,
  33. >but when I tried to print the number of args, as u can see from the
  34. >output, I am getting it wrong...
  35. >
  36. >6:45pm C-> a.out arg2
  37. >%d - %s
  38. >%d - %s
  39. >6:45pm C-> a.out arg2 arg3
  40. >%d - %s
  41. >%d - %s
  42. >%d - %s
  43. >6:46pm C-> a.out arg2 arg3 arg4
  44. >%d - %s
  45. >%d - %s
  46. >%d - %s
  47. >%d - %s
  48. >6:46pm C-> a.out 
  49. >%d - %s
  50. >
  51. >
  52. >Could somebody suggest me in this regard ? Thanks in advance.
  53. >
  54. >Sekhar Chitti
  55. >
  56. :
  57. :Well, as far as I know, puts() does not recognize any format
  58. :specifiers (like %d, %s, etc).  It just takes a string as an argument
  59. :and prints it out on the sdout.  That's why you get these results (it
  60. :prints just the string, i.e. whatever is inside the quotation marks).
  61. :
  62.  
  63. One other thing. There is ALWAYS an extra argumentwhen you read in the
  64. command line. The argument *arg2[0] is almost always the name of the
  65. program. 
  66.     DW
  67.