By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,165 Members | 850 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,165 IT Pros & Developers. It's quick & easy.

Why the variablle's value is so weird

P: n/a
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.

Feb 9 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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.
Feb 10 '06 #2

P: n/a
On 9 Feb 2006 15:33:49 -0800, in comp.lang.c , "yezi"
<ye*****@hotmail.com> 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 =----
Feb 10 '06 #3

P: n/a
Mark McIntyre wrote:
On 9 Feb 2006 15:33:49 -0800, in comp.lang.c , "yezi"
<ye*****@hotmail.com> 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.
Feb 10 '06 #4

P: n/a
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:Th*************@gmail.com>

Feb 10 '06 #5

P: n/a
August Karlstrom wrote
(in article <il*******************@newsb.telia.net>):
Mark McIntyre wrote:
On 9 Feb 2006 15:33:49 -0800, in comp.lang.c , "yezi"
<ye*****@hotmail.com> 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

Feb 11 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.