Hello all!
My first post here.
I am investigating constants and I am seeing weird effect in defining constants.
Here is the problem.
Code:
#include <stdio.h>
#define CONST 5/9
int main()
{
double test_const = 5.0/9.0;
printf("test_const: %f\n",test_const);
printf(" CONST: %f\n",CONST);
return 0;
}
gives
Code:
test_const: 0.555556
CONST: 0.555555
First question, why the rounding difference, no rounding at all in constant definition?
However, if I comment out first print function I get even weirder result:
Code:
#include <stdio.h>
#define CONST 5/9
int main()
{
double test_const = 5.0/9.0;
//printf("test_const: %f\n",test_const);
printf(" CONST: %f\n",CONST);
return 0;
}
How does the commenting out of one simple print function result such effect?
However, If I use correct type casting in constant definition
Code:
#define CONST (float)5/9
No such problems appears.
However, in linux I get for out commented print function random outputs:
Code:
[root@xxx test]# ./a.out
CONST: -0.510853
[root@xxx test]# ./a.out
CONST: -0.030291
[root@xxx test]# ./a.out
CONST: -0.023501
[root@xxx test]# ./a.out
CONST: -0.051702
Witch gives me good reason to think uninitialized memory area. Again, correct casting solves problem.
But the main question is: how does commenting out first print function affect the output?
On windows I used
Code:
C:\test>gcc --version
gcc (GCC) 3.4.2 (mingw-special)
Copyright (C) 2004 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
and linux (debian)
Code:
[root@xxx test]# gcc --version
gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-44)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.