Why the variablle's value is so weird

Y

yezi

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.
 
M

Michael Mair

yezi said:
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
 
M

Mark McIntyre

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
 
K

Kenneth Brody

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:[email protected]>
 
R

Randy Howard

August Karlstrom wrote
(in article said:
...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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top