command line argument 
Author Message
 command line argument

/* could anyone tell me why this doesn't work? */

#include <stdio.h>

main(int argc, int * argv[])

{

if(* argv[1] >= 100)
        printf("argv is bigger or equal to 100!\n");
else
        printf("argv is less than 100!\n");

return 0;

Quote:
}

/* I cant see why this shouldn't work! */


Sun, 27 Jun 2004 19:09:05 GMT  
 command line argument
/* could anyone tell me why this doesn't work? */

#include <stdio.h>

main(int argc, int * argv[])

{

if(* argv[1] >= 100)
        printf("argv is bigger or equal to 100!\n");
else
        printf("argv is less than 100!\n");

return 0;

Quote:
}

/* I cant see why this shouldn't work! */


Sun, 27 Jun 2004 19:12:07 GMT  
 command line argument


Quote:
> /* could anyone tell me why this doesn't work? */

> #include <stdio.h>

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

That's not a legal definition for `main`; its second argument should
be a char**, not an int**.

Quote:
> {

> if(* argv[1] >= 100)
>         printf("argv is bigger or equal to 100!\n");
> else
>         printf("argv is less than 100!\n");

> return 0;

> }

> /* I cant see why this shouldn't work! */

because it's not *supposed* to work. WIBNI doesn't apply.

--
Chris "C being C, and none of CPL, Ada, PL/1, Lisp, Smalltalk, or Pop" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgroup/comp/comp.lang.c.html



Sun, 27 Jun 2004 19:59:56 GMT  
 command line argument

Quote:

> /* could anyone tell me why this doesn't work? */
> #include <stdio.h>
> main(int argc, int * argv[])

You got this wrong, argv is an array of char pointers (and main() always
returns an int):

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

Quote:
> {
> if(* argv[1] >= 100)
>         printf("argv is bigger or equal to 100!\n");
> else
>         printf("argv is less than 100!\n");

Now it should be obvious why this won't work - you must convert the
string argv[ 1 ] to an integer before comparing. But before you do so
you should first check if argv[ 1 ] isn't NULL! And you should also be
prepared for situations where you don't get passed a string that can
be interpreted as an integer, e.g. if the user typed "lO0" instead of
"100".
                                        Regards, Jens
--
      _  _____  _____

  _  | |  | |    | |          AG Moebius, Institut fuer Molekuelphysik
 | |_| |  | |    | |          Fachbereich Physik, Freie Universitaet Berlin
  \___/ens|_|homs|_|oerring   Tel: ++49 (0)30 838 - 53394 / FAX: - 56046


Sun, 27 Jun 2004 20:28:14 GMT  
 command line argument
On Wednesday, in article


Quote:
>/* could anyone tell me why this doesn't work? */

>#include <stdio.h>

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

You have an incorrect type for argv. You need

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

argv points to the first element of an array of pointers to strings.

Quote:
>{

>if(* argv[1] >= 100)

argv[1] is a pointer to a string. If that string has the form of an integer
you can convert it to an integer type using a fucntion like strtol().
E.g.

  {
      long number;

      number = strtol(argv[1], NULL, 10);
      if (number >= 100)

Quote:
>        printf("argv is bigger or equal to 100!\n");
>else
>        printf("argv is less than 100!\n");

That isn't argv but the argument corresponding to argv[1]. Note that
the code assumes that argv[1] exists, you should test that argc>1 before
attempting toa ccess argv[1]. Also to use strtol() you should
#include <stdlib.h> at the top.

Quote:
>return 0;

>}

--
-----------------------------------------


-----------------------------------------


Sun, 27 Jun 2004 20:26:31 GMT  
 command line argument

Quote:


> > /* could anyone tell me why this doesn't work? */

> > #include <stdio.h>

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

> You got this wrong, argv is an array of char pointers (and main() always
> returns an int):

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

> > {
> > if(* argv[1] >= 100)
> >         printf("argv is bigger or equal to 100!\n");
> > else
> >         printf("argv is less than 100!\n");

> Now it should be obvious why this won't work - you must convert the
> string argv[ 1 ] to an integer before comparing. But before you do so

i'm not very sure, but i think, *argv[1] >= 100 will work (if: char
*argv[]), but it will compaire the Ascii number, surely not what it is
expected to do :-)

Quote:
> you should first check if argv[ 1 ] isn't NULL! And you should also be
> prepared for situations where you don't get passed a string that can
> be interpreted as an integer, e.g. if the user typed "lO0" instead of
> "100".
>                                         Regards, Jens

--
#!/usr/bin/perl
use Inline C=>q{char trans (int n, char c){





Mon, 28 Jun 2004 02:02:13 GMT  
 command line argument

Quote:


>> Now it should be obvious why this won't work - you must convert the
>> string argv[ 1 ] to an integer before comparing. But before you do so
> i'm not very sure, but i think, *argv[1] >= 100 will work (if: char
> *argv[]), but it will compaire the Ascii number, surely not what it is
> expected to do :-)

That's of course correct. I was just assuming from the context that the
OP was expecting an integer as input to the program ;-)

                                         Regards, Jens
--
      _  _____  _____

  _  | |  | |    | |          AG Moebius, Institut fuer Molekuelphysik
 | |_| |  | |    | |          Fachbereich Physik, Freie Universitaet Berlin
  \___/ens|_|homs|_|oerring   Tel: ++49 (0)30 838 - 53394 / FAX: - 56046



Mon, 28 Jun 2004 03:26:09 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. command line arguments, double clicking an associated file

2. command line arguments

3. command line arguments

4. Command line arguments/string problem with *argv[]

5. Command line arguments

6. command line argument

7. Command Line Arguments

8. Numerical values as command line arguments?

9. numerical values as command line arguments?

10. command line arguments

11. Mixed Command Line Arguments in C/C++

12. need help with command line arguments

 

 
Powered by phpBB® Forum Software