04-10-2008
Interesting implementation: atol, atof etc
Hi,
Check the following code:
#include <stdio.h>
int main()
{
char *s="123.33";
double d=0.0;
d = (double)atof(s);
printf("s is %2$s, d is %1$f\n", d, s);
d = (double)strtod(s, NULL);
printf("s is %3$s, d is %1$.*2$f\n", d, 2, s);
return 0;
}
Output:
s is 123.33, d is 133592.000000
s is 123.33, d is 133592.00
Quite Obvious: I have not included <stdlib.h>
But my code still compiles, because 'libc' is anyways referenced (eg: printf).
=> What are these values returned? This implies there is a 'default' atof which has resolved the symbol reference.
Something interesting now, is i can define atof as a local function: a global symbol and use it as i like.
Now my Question/Problem
But what i was very surprised to see is:
$: nm -A /usr/lib/libc.so.1 | grep atof
/usr/lib/libc.so.1: [3266] | 207888| 12|FUNC |GLOB |0 |9
|atof
'atof' is a symbol with Global visibility in libc.
My questions:
1. Standard C functions should be defined as WEAK symbols, so that a user can override them, like in the above case.
2. However, the resolution in above case has happened due to concept of "interposition" in shared libraries.
Am I correct in concluding this?
Please advice.
4 More Discussions You Might Find Interesting
1. UNIX for Dummies Questions & Answers
first off, i LOVE unix to death, but i really only like the text/console side of it. im not big into the XFree86/Xwindows side of things.. so with that known... im in search of really old *nix's.. like way outdated, floppy OS's from way back in the day. im kinda in search of the pure'r UNIX's.... (0 Replies)
Discussion started by: 01000101
0 Replies
2. Shell Programming and Scripting
Does anyone know how to test if an ethernet interface is alive, or accepting connections? Here is the scenario - I have rsc and sc console interfaces on some Suns. There are some sporadic vulnerability scans that send them out to lunch when they run the scans. I have to login to the host and reset... (0 Replies)
Discussion started by: lm_admin_dh
0 Replies
3. Shell Programming and Scripting
Hello,
So I'm utilizing the bash brace expansion feature to checkout multiple folders from cvs with ease, while excluding certain subfolders within. So I do a command like this:
cvs co trunk/{mod_a,mod_b,mod_c} \!trunk/{mod_a,mod_b,mod_c}/web
to checkout modules trunk/mod_a , trunk/mod_b ,... (1 Reply)
Discussion started by: neked
1 Replies
4. Programming
Hi
I have a small code snippet.
printf("sTaTuS |%s| , |%s| => %f , %f \n",before,after,strtod(before,0),strtod(after,0));
If the variables before and after has values 1000.2234 and 1000.0234 then i get the following output.
sTaTuS |1000.2234| , |1000.0234| => 0.000000 , -0.000000
... (1 Reply)
Discussion started by: kumaran_5555
1 Replies
ATOF(3) BSD Library Functions Manual ATOF(3)
NAME
atof, atof_l -- convert ASCII string to double
LIBRARY
Standard C Library (libc, -lc)
SYNOPSIS
#include <stdlib.h>
double
atof(const char *str);
#include <xlocale.h>
double
atof_l(const char *str, locale_t loc);
DESCRIPTION
The atof() function converts the initial portion of the string pointed to by str to double representation.
It is equivalent to:
strtod(str, (char **)NULL);
The decimal point character is defined in the program's locale (category LC_NUMERIC).
While the atof() function uses the current locale, the atof_l() function may be passed a locale directly. See xlocale(3) for more informa-
tion.
IMPLEMENTATION NOTES
The atof() and atof_l() functions are thread-safe and async-cancel-safe.
The atof() and atof_l() functions have been deprecated by strtod() and strtod_l() and should not be used in new code.
ERRORS
The function atof() need not affect the value of errno on an error.
SEE ALSO
atoi(3), atol(3), strtod(3), strtol(3), strtoul(3), xlocale(3)
STANDARDS
The atof() function conforms to ISO/IEC 9945-1:1990 (``POSIX.1''), ISO/IEC 9899:1990 (``ISO C90''), and ISO/IEC 9899:1999 (``ISO C99'').
BSD
June 4, 1993 BSD