Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > what's wrong with atof() and casting?

Reply
Thread Tools

what's wrong with atof() and casting?

 
 
XZ
Guest
Posts: n/a
 
      06-29-2005
Hi everyone, this is really confusing to me:

#include <stdio.h>
main(int argc, char **argv) {
printf("argv[1] = %f\n",(double)atof(argv[1]));
printf("argv[1] = %d\n\n",atoi(argv[1]));
}

$ a.out a
argv[1] = 97.000000
argv[1] = 0

$ a.out 3
argv[1] = 0.000000
argv[1] = 3

Without explicit casting, the first printf() always gives 0.0.

Could anyone help me understand what's wrong with the code?

Thank you for your time,
Steve
 
Reply With Quote
 
 
 
 
Jack Klein
Guest
Posts: n/a
 
      06-29-2005
On Tue, 28 Jun 2005 23:57:12 -0500, XZ <> wrote in
comp.lang.c:

> Hi everyone, this is really confusing to me:
>
> #include <stdio.h>
> main(int argc, char **argv) {
> printf("argv[1] = %f\n",(double)atof(argv[1]));
> printf("argv[1] = %d\n\n",atoi(argv[1]));
> }
>
> $ a.out a
> argv[1] = 97.000000


The output above is wrong, of course.

> argv[1] = 0
>
> $ a.out 3
> argv[1] = 0.000000
> argv[1] = 3
>
> Without explicit casting, the first printf() always gives 0.0.
>
> Could anyone help me understand what's wrong with the code?
>
> Thank you for your time,
> Steve


Your program invokes undefined behavior. You do not have a prototype
in scope for atof(). You need to include <stdlib.h>.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
Reply With Quote
 
 
 
 
Roman Mashak
Guest
Posts: n/a
 
      06-29-2005
Hello, Jack!
You wrote on Wed, 29 Jun 2005 00:51:45 -0500:

??>> Could anyone help me understand what's wrong with the code?
??>>
??>> Thank you for your time,
??>> Steve

JK> Your program invokes undefined behavior. You do not have a prototype
JK> in scope for atof(). You need to include <stdlib.h>.
Why is compilation and linking of code successful even without including
stdlib.h? At least linking error should arise, no ?

With best regards, Roman Mashak. E-mail:


 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      06-29-2005
On Wed, 29 Jun 2005 15:11:18 +0900, "Roman Mashak" <>
wrote in comp.lang.c:

> Hello, Jack!
> You wrote on Wed, 29 Jun 2005 00:51:45 -0500:
>
> ??>> Could anyone help me understand what's wrong with the code?
> ??>>
> ??>> Thank you for your time,
> ??>> Steve
>
> JK> Your program invokes undefined behavior. You do not have a prototype
> JK> in scope for atof(). You need to include <stdlib.h>.
> Why is compilation and linking of code successful even without including
> stdlib.h? At least linking error should arise, no ?


Prior to the 1999 update to the C language standard, it was allowed to
call a function without any sort of declaration or prototype in scope.

This would be defined behavior and work properly if and only if
certain conditions were met:

1. The return type of the function was int (true for atoi(), but NOT
true for atof()).

2. The function took a fixed number and type of arguments (that is,
not like printf() that takes variable arguments.

3. The arguments were all the types produced by default promotion
(that is, not char or short or float).

4. In your call of the function, the arguments you passed were the
correct number and type (after default promotion).

So when you call atoi(argv[1]), the compiler generates a call to a
function returning an int and accepting one char * parameter. This is
the correct type for atoi(), so the call works.

Then you call atof(argv[1]), the compiler generates a call to a
function returning an int and accepting one char * parameter. Since
atof() DOES NOT return an int, this is wrong and undefined behavior.

Note that a compiler is not required to do anything at all when you
generate undefined behavior. Since there is a function with that
name, it links. Some C compilers use an object file format where they
can detect a mis-match in return types when linking, others do not.
There is no requirement in the C standard that they do.

Note that under the 1999 and later versions of the C standard, this
"implicit declaration of function returning int" has been removed from
the language. A compiler conforming to the later standard versions
would be required to issue a diagnostic for this, but that still does
not prevent it from trying to produce a program.

Finally, every compiler I have ever used has an option to generate
some sort of message when you call a function without a prototype,
even prior to C99 where this was not actually a constraint violation.
You should consult your compiler's documentation to see how to enable
such warnings.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
Reply With Quote
 
Roman Mashak
Guest
Posts: n/a
 
      06-29-2005
Hello, Jack!
You wrote on Wed, 29 Jun 2005 01:52:19 -0500:

??>> Why is compilation and linking of code successful even without
??>> including stdlib.h? At least linking error should arise, no ?

JK> Prior to the 1999 update to the C language standard, it was allowed to
JK> call a function without any sort of declaration or prototype in scope.
[skip]

Moreover, atoi() and atof() are both defined in stdlib.h but only #include
<stdio.h> is put in the source code. Why does compilation succeed anyway?

With best regards, Roman Mashak. E-mail:


 
Reply With Quote
 
Rajan
Guest
Posts: n/a
 
      06-29-2005
Infact for me I got some surprising results.
Following is the piece of code which I compiled using gcc :-

#include <stdio.h>
#include <stdlib.h>

int main(int argc,char** argv)
{

int iv = atoi(argv[1]);
double dv = atof(argv[1]);
printf("%f %d \n",dv,iv);
return 0;
}

I found that when I give a.out a it gives me 0.000000 and 0
If i give a.out 3 it gives me
3.000000 and 3
This is certainly an unpredictable behaviour

 
Reply With Quote
 
Clark S. Cox III
Guest
Posts: n/a
 
      06-29-2005
On 2005-06-29 06:04:35 -0400, "Rajan" <> said:

> Infact for me I got some surprising results.
> Following is the piece of code which I compiled using gcc :-
>
> #include <stdio.h>
> #include <stdlib.h>
>
> int main(int argc,char** argv)
> {
>
> int iv = atoi(argv[1]);
> double dv = atof(argv[1]);
> printf("%f %d \n",dv,iv);
> return 0;
> }
>
> I found that when I give a.out a it gives me 0.000000 and 0
> If i give a.out 3 it gives me
> 3.000000 and 3
> This is certainly an unpredictable behaviour


What's unpredictable about that? Both functions are doing exactly what
they're supposed to do. They are attempting to convert the given string
to an int or double respectively, and when they fail to do so, they are
returning zero.

--
Clark S. Cox, III


 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      06-29-2005
Rajan wrote:
> Infact for me I got some surprising results.
> Following is the piece of code which I compiled using gcc :-
>
> #include <stdio.h>
> #include <stdlib.h>
>
> int main(int argc,char** argv)
> {
>
> int iv = atoi(argv[1]);
> double dv = atof(argv[1]);
> printf("%f %d \n",dv,iv);
> return 0;
> }
>
> I found that when I give a.out a it gives me 0.000000 and 0
> If i give a.out 3 it gives me
> 3.000000 and 3
> This is certainly an unpredictable behaviour


I don't find it unpredictable at all since the last time I checked "a"
was not a valid decimal digit. So atoi and atof return 0 because the
string was not a valid textual representation of a number.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      06-29-2005
Roman Mashak wrote:
> Hello, Jack!
> You wrote on Wed, 29 Jun 2005 01:52:19 -0500:
>
> ??>> Why is compilation and linking of code successful even without
> ??>> including stdlib.h? At least linking error should arise, no ?
>
> JK> Prior to the 1999 update to the C language standard, it was allowed to
> JK> call a function without any sort of declaration or prototype in scope.
> [skip]
>
> Moreover, atoi() and atof() are both defined in stdlib.h but only #include
> <stdio.h> is put in the source code. Why does compilation succeed anyway?


It compiles because, as Jack stated, the C language prior to C 99 ALLOWS
you to call a function without a prototype in scope.

What you might not have realised is stdlib.h does not include the
definition of atoi, it only includes a prototype to tell the compiler
what the interface is. The actual function is defined in the C library
which your system is automatically linking in.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
 
Reply With Quote
 
XZ
Guest
Posts: n/a
 
      06-29-2005
Jack Klein wrote:
> On Wed, 29 Jun 2005 15:11:18 +0900, "Roman Mashak" <>
> wrote in comp.lang.c:
>
>
>>Hello, Jack!
>>You wrote on Wed, 29 Jun 2005 00:51:45 -0500:
>>
>> ??>> Could anyone help me understand what's wrong with the code?
>> ??>>
>> ??>> Thank you for your time,
>> ??>> Steve
>>
>> JK> Your program invokes undefined behavior. You do not have a prototype
>> JK> in scope for atof(). You need to include <stdlib.h>.
>>Why is compilation and linking of code successful even without including
>>stdlib.h? At least linking error should arise, no ?

>
>
> Prior to the 1999 update to the C language standard, it was allowed to
> call a function without any sort of declaration or prototype in scope.
>
> This would be defined behavior and work properly if and only if
> certain conditions were met:
>
> 1. The return type of the function was int (true for atoi(), but NOT
> true for atof()).
>
> 2. The function took a fixed number and type of arguments (that is,
> not like printf() that takes variable arguments.
>
> 3. The arguments were all the types produced by default promotion
> (that is, not char or short or float).
>
> 4. In your call of the function, the arguments you passed were the
> correct number and type (after default promotion).
>
> So when you call atoi(argv[1]), the compiler generates a call to a
> function returning an int and accepting one char * parameter. This is
> the correct type for atoi(), so the call works.
>
> Then you call atof(argv[1]), the compiler generates a call to a
> function returning an int and accepting one char * parameter. Since
> atof() DOES NOT return an int, this is wrong and undefined behavior.
>
> Note that a compiler is not required to do anything at all when you
> generate undefined behavior. Since there is a function with that
> name, it links. Some C compilers use an object file format where they
> can detect a mis-match in return types when linking, others do not.
> There is no requirement in the C standard that they do.
>
> Note that under the 1999 and later versions of the C standard, this
> "implicit declaration of function returning int" has been removed from
> the language. A compiler conforming to the later standard versions
> would be required to issue a diagnostic for this, but that still does
> not prevent it from trying to produce a program.
>
> Finally, every compiler I have ever used has an option to generate
> some sort of message when you call a function without a prototype,
> even prior to C99 where this was not actually a constraint violation.
> You should consult your compiler's documentation to see how to enable
> such warnings.
>



Very clear explanation. Thanks Jack!
I'm actually using gcc 3.3.3. Isn't it supposed to comply with the 1999
standard by default? Or do I have to consult a user's manual even for
compiling such simple code?

Thanks,
Steve
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
PIX 501 and PAT going to wrong host Concerned Citizen Cisco 0 08-26-2005 02:17 PM
Simulation and realworld problem in design - what is wrong? Preben Holm VHDL 6 04-25-2005 04:25 PM
Is XML Doc wrong or is Schema wrong? (or both) Matthew XML 7 01-07-2005 10:05 PM
Whats wrong with .net passport and IE =?Utf-8?B?S2ViaXNob3AzMQ==?= MCSE 2 10-14-2004 01:59 PM
swinging and nudity is WRONG and immoral Steve Young Digital Photography 10 11-10-2003 04:14 PM



Advertisments
 



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 50 51 52 53 54 55 56 57