-1
#include <stdio.h>

int main()
{
    int c = 5, no = 10;
    do {
        no /= c;
    } while(c--);

    printf ("%d\n", no);

    return 0;

}

Please help me trace the while loop. I think it stops at one stage, but that is not the case, It executes even when c=0,hence gives runtime error.
Please explain

O'Neil
  • 3,790
  • 4
  • 16
  • 30
Ayush Singh
  • 227
  • 2
  • 10
  • 2
    Do you know what the difference between `c--` and `--c` is? – tkausl Feb 17 '18 at 10:41
  • 1
    Possible duplicate of [Post-increment and Pre-increment concept?](https://stackoverflow.com/questions/4445706/post-increment-and-pre-increment-concept) –  Feb 17 '18 at 10:51
  • When C is 1, the loop will continue after decrementing 1, and integer division by 0 has undefined behaviour. – George Feb 17 '18 at 11:33

1 Answers1

1

change 8th line while(c--) to while(--c) c-- will check the condition and then it will decrease the value of C variable

yash thakor
  • 136
  • 1
  • 1
  • 9