From: pete on 19 May 2010 02:57 amelia jane wrote: > > On May 17, 8:22 pm, pete <pfil...(a)mindspring.com> wrote: > > amelia jane wrote: > > > > > Hello folks, > > > > > I have the following pseudo code: > > > > > #define MY_DEF 10 > > > void my_func(void) > > > { > > > > > int i, loopnumber = 0; loopcount = 0, offset = 6; > > > > > loopnumber = (MY_DEF-offset)/2; > > > for (i = 0; i< loopnumber; i++) > > > { > > > loopcount++; > > > } > > > > > printf("loopcount=%d\n",loopcount); > > > } > > > > > This code prints out loopcount=2. > > > > > I am a bit confused on why this is the case. > > > Any thoughts on this are > > > welcome. > > > > loopnumber is assigned a value of two. > > The loop executes twice. > > loopcount starts at zero and increments twice. > Thanks for the responses so far. For the benefit of everyone, here is > the original piece of code in C: > > #define MY_DEF 10 > > void my_func(void) > { > int i; > int loopcount; > int offset = 6; > loopcount = 0; > > for (i = 0; i< ((MY_DEF-offset)/2); i++) > { > loopcount++; > } > > printf("loopcount=%d\n",loopcount); > } > > main() > { > my_func(); > } > > I still don't get why loopcount equals 1; It doesn't. When I run that code, the output is: loopcount=2 -- pete
From: Dann Corbit on 19 May 2010 03:21 In article <4BF38BF1.611E(a)mindspring.com>, pfiland(a)mindspring.com says... > > It doesn't. > When I run that code, the output is: > > loopcount=2 It could conceivably be anything. A varadic function (printf) is used with no prototype in scope, and so undefined behavior is created. If this program also does not produce 2 on the O.P.'s machine, then I am at a loss for any possible explanation: #include <stdio.h> #define MY_DEF 10 void my_func(void) { int i; int loopcount; const int offset = 6; loopcount = 0; for (i = 0; i< ((MY_DEF-offset)/2); i++) { loopcount++; } printf("loopcount=%d\n",loopcount); } int main(void) { my_func(); return 0; }
From: bart.c on 19 May 2010 05:56 amelia jane wrote: > Thanks for the responses so far. For the benefit of everyone, here is > the original piece of code in C: > > #define MY_DEF 10 > > void my_func(void) > { > int i; > int loopcount; > int offset = 6; > loopcount = 0; > > for (i = 0; i< ((MY_DEF-offset)/2); i++) > { > loopcount++; > } > > printf("loopcount=%d\n",loopcount); > } > > main() > { > my_func(); > } > > I still don't get why loopcount equals 1; Put a printf call within the loop body (printing a message, and perhaps the value of i, loopcount and the complete MY_DEF expression); then you can see how many times the loop is executed. And maybe print the value of i after the loop too; it should be 2. And, is it really the original code? I think a superfluous semicolon at the end of the 'for' line would give loopcount=1. -- Bartc
From: Patricia Shanahan on 19 May 2010 06:52 amelia jane wrote: > On May 17, 8:22 pm, pete <pfil...(a)mindspring.com> wrote: >> amelia jane wrote: >> >>> Hello folks, >>> I have the following pseudo code: >>> #define MY_DEF 10 >>> void my_func(void) >>> { >>> int i, loopnumber = 0; loopcount = 0, offset = 6; >>> loopnumber = (MY_DEF-offset)/2; >>> for (i = 0; i< loopnumber; i++) >>> { >>> loopcount++; >>> } >>> printf("loopcount=%d\n",loopcount); >>> } >>> This code prints out loopcount=2. >>> I am a bit confused on why this is the case. Any thoughts on this are >>> welcome. >> loopnumber is assigned a value of two. >> The loop executes twice. >> loopcount starts at zero and increments twice. >> >> -- >> pete > > Thanks for the responses so far. For the benefit of everyone, here is > the original piece of code in C: > > #define MY_DEF 10 > > void my_func(void) > { > int i; > int loopcount; > int offset = 6; > loopcount = 0; > > for (i = 0; i< ((MY_DEF-offset)/2); i++) > { > loopcount++; > } > > printf("loopcount=%d\n",loopcount); > } > > main() > { > my_func(); > } > > I still don't get why loopcount equals 1; I compiled the code with gcc and ran it, result "loopcount=2". Are you sure this is exactly (copy and paste, no retyping) the code for which you get loopcount=1? As Bartc suggested, the following program does get "loopcount=1": #define MY_DEF 10 void my_func(void) { int i; int loopcount; int offset = 6; loopcount = 0; for (i = 0; i< ((MY_DEF-offset)/2); i++); { loopcount++; } printf("loopcount=%d\n",loopcount); } main() { my_func(); } Patricia
From: stan on 19 May 2010 13:01 Patricia Shanahan wrote: > amelia jane wrote: >> On May 17, 8:22 pm, pete <pfil...(a)mindspring.com> wrote: >>> amelia jane wrote: <snip> >> Thanks for the responses so far. For the benefit of everyone, here is >> the original piece of code in C: >> >> #define MY_DEF 10 >> >> void my_func(void) >> { >> int i; >> int loopcount; >> int offset = 6; >> loopcount = 0; >> >> for (i = 0; i< ((MY_DEF-offset)/2); i++) >> { >> loopcount++; >> } >> >> printf("loopcount=%d\n",loopcount); >> } >> >> main() >> { >> my_func(); >> } >> >> I still don't get why loopcount equals 1; > > I compiled the code with gcc and ran it, result "loopcount=2". Are you > sure this is exactly (copy and paste, no retyping) the code for which > you get loopcount=1? As Bartc suggested, the following program does get > "loopcount=1": As mentioned, after adding the stdio.h, I get the expected result. I agree with the others as to possible causes, but I might add that this may be a perfect time to learn to step through code in a debugger. You might discover the problem with your code and you will certainly learn a skill which can be very handy. It won't be time wasted. Debuggers are not a silver bullet, they can't solve every problem all the time. But when you fine yourself stumped you can never have too many tools available. Just a thought.
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 Prev: IDE for C with good context sensitive help Next: KOBWEB JAVA |