Why the variablle's value is so weird

Discussion in 'C Programming' started by yezi, Feb 9, 2006.

  1. yezi

    yezi Guest

    HI:

    The code I write is :

    static int numTrial;
    static float perCentage;
    static int gapValue;
    static int inValue;


    for( gapValue = inValue; gapValue <= MAX_GAP; gapValue = (gapValue +
    GAP_STEP)){

    for (perCentage = 0.0; perCentage <= 1.0;perCentage = (perCentage
    +0.1)){

    for (numTrial = 0; numTrial < 5 ; numTrial ++){

    Send packet sequence to the End Node;

    printf("gap %d, percentage %d,Numtrial
    %d\n",gapValue,perCentage,numTrial);
    }
    }
    }

    Suppose the percentage should be 0.0. 1.0, 2.0 . however the value is
    0.0 , -1610612736
    The numTrial should be 0, 1,2,3,4,, however the value is 0,1069128089.

    Why, is my declariation sth wrong, if I delete the "static", same thing
    happens.

    Thanks for any comments.
     
    yezi, Feb 9, 2006
    #1
    1. Advertising

  2. yezi

    Michael Mair Guest

    yezi wrote:
    > HI:
    >
    > The code I write is :
    >
    > static int numTrial;
    > static float perCentage;
    > static int gapValue;
    > static int inValue;
    >
    >
    > for( gapValue = inValue; gapValue <= MAX_GAP; gapValue = (gapValue +
    > GAP_STEP)){
    >
    > for (perCentage = 0.0; perCentage <= 1.0;perCentage = (perCentage
    > +0.1)){
    >
    > for (numTrial = 0; numTrial < 5 ; numTrial ++){
    >
    > Send packet sequence to the End Node;
    >
    > printf("gap %d, percentage %d,Numtrial
    > %d\n",gapValue,perCentage,numTrial);
    > }
    > }
    > }


    This code is not valid, compiling C.
    Provide a minimal example.

    > Suppose the percentage should be 0.0. 1.0, 2.0 . however the value is
    > 0.0 , -1610612736
    > The numTrial should be 0, 1,2,3,4,, however the value is 0,1069128089.


    Read
    http://c-faq.com/fp/index.html
    If there are still questions afterwards, feel free to ask.


    > Why, is my declariation sth wrong, if I delete the "static", same thing
    > happens.


    I would be puzzled if it did change the behaviour drastically.
    Reread the parts about "static" in your textbook.

    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
     
    Michael Mair, Feb 9, 2006
    #2
    1. Advertising

  3. On 9 Feb 2006 15:33:49 -0800, in comp.lang.c , "yezi"
    <> wrote:

    >static float perCentage;


    > printf("gap %d, percentage %d,Numtrial %d\n",gapValue,perCentage,numTrial);


    perCentage is a float, you're trying to print it with %d.

    Please read your C book to learn about the format specifiers for
    printf.

    Mark McIntyre
    --
    "Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are,
    by definition, not smart enough to debug it."
    --Brian Kernighan

    ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
     
    Mark McIntyre, Feb 9, 2006
    #3
  4. Mark McIntyre wrote:
    > On 9 Feb 2006 15:33:49 -0800, in comp.lang.c , "yezi"
    > <> wrote:
    >
    >
    >>static float perCentage;

    >
    >
    >> printf("gap %d, percentage %d,Numtrial %d\n",gapValue,perCentage,numTrial);

    >
    >
    > perCentage is a float, you're trying to print it with %d.


    ....and "percentage" is one word (no need for camel casing).


    August

    --
    I am the "ILOVEGNU" signature virus. Just copy me to your
    signature. This email was infected under the terms of the GNU
    General Public License.
     
    August Karlstrom, Feb 10, 2006
    #4
  5. yezi wrote:
    [...]
    > static int numTrial;
    > static float perCentage;
    > static int gapValue;

    [...]
    > printf("gap %d, percentage %d,Numtrial %d\n",gapValue,perCentage,
    > numTrial);

    [...]

    You lied to printf. You told it to expect an int as the second value,
    and then gave it perCentage, which is a float.

    --
    +-------------------------+--------------------+-----------------------------+
    | Kenneth J. Brody | www.hvcomputer.com | |
    | kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------------+
    Don't e-mail me at: <mailto:>
     
    Kenneth Brody, Feb 10, 2006
    #5
  6. yezi

    Randy Howard Guest

    August Karlstrom wrote
    (in article <ilSGf.44264$>):

    > Mark McIntyre wrote:
    >> On 9 Feb 2006 15:33:49 -0800, in comp.lang.c , "yezi"
    >> <> wrote:
    >>
    >>
    >>> static float perCentage;

    >>
    >>
    >>> printf("gap %d, percentage %d,Numtrial %d\n",gapValue,perCentage,numTrial);

    >>
    >>
    >> perCentage is a float, you're trying to print it with %d.

    >
    > ...and "percentage" is one word (no need for camel casing).


    Hmmm. I had to go look up "Camel casing", having never heard it
    described as that before. Informative wiki article. Now I know
    why I haven't head it. It was made up only 11 years ago, or
    "yesterday". :)

    I have heard it referred to as medial capitals though.



    --
    Randy Howard (2reply remove FOOBAR)
    "The power of accurate observation is called cynicism by those
    who have not got it." - George Bernard Shaw
     
    Randy Howard, Feb 11, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    999
    Mark Rae
    Dec 21, 2006
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,271
    Smokey Grindel
    Dec 2, 2006
  3. dorayme
    Replies:
    1
    Views:
    657
    richard
    Jan 21, 2011
  4. richard
    Replies:
    0
    Views:
    617
    richard
    Jan 21, 2011
  5. richard
    Replies:
    0
    Views:
    663
    richard
    Jan 21, 2011
Loading...

Share This Page