This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
GCC, C dialects, and srand48
- To: help-gcc at gnu dot org
- Subject: GCC, C dialects, and srand48
- From: jbrody2 at umbc dot edu
- Date: Wed, 1 Sep 1999 12:20:33 -0400
- Newsgroups: gnu.gcc.help
- Organization: HCF - Johns Hopkins University, Baltimore, Maryland, USA
- Reply-To: jbrody2 at umbc dot edu
- Xref: wodc7nx0 gnu.gcc.help:800
I've run in to some interesting things using GCC on a couple of
different solaris machines. The first machine is a Solaris 2.5 box
running gcc 2.6.3. The second is a Solaris 2.6 box running gcc
2.8.1. I've written a program which uses srand48 and drand48; I want
to get the prototypes for these by #including stdlib.h. stdlib.h has
these prototypes guarded by the following conditions:
#if defined(__EXTENSIONS__) || \
(__STDC__ == 0 && !defined(_POSIX_C_SOURCE)) || \
(defined(_XOPEN_SOURCE) && (_XOPEN_VERSION - 0 == 4))
The strange behaviour is this: On the 2.6 machine everything is fine;
I include <stdlib.h> and the protype seems to get read. On the other
machine, however, the prototype isn't read. What's even stranger is
that I wrote a test program to see which of the above conditions were
being met on the 2.6 machine: the answer seems to be none! Here's
the program:
#include <stdio.h>
#include <stdlib.h>
int main() {
int i;
#if defined(__EXTENSIONS__)
printf("extensions\n");
#endif
#if defined(_POSIX_C_SOURCE)
printf("posix\n");
#endif
#if defined(_XOPEN_SOURCE)
printf("Xopen version %d\n", _XOPEN_VERSION);
#endif
#if (__STDC__ == 0)
printf("__STDC__ == 0\n");
#endif
#if defined(__STDC__)
printf("STDC = %d\n", __STDC__);
#endif
srand48(23423);
for(i=0; i < 10; i++) printf("%g\n", drand48());
}
Here's a test compile and run from the 2.6 machine:
Script started on Tue Aug 31 08:29:43 1999
sunserver1[2] gcc -Wall test3.c
test3.c: In function `main':
test3.c:29: warning: control reaches end of non-void function
sunserver1[3] a.out
STDC = 1
0.973304
0.372346
0.706783
0.764237
0.855564
0.396148
0.337794
0.69388
0.313063
0.610031
sunserver1[4] exit
exit
script done on Tue Aug 31 08:29:58 1999
And here's the output from the 2.5 machine:
Script started on Tue Aug 31 08:29:41 1999
w[345] gcc -Wall test3.c
test3.c: In function `main':
test3.c:27: warning: implicit declaration of function `srand48'
test3.c:28: warning: implicit declaration of function `drand48'
test3.c:28: warning: double format, different type arg (arg 2)
test3.c:29: warning: control reaches end of non-void function
w[346] a.out
STDC = 1
3.33909e-319
2.12203e-314
4.24402e-314
6.36602e-314
8.48802e-314
1.061e-313
1.2732e-313
1.4854e-313
1.6976e-313
1.9098e-313
w[347] exit
script done on Tue Aug 31 08:29:59 1999
Anyone know what's going on??? I'd also be interested if anyone knows
where the numbers produced by the 2.5 machine are coming from - I know
this is probably undefined behaviour - still curious though...