Unusual program Output, Help

This is a discussion on Unusual program Output, Help within the C Programming forums, part of the General Programming Boards category; Below is the small program Code: #include<stdio.h> #include<stdlib.h> int main() { printf(" %#f\n", 415); return 0; } Displays OutPut as ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    21

    Arrow Unusual program Output, Help

    Below is the small program
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    int main()
    {   
        printf(" %#f\n", 415);
            
        return 0;
    }
    Displays OutPut as = -0.028148

    Unable to understand, please help.....
    I am using NetBeans 6 under Ubuntu.

  2. #2
    Registered User
    Join Date
    Feb 2008
    Posts
    21
    with each run, it displays a random number

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Where you supply 415 - that's supposed to be a reference to a float variable.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,299
    You're telling it to print a double, but you're not passing it a double.
    Use 415.0 instead.

    Welcome to the complete lack of type safety in printf!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    you put 4 bytes on stack, but using format f - which reads 8 bytes and interprets them as double... So you get 4 random bytes of garbage in your output.
    Make your number to be consistent with the format, for example 415.0
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    69
    Quote Originally Posted by Todd Burch View Post
    Where you supply 415 - that's supposed to be a reference to a float variable.
    I'm not sure what you mean here, but you don't necessarily have to provide a variable in order to be able to leverage the formatting specifiers.

    Example:
    printf("&#37;d\n", 5);

    will print 5 to standard output.

  7. #7
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Quote Originally Posted by cs32 View Post
    I'm not sure what you mean here, but you don't necessarily have to provide a variable in order to be able to leverage the formatting specifiers.
    Rereading it - it is not that clear, but not incorrect. The others said it better.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    69
    Quote Originally Posted by Todd Burch View Post
    Rereading it - it is not that clear, but not incorrect. The others said it better.
    No sweat -- just wanted to clarify.

  9. #9
    Registered User
    Join Date
    Feb 2008
    Posts
    21
    thanks guys,

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program looping with final output
    By hebali in forum C Programming
    Replies: 24
    Last Post: 02-28-2008, 09:58 AM
  2. Program output not right
    By newbiec++ in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2005, 01:49 PM
  3. Replies: 3
    Last Post: 09-05-2005, 08:57 AM
  4. Redirecting program output straight to an edit control
    By bennyandthejets in forum C++ Programming
    Replies: 5
    Last Post: 07-05-2004, 08:25 AM
  5. Program Output
    By lavon in forum C Programming
    Replies: 1
    Last Post: 03-19-2002, 09:32 PM

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