how to pass float value from argv?

Discussion in 'C Programming' started by miloody, May 9, 2009.

  1. miloody

    miloody Guest

    Dear all:
    I know atoi can transfer char* to integer such that I can pass integer
    to program by argv.
    But how can I pass float value by argv?
    appreciate your help,
    miloody
    miloody, May 9, 2009
    #1
    1. Advertising

  2. miloody

    Willem Guest

    miloody wrote:
    ) Dear all:
    ) I know atoi can transfer char* to integer such that I can pass integer
    ) to program by argv.
    ) But how can I pass float value by argv?
    ) appreciate your help,
    ) miloody

    Do you also know what atoi stands for ? If so, you should be able to make
    a good guess yourself.


    SaSW, Willem
    --
    Disclaimer: I am in no way responsible for any of the statements
    made in the above text. For all I know I might be
    drugged or something..
    No I'm not paranoid. You all think I'm paranoid, don't you !
    #EOT
    Willem, May 9, 2009
    #2
    1. Advertising

  3. miloody

    Richard Bos Guest

    Willem <> wrote:

    > miloody wrote:
    > ) I know atoi can transfer char* to integer such that I can pass integer
    > ) to program by argv.
    > ) But how can I pass float value by argv?
    >
    > Do you also know what atoi stands for ? If so, you should be able to make
    > a good guess yourself.


    A bad guess, actually; much better to use strtol() and strtod() instead
    of atoi() and atof().

    Richard
    Richard Bos, May 9, 2009
    #3
  4. miloody

    Sunny Guest

    On May 9, 8:36 pm, Willem <> wrote:
    > miloody wrote:
    >
    > ) Dear all:
    > ) I know atoi can transfer char* to integer such that I can pass integer
    > ) to program by argv.
    > ) But how can I pass float value by argv?
    > ) appreciate your help,
    > ) miloody
    >
    > Do you also know what atoi stands for ?  If so, you should be able to make
    > a good guess yourself.
    >
    > SaSW, Willem
    > --
    > Disclaimer: I am in no way responsible for any of the statements
    >             made in the above text. For all I know I might be
    >             drugged or something..
    >             No I'm not paranoid. You all think I'm paranoid, don't you !
    > #EOT


    It would be better to try not to use atoi()/atof(). If you read the C
    manual. It clearly says that both atoi() and atof() has been
    deprecated by strtol() and strtod()
    Sunny, May 9, 2009
    #4
  5. miloody

    Ian Collins Guest

    Richard Heathfield wrote:
    > Sunny said:
    >
    > <snip>
    >
    >> It would be better to try not to use atoi()/atof().

    >
    > Agreed.
    >
    >> If you read
    >> the C manual. It clearly says that both atoi() and atof() has been
    >> deprecated by strtol() and strtod()

    >
    > Which C manual? If you mean the Standard, C&V please. If you mean
    > the man pages, mine don't say any such thing.


    Some do, the (Open)Solaris page says:

    Calls to atoi() and atol() might be faster than correspond-
    ing calls to strtol(), and calls to atoll() might be faster
    than corresponding calls to strtoll(). However, applications
    should not use the atoi(), atol(), or atoll() functions
    unless they know the value represented by the argument will
    be in range for the corresponding result type.

    --
    Ian Collins
    Ian Collins, May 9, 2009
    #5
  6. miloody

    CBFalconer Guest

    Willem wrote:
    > miloody wrote:
    >
    >> I know atoi can transfer char* to integer such that I can pass
    >> integer to program by argv. But how can I pass float value by
    >> argv? appreciate your help,

    >
    > Do you also know what atoi stands for ? If so, you should be
    > able to make a good guess yourself.


    Don't recommend atoi. strtod is better, detects errors, etc.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.
    CBFalconer, May 10, 2009
    #6
  7. Ian Collins <> writes:
    > Richard Heathfield wrote:
    >> Sunny said:
    >>
    >> <snip>
    >>
    >>> It would be better to try not to use atoi()/atof().

    >>
    >> Agreed.
    >>
    >>> If you read
    >>> the C manual. It clearly says that both atoi() and atof() has been
    >>> deprecated by strtol() and strtod()

    >>
    >> Which C manual? If you mean the Standard, C&V please. If you mean
    >> the man pages, mine don't say any such thing.

    >
    > Some do, the (Open)Solaris page says:
    >
    > Calls to atoi() and atol() might be faster than correspond-
    > ing calls to strtol(), and calls to atoll() might be faster
    > than corresponding calls to strtoll(). However, applications
    > should not use the atoi(), atol(), or atoll() functions
    > unless they know the value represented by the argument will
    > be in range for the corresponding result type.


    Yes, but that doesn't say they're deprecated.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, May 10, 2009
    #7
  8. miloody

    Sunny Guest

    On May 10, 5:34 am, Richard Heathfield <> wrote:
    > Sunny said:
    >
    > <snip>
    >
    > > It would be better to try not to use atoi()/atof().

    >
    > Agreed.
    >
    > > If you read
    > > the C manual. It clearly says that both atoi() and atof() has been
    > > deprecated by strtol() and strtod()

    >
    > Which C manual? If you mean the Standard, C&V please. If you mean
    > the man pages, mine don't say any such thing.
    >
    > --
    > Richard Heathfield <http://www.cpax.org.uk>
    > Email: -http://www. +rjh@
    > Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    > "Usenet is a strange place" - dmr 29 July 1999


    I was referring to the man pages on my MacOS. However I checked the
    man pages on Ubuntu and it doesn't say so. The man pages on Ubuntu
    only says that both atoi() and atof() are the same as strtol() and
    strtof(). The only difference is that atoi()/atof() does not detect
    errors.
    Sorry for the confusion.

    Best,
    Sunny
    Sunny, May 10, 2009
    #8
  9. miloody

    miloody Guest

    On May 10, 9:34 am, CBFalconer <> wrote:
    > Willem wrote:
    > > miloody wrote:

    >
    > >> I know atoi can transfer char* to integer such that I can pass
    > >> integer to program by argv.  But how can I pass float value by
    > >> argv? appreciate your help,

    >
    > > Do you also know what atoi stands for ?  If so, you should be
    > > able to make a good guess yourself.

    >
    > Don't recommend atoi.  strtod is better, detects errors, etc.
    >
    > --
    >  [mail]: Chuck F (cbfalconer at maineline dot net)
    >  [page]: <http://cbfalconer.home.att.net>
    >             Try the download section.


    hi:
    thanks for your all kind help.
    I have one question about using strtof.
    Below is the proper type of it.
    float strtof(const char *nptr, char **endptr);

    but I cannot get correct value like below:
    (gdb) list
    16
    17
    18
    19 int main(int argc, char** argv){
    20 char *f="1e-1";
    21 float f2=0;
    22 f2=strtof("0.125",NULL);
    23
    24
    25
    (gdb) n
    22 f2=strtof("0.125",NULL);
    1: f2 = 0
    (gdb)
    32 PPYBlock temp=calloc(1, sizeof(ppYblocks));
    1: f2 = 1.04018739e+09
    (gdb)
    Did I use the wrong parameter format?
    But I can get correct value by the same "0.125" under strtod.
    thanks for your help,
    miloody
    miloody, May 10, 2009
    #9
  10. miloody

    CBFalconer Guest

    miloody wrote:
    > CBFalconer <> wrote:
    >> Willem wrote:
    >>> miloody wrote:

    >>
    >>>> I know atoi can transfer char* to integer such that I can pass
    >>>> integer to program by argv. But how can I pass float value by
    >>>> argv? appreciate your help,
    >>>
    >>> Do you also know what atoi stands for ? If so, you should be
    >>> able to make a good guess yourself.

    >>
    >> Don't recommend atoi. strtod is better, detects errors, etc.
    > >
    > > --
    > > [mail]: Chuck F (cbfalconer at maineline dot net)
    > > [page]: <http://cbfalconer.home.att.net>
    > > Try the download section.


    DON't quote signatures. They are everything after the "-- "
    marker, inclusive.

    >
    > thanks for your all kind help. I have one question about using
    > strtof. Below is the proper type of it.
    > float strtof(const char *nptr, char **endptr);
    > but I cannot get correct value like below:

    .... snip ...
    > Did I use the wrong parameter format? But I can get correct value
    > by the same "0.125" under strtod.


    Are you trying to read a floating value or an integer? Pick the
    appropriate routine. Read the C standard (marked C99 below), or
    the dinkumware C library docs.

    Some useful references about C:
    <http://www.ungerhu.com/jxh/clc.welcome.txt>
    <http://c-faq.com/> (C-faq)
    <http://benpfaff.org/writings/clc/off-topic.html>
    <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf> (C99)
    <http://cbfalconer.home.att.net/download/n869_txt.bz2> (pre-C99)
    <http://www.dinkumware.com/c99.aspx> (C-library}
    <http://gcc.gnu.org/onlinedocs/> (GNU docs)
    <http://clc-wiki.net/wiki/C_community:comp.lang.c:Introduction>
    <http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.
    CBFalconer, May 10, 2009
    #10
  11. miloody <> writes:

    > On May 10, 9:34 am, CBFalconer <> wrote:

    <snip>
    >> --
    >>  [mail]: Chuck F (cbfalconer at maineline dot net)
    >>  [page]: <http://cbfalconer.home.att.net>
    >>             Try the download section.


    It's best not to quote people's sig blocks.

    > I have one question about using strtof.
    > Below is the proper type of it.
    > float strtof(const char *nptr, char **endptr);
    >
    > but I cannot get correct value like below:
    > (gdb) list
    > 19 int main(int argc, char** argv){
    > 20 char *f="1e-1";
    > 21 float f2=0;
    > 22 f2=strtof("0.125",NULL);
    > (gdb) n
    > 22 f2=strtof("0.125",NULL);
    > 1: f2 = 0
    > (gdb)
    > 32 PPYBlock temp=calloc(1, sizeof(ppYblocks));
    > 1: f2 = 1.04018739e+09
    > (gdb)
    > Did I use the wrong parameter format?


    Check that you have a valid prototype in scope. Compile with at least
    enough warnings to see when a function is used without one (-Wall is
    enough for gcc). Note that you need to do more than just #include
    <stdlib.h> because strof is C99 not C90 (gcc -Wall -std=c99 will do).

    --
    Ben.
    Ben Bacarisse, May 10, 2009
    #11
  12. In article <gu6ko1$nag$>,
    Richard <> wrote:
    ....
    >Just who do you think you are?
    >
    >Chuck old son, you're a laughing stock. Take a holiday. Leave the C
    >standard at home. Trust me, you'll come back a better person.


    Oh the mental images...

    1) Chuck (or Keith) packing for vacation:
    a) Tickets - check
    b) Cash - check
    c) Sunblock lotion - check
    d) C standard - check

    or

    2) Chuck on the beach at Puerto Vallarta, reading the C standard...
    Kenny McCormack, May 10, 2009
    #12
  13. CBFalconer <> writes:

    > miloody wrote:

    <snip>
    >> thanks for your all kind help. I have one question about using
    >> strtof. Below is the proper type of it.
    >> float strtof(const char *nptr, char **endptr);
    >> but I cannot get correct value like below:

    > ... snip ...
    >> Did I use the wrong parameter format? But I can get correct value
    >> by the same "0.125" under strtod.

    >
    > Are you trying to read a floating value or an integer? Pick the
    > appropriate routine. Read the C standard (marked C99 below), or
    > the dinkumware C library docs.


    He posted code with float variable and is using strtof to set it.
    What make you think he has not already "picked the appropriate
    routine"? He's almost certainly not got a valid prototype in scope
    but he appears to be using a suitable function.

    --
    Ben.
    Ben Bacarisse, May 10, 2009
    #13
  14. miloody

    BartC Guest

    "CBFalconer" <> wrote in message
    news:...
    > miloody wrote:
    >> CBFalconer <> wrote:
    >>> Willem wrote:
    >>>> miloody wrote:
    >>>
    >>>>> I know atoi can transfer char* to integer such that I can pass
    >>>>> integer to program by argv. But how can I pass float value by
    >>>>> argv? appreciate your help,
    >>>>
    >>>> Do you also know what atoi stands for ? If so, you should be
    >>>> able to make a good guess yourself.
    >>>
    >>> Don't recommend atoi. strtod is better, detects errors, etc.


    >> thanks for your all kind help. I have one question about using
    >> strtof. Below is the proper type of it.
    >> float strtof(const char *nptr, char **endptr);
    >> but I cannot get correct value like below:

    > ... snip ...
    >> Did I use the wrong parameter format? But I can get correct value
    >> by the same "0.125" under strtod.

    >
    > Are you trying to read a floating value or an integer? Pick the
    > appropriate routine. Read the C standard (marked C99 below), or
    > the dinkumware C library docs.
    >
    > Some useful references about C:
    > <http://www.ungerhu.com/jxh/clc.welcome.txt>
    > <http://c-faq.com/> (C-faq)
    > <http://benpfaff.org/writings/clc/off-topic.html>
    > <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf> (C99)
    > <http://cbfalconer.home.att.net/download/n869_txt.bz2> (pre-C99)
    > <http://www.dinkumware.com/c99.aspx> (C-library}
    > <http://gcc.gnu.org/onlinedocs/> (GNU docs)
    > <http://clc-wiki.net/wiki/C_community:comp.lang.c:Introduction>
    > <http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>


    Yes, and next week's winning (UK) lottery numbers are here:

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
    29 30 31 32 33 34 35 36
    37 38 39 40 41 42 43 44 45 46 47 48 49

    --
    Bart
    BartC, May 10, 2009
    #14
  15. Richard Heathfield <> writes:
    > CBFalconer said:
    >> Willem wrote:
    >>> miloody wrote:
    >>>> I know atoi can transfer char* to integer such that I can pass
    >>>> integer to program by argv. But how can I pass float value by
    >>>> argv? appreciate your help,
    >>>
    >>> Do you also know what atoi stands for ? If so, you should be
    >>> able to make a good guess yourself.

    >>
    >> Don't recommend atoi.

    >
    > He didn't.


    It looks to me like he did, at least indirectly. Someone reading
    Willem's resonse without reading the rest of this thread would
    naturally reach the conclusion that atof is the right solution. This
    is rather clearly based on the assumption that atoi is the right way
    to get integer values from argv. atoi and atof share the same problem
    (lack of error checking and undefined behavior on certain errors).

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, May 10, 2009
    #15
  16. "Malcolm McLean" <> writes:
    > "Sunny" <> wrote in message
    >>I was referring to the man pages on my MacOS. However I checked the
    >>man pages on Ubuntu and it doesn't say so. The man pages on Ubuntu
    >>only says that both atoi() and atof() are the same as strtol() and
    >>strtof(). The only difference is that atoi()/atof() does not detect
    >>errors.
    >>Sorry for the confusion.
    >>

    > My version of strtol() doesn't detect errors either. Pass it a huge
    > integer and it scans to the end of the string and returns a garbage
    > result. (This is gcc on Linux).


    Presumably it's glibc, not gcc, that's providing your strtol() function.

    Did you check errno? On overflow, strtol() returns LONG_MIN or
    LONG_MAX and sets errno to ERANGE. (It works fine on my Linux
    system.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, May 10, 2009
    #16
  17. miloody

    Flash Gordon Guest

    Malcolm McLean wrote:
    > "Sunny" <> wrote in message
    >> I was referring to the man pages on my MacOS. However I checked the
    >> man pages on Ubuntu and it doesn't say so. The man pages on Ubuntu
    >> only says that both atoi() and atof() are the same as strtol() and
    >> strtof(). The only difference is that atoi()/atof() does not detect
    >> errors.
    >> Sorry for the confusion.
    >>

    > My version of strtol() doesn't detect errors either. Pass it a huge integer
    > and it scans to the end of the string and returns a garbage result. (This is
    > gcc on Linux).


    It's actually glibc on Linux. However, my copy correctly returns
    LONG_MAX and sets errno to ERANGE as required by the standard. So I
    suggest you are not using it properly or are using a very buggy old version.

    [markg@cpa-re-test ~]16$ cat t.c
    #include <errno.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <limits.h>
    #include <string.h>
    int main(void)
    {
    char *src = "999999999999999999999999999999";
    char *end;
    long v = strtol(src,&end,0);
    fprintf(stdout,"v-correct=%d, out-of-range=%d, scanned-correct=%d\n",
    v==LONG_MAX,errno==ERANGE,(end-src)==strlen(src));
    return 0;
    }
    [markg@cpa-re-test ~]17$ gcc t.c -ansi -pedantic -Wall -W
    t.c: In function `main':
    t.c:12: warning: comparison between signed and unsigned
    [markg@cpa-re-test ~]18$ ./a.out
    v-correct=1, out-of-range=1, scanned-correct=1
    [markg@cpa-re-test ~]19$

    --
    Flash Gordon
    Flash Gordon, May 10, 2009
    #17
  18. miloody

    miloody Guest

    Dear all:
    On May 11, 1:41 am, Flash Gordon <> wrote:
    > Malcolm McLean wrote:
    > > "Sunny" <> wrote in message
    > >> I was referring to the man pages on my MacOS. However I checked the
    > >> man pages on Ubuntu and it doesn't say so. The man pages on Ubuntu
    > >> only says that both atoi() and atof() are the same as strtol() and
    > >> strtof(). The only difference is that atoi()/atof() does not detect
    > >> errors.
    > >> Sorry for the confusion.

    >
    > > My version of strtol() doesn't detect errors either. Pass it a huge integer
    > > and it scans to the end of the string and returns a garbage result. (This is
    > > gcc on Linux).

    >
    > It's actually glibc on Linux. However, my copy correctly returns
    > LONG_MAX and sets errno to ERANGE as required by the standard. So I
    > suggest you are not using it properly or are using a very buggy old version.
    >
    > [markg@cpa-re-test ~]16$ cat t.c
    > #include <errno.h>
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <limits.h>
    > #include <string.h>
    > int main(void)
    > {
    >     char *src = "999999999999999999999999999999";
    >     char *end;
    >     long v = strtol(src,&end,0);
    >     fprintf(stdout,"v-correct=%d, out-of-range=%d, scanned-correct=%d\n",
    >             v==LONG_MAX,errno==ERANGE,(end-src)==strlen(src));
    >     return 0;}
    >
    > [markg@cpa-re-test ~]17$ gcc t.c -ansi -pedantic -Wall -W
    > t.c: In function `main':
    > t.c:12: warning: comparison between signed and unsigned
    > [markg@cpa-re-test ~]18$ ./a.out
    > v-correct=1, out-of-range=1, scanned-correct=1
    > [markg@cpa-re-test ~]19$
    >
    > --
    > Flash Gordon

    Appreciate all your kind help,
    miloody
    miloody, May 12, 2009
    #18
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. bd
    Replies:
    0
    Views:
    615
  2. Bret

    char **argv vs. char* argv[]

    Bret, Aug 31, 2003, in forum: C Programming
    Replies:
    21
    Views:
    4,575
    Richard Heathfield
    Sep 3, 2003
  3. David
    Replies:
    10
    Views:
    5,944
    Richard Heathfield
    Sep 15, 2003
  4. Hal Styli
    Replies:
    14
    Views:
    1,626
    Old Wolf
    Jan 20, 2004
  5. Carsten Fuchs
    Replies:
    45
    Views:
    1,526
    James Kanze
    Oct 8, 2009
Loading...

Share This Page