how to pass float value from argv?

M

miloody

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
 
W

Willem

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
 
R

Richard Bos

Willem said:
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
 
S

Sunny

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()
 
I

Ian Collins

Richard said:
Sunny said:



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.
 
C

CBFalconer

Willem said:
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.
 
K

Keith Thompson

Ian Collins said:
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.
 
S

Sunny

Sunny said:



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
 
M

miloody

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

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
 
C

CBFalconer

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>
 
B

Ben Bacarisse

miloody said:
On May 10, 9:34 am, CBFalconer <[email protected]> wrote:

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).
 
K

Kenny McCormack

Richard said:
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...
 
B

Ben Bacarisse

CBFalconer said:
miloody wrote:

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.
 
B

BartC

CBFalconer said:

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
 
K

Keith Thompson

Richard Heathfield said:
CBFalconer said:

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).
 
K

Keith Thompson

Malcolm McLean said:
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.)
 
F

Flash Gordon

Malcolm said:
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$
 
M

miloody

Dear all:
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$
Appreciate all your kind help,
miloody
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top