0

I hav one doubt regarding post decriment,the code in c is as follows.............

void main()
{
clrscr();
while(i--!=6)
i=i+2;
printf("%d\n",i);
getch();
}

I know the output is 5, but i read in a book dat the post incriment or decrement operators are evaluated after reaching semicolon(;).....but in while condition,the ondition fails so it wont execute the next line so, directly reaches printf.Till now there is no semicolon still the i value is decresed......wts da reason...can u sugest me..?

Thanks & Regards.

3
Contributors
2
Replies
3
Views
11 Years
Discussion Span
Last Post by jim mcnamara
0

If you want help you must to fix your code. i is undefined and uninitialised. There are more problems with your code but first fix and beautify it.

1

Wow.

void main()  - don't use void main. main() returns an int.
{
  clrscr();
  while(i--!=6)      - where is i set to some intitial value?
      i=i+2;           - this is the end of the while loop 
                              I think you are missing {} to make a while block?
   printf("%d\n",i);
   getch();
}

You do realize that your while loop subtracts then adds to the same variable, essentially in the same statement. Eeeek...

Back up.

What are you trying to do?

Votes + Comments
andor agrees
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.