weird problem in constant definition

This is a discussion on weird problem in constant definition within the C Programming forums, part of the General Programming Boards category; Hello all! My first post here. I am investigating constants and I am seeing weird effect in defining constants. Here ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    4

    weird problem in constant definition

    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;	
    }
    Code:
    CONST: 0.000000
    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.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    5/9 should always give you 0. Perhaps what is going on in the first instance is that you are passing only an int (0 is an int, and so is 5/9) but printf is expecting a double, so it needs to get four bytes of information from somewhere, so it's probably reading the old information from the previous call that is still left on the stack.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    4

    Thumbs up

    Yeah, that is it!

    If I used this:
    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);
    	printf("      NULL: %f\n",0);
    	printf("  HARDCODE: %f\n",0.007);
    	printf("     CONST: %f\n",CONST);
    	printf("      TEXT: %s\n","AAaaAAaa");
    	printf("     CONST: %f\n",CONST);
    	printf("  HARDCODE: %f\n",20007.1);
    	printf("     CONST: %f\n",CONST);
    	
    		
    
    	
    	printf("%x\n",test_const);
    	printf("%x\n",CONST);
    
    	return 0;	
    }
    i get
    Code:
    test_const: 0.555556
         CONST: 0.555555
          NULL: 0.555555
      HARDCODE: 0.007000
         CONST: 0.007000
          TEXT: AAaaAAaa
         CONST: 0.007000
      HARDCODE: 20007.100000
         CONST: 20007.093750
    71c71c72
    0
    So obviousely printf gets trash as you mentioned. Thank you for replay, it cleared my mind as fresh water

    Obviousely printf gets CONST (replaced by 0 in pre compile) witch is hard coded and taking float data from code previousely used... debug would probably clear it up correctly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. weird CString problem
    By axr0284 in forum Windows Programming
    Replies: 1
    Last Post: 03-18-2006, 04:05 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  3. Replies: 6
    Last Post: 05-12-2005, 04:39 AM
  4. Errors
    By Rhidian in forum C Programming
    Replies: 10
    Last Post: 04-04-2005, 01:22 PM
  5. Weird problem
    By khpuce in forum Tech Board
    Replies: 0
    Last Post: 04-29-2004, 09:29 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21