The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM



View Single Post in UNIX Forums - Click on the Thread or Permalink to View Entire Thread -->
  #1 (permalink)  
Old 04-10-2008
karthikb23 karthikb23 is offline
Registered User
 

Join Date: Nov 2007
Posts: 18
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.
Reply With Quote
Forum Sponsor