
@chris your code is extremely dangerous. Theortically your program will never stop execution but again in reality your program is going to crush eventually. You must remember when dealing with C/C++ that its your responsibility to manage the memory on the stack and the heap otherwise you end up with memory leaks, buffer overruns or stack overflows. Like the case in this code. The bounds of a signed int (16bit) are ( -32768 - 32767) so when j crosses the upper bound something has got to give. Seeing your code will cause an infinite loop. REMEMBER C AND C++ DO NOT PROTECT YOU AGAINST OVERWRITTING, ACCESSING OR OVERRUNNING MEMORY, YOU HAVE TO FIGURE ALL THIS OUT YOURSELF AND DECIDE HOW YOU ARE GONNA HANDLE IT. IN THIS CASE MAKE SURE J DOESN'T CROSS THE UPPER BOUND OF int... YOU HAVE BE WARNED!!! Steve Obbayi, Address: P. O. Box 19916 - 00100, Nairobi, Kenya. Tel: +1 202 470 0575 Tel: +254 20 3500525 cell: +254 722 627691 info@sobbayi.com http://www.sobbayi.com :http://www.sceniceastafrica.com :http://blog.andaiconsulting.com/wp -----Original Message----- From: skunkworks-bounces@lists.my.co.ke [mailto:skunkworks-bounces@lists.my.co.ke] On Behalf Of Chris Mwirigi Sent: Monday, June 22, 2009 10:04 PM To: Skunkworks forum Subject: Re: [Skunkworks] C Programming Query true @ billy j is only incrementing and not being evaluated for the loop. try this main() { int i, j; for (i=0, j=1; i<j; i++, j++) printf("%d - %d = %d\n", j, i, j - i); return 0; } On 6/22/09, Billy <billyx5@gmail.com> wrote:
i < 8 is the condition being evaluated so the moment it returns true (when i == 7) thats it. j has no influence on the evaluation of this condition. its same as if you had done
main()
{ int i, j;
for (i=0, j=1; i<8; i++) { printf("%d - %d = %d\n", j, i, j - i); *j++;* } return 0; }
-Billy
2009/3/22 Rahim Kara <rahim@applecentre.co.ke>
It's a basic question and i'm sure alot of you can answer this but i'm still a bit sure. Take a look at the code below.
#include <stdio.h>
main()
{ int i, j;
for (i=0, j=1; i<8; i++, j++)
printf("%d - %d = %d\n", j, i, j - i); return 0; }
Now the results yielded are
1 - 0 = 1 2 - 1 = 1 ... 8 - 7 = 1
i'd like to know what the delimiting factor here is, i.e. considering i've not set a loop limit for j, what's stopping my calculation from being as follows..
148 - 8 = 0
as an example though from my understanding, it should present an infinite loop.
I\ve been pondering this a while, thought i'd get some Guru's out there to help out with my understanding of this.
Rahim.
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks Other services @ http://my.co.ke Other lists ------------- Skunkworks announce: http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks-announce Science - http://lists.my.co.ke/cgi-bin/mailman/listinfo/science kazi - http://lists.my.co.ke/cgi-bin/mailman/admin/kazi/general
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks Other services @ http://my.co.ke Other lists ------------- Skunkworks announce: http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks-announce Science - http://lists.my.co.ke/cgi-bin/mailman/listinfo/science kazi - http://lists.my.co.ke/cgi-bin/mailman/admin/kazi/general