Help with printf

This is a discussion on Help with printf within the C Programming forums, part of the General Programming Boards category; Hi all I'm having some problems with this code: Code: struct Note{ float duration; float ampvalue; float pitchvalue; }; int ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    18

    Help with printf

    Hi all
    I'm having some problems with this code:
    Code:
    struct Note{
           float duration;
           float ampvalue;
           float pitchvalue;
    };
    
    int main()
    {
           struct Note *gnote = malloc(sizeof(struct Note));
           noteprepare(gnote, 2);    /*fill *gnote with some data*/
           
           printf("%f   %f   %f\n\n", gnote->duration, gnote->ampvalue, gnote->pitchvalue);
           printf("%f   %d   %f\n\n", gnote->duration, gnote->ampvalue, gnote->pitchvalue);
           return 0;
    }
    The only output I care about of course is from the printf() with all floats, but I can't figure out why I get weird input from the second printf().
    Here is an example of what I get:
    Code:
    0.250000   2000.000000   587.299988
    0.250000   0   26815622315368532000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000000000000000000000000000000000
    0000000000.000000.
    Or:
    Code:
    0.000000   2300.000000   659.260010
    0.000000   0   -0.000000
    The first line of output is exactly what I want, but I don't understand what's going on with the second.
    Thanks for your help
    PS I'll pose noteprepare() if anyone thinks it will be helpful
    PPS I'm using WindowsXP with MinGW

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    These are not the same.
    Code:
           printf("%f   %f   %f\n\n", gnote->duration, gnote->ampvalue, gnote->pitchvalue);
           printf("%f   %d   %f\n\n", gnote->duration, gnote->ampvalue, gnote->pitchvalue);
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    I think the OP knows that but is wondering why it prints 0 (for a float passed to %d) every time . . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Location
    China
    Posts
    2
    Just one point - the float's memory format is different from int's!

    E.G. (float)1.0 is verrrrry different from (int)1 in memory.

    If %f, printf() thinks that the gnote->ampvalue is a float; If %d, printf() thinks that the gnote->ampvalue is a int.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. making it portable.....?
    By ShadeS_07 in forum C Programming
    Replies: 11
    Last Post: 12-24-2008, 08:38 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM
  3. segmentation fault upon reload
    By yabud in forum C Programming
    Replies: 8
    Last Post: 12-18-2006, 05:54 AM
  4. Simple C question: user input to repeat a loop
    By evernaut in forum C Programming
    Replies: 2
    Last Post: 11-18-2006, 08:23 AM
  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