Thread: Unusual program Output, Help

  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
    Chappell Hill, Texas
    Posts
    2,332
    Where you supply 415 - that's supposed to be a reference to a float variable.
    Mainframe assembler programmer by trade. C coder when I can.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    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
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  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
    Chappell Hill, Texas
    Posts
    2,332
    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.
    Mainframe assembler programmer by trade. C coder when I can.

  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, 10: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, 10:32 PM