problem with printf

This is a discussion on problem with printf within the C++ Programming forums, part of the General Programming Boards category; Hi all.. why the below program prints some garbage? Code: void main() { printf("%d",3.34); }...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    8

    problem with printf

    Hi all..

    why the below program prints some garbage?


    Code:
    void main()
    {
        printf("%d",3.34);
    
    }

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Because you are telling in the format string that 3.34 is an integer (which it isn't), so it's bit pattern is interpreted as if it was an integer?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    8
    well..
    in that case even the statement

    Code:
    pritnf("%d",3.00);
    should also print some garbage right? but it doesn't...it prints 0.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    A double might be twice as large as an int. You might be printing the bit pattern of only half of the double that happens to be all zeros?

    And exactly how is 0 not garbage (wrong answer) if it is supposed to print 3.00?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >but it doesn't...it prints 0.
    Undefined behavior isn't restricted in any way. It could print "I'm a little teapot" every time, which also isn't technically garbage but is equally useless.
    My best code is written with the delete key.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by raj_ksrt View Post
    well..
    in that case even the statement

    Code:
    pritnf("%d",3.00);
    should also print some garbage right? but it doesn't...it prints 0.
    In this case, 0 is garbage since you were expecting it to print "3.00".

  7. #7
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    What its printing is the first 32 bits fo the number, which in the case of a double on a little endian machine is the exponent and some bits of the mantissa. 3.34 has a non zero exponent, 3.00 has a zero exponent. Technically yoru compiler shoudl complain about type conversion, which it probably is unless you turned warnigns off. Otherwise, I would say its a nonstandard implimentation of printf() since it should do the conversion itself anyway. The simple solution is to either provide the data in a variable, which will cause type conversion, or use the correct constant type.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  8. #8
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    Code:
    printf("%.2f\n",3.34)
    %f is floating point as is %g

    You need to use the right type per data value. And main() returns an int.
    I'm just trying to be a better person - My Name Is Earl

  9. #9
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Quote Originally Posted by swgh View Post
    Code:
    printf("%.2f\n",3.34)
    %f is floating point as is %g

    You need to use the right type per data value. And main() returns an int.
    void main(void) is syntactically correct. It breaks convention, but its not 'wrong'. You should use int main() though, if for no other reason than to keep people from complaining.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  10. #10
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by abachler View Post
    void main(void) is syntactically correct. It breaks convention, but its not 'wrong'. You should use int main() though, if for no other reason than to keep people from complaining.
    Read Salem's icon. void main() is allowed on many compilers, but it's not standard, so it shouldn't be used: http://faq.cprogramming.com/cgi-bin/...&id=1043284376

  11. #11
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    void main() also doesn't stop the calling environment from receiving an int from main(), even if it's an arbitrary number. The only places I believe alternative versions of main() are allowed by the standards are in environments where the program does not return, however, void main() has never been part of any official C or C++ standard.

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >It breaks convention, but its not 'wrong'.
    That depends on your compiler. If you're writing freestanding code, you can do what you want. If you're writing hosted code and the compiler doesn't support an extension that allows void main, the behavior is undefined. Otherwise it's implementation defined.

    The short answer is that int main is always correct and well defined, while void main is not.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems reading entered race times C
    By loopymoo26 in forum C Programming
    Replies: 12
    Last Post: 05-23-2009, 07:38 AM
  2. Help with a C Programing Quiz project
    By dilemma in forum C Programming
    Replies: 12
    Last Post: 05-15-2009, 03:35 PM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. error in program????
    By SpEkTrE in forum C Programming
    Replies: 5
    Last Post: 11-24-2003, 05:16 PM
  5. Drawing tables in C
    By stanoman in forum C Programming
    Replies: 5
    Last Post: 10-09-2003, 10:14 AM

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