0
signed char ch=5;
while(ch = ch--)
    printf("%d",ch);

I read this. It is clearly stated that while statement and end of a statement(;) are sequence points.

So i don't understand why the above one runs infinite time and prints same value[5].

Gibbs
  • 21,904
  • 13
  • 74
  • 138
  • I am ssigning the value. Not checking for equalit – Gibbs Oct 30 '14 at 10:15
  • 1
    change `(ch = ch--)` to `(ch --)` – Sourav Ghosh Oct 30 '14 at 10:15
  • For that link alone, i have posted the details o sequence points – Gibbs Oct 30 '14 at 10:16
  • 4
    `ch = ch--` Is undefined behaviour. In your case, I imagine that `ch--` is evaluated (`ch`'s value is saved, *then* `ch` is decremented) and *then* `ch` is assigned to the saved value (the old one), thus not changing `ch` in the end. – Dettorer Oct 30 '14 at 10:17
  • @Dettorer, yeah i need this explanation. please explain with numbers rather than words. I am confusing little bit. – Gibbs Oct 30 '14 at 10:20
  • Hum... Are you just why it is an infinite loop (answer: undefined behaviour of `ch = ch--`) or are you trying to do something? If so, please detail what you want to do and we'll be happy to help :). – Dettorer Oct 30 '14 at 10:20
  • @Dettorer, Yeah i want to know why it is infinite loop alone – Gibbs Oct 30 '14 at 10:20
  • [Have a look here](http://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points) maybe (first answer defines what is an undefined behaviour). The problem is that your code's behaviour (or "result") is not specified by the C standard, so it can do things completely different (and likely unwanted) with another computer, another OS, another compiler, another surrounding code etc. It simply isn't defined. On your computer with your compiler and this particular piece of code, it does an infinite loop. – Dettorer Oct 30 '14 at 10:24
  • 2
    There is a sequence point at the semicolon of ch=5;. There is a sequence point at the _end_ of the while statement while(...). Between those two sequence points, ch is written to twice, therefore this is UB. See the duplicate link for details. – Lundin Oct 30 '14 at 10:55

2 Answers2

5

Your code should be

signed char ch=5;
while(ch--)
    printf("%d",ch);

as ch-- already is an assignment. You reassigned ch to its previous value before ch-- with ch = ch-- so ch-- has no effect and you get the same value at each iteration.

damienfrancois
  • 52,978
  • 9
  • 96
  • 110
  • Hi, my doubt is that, while(ch=0){} ll fail. right? – Gibbs Oct 30 '14 at 10:17
  • @Rizier123, while(0) isn't false in C? – Gibbs Oct 30 '14 at 10:21
  • wrong, while(0) will be evaluated to false. – specializt Oct 30 '14 at 10:28
  • @specializt Smilarly, at 5th iteration ch ll be 0. isn't? i dont understand the point here.How old value gets preserved? – Gibbs Oct 30 '14 at 11:20
  • 1
    i ... cant decipher your words, sorry. I recommend polishing your english-skills as software development heavily depends on this language. – specializt Oct 30 '14 at 11:40
  • The code given here is correct and the loop is executed 5 times (it prints "43210"). `ch` is decremented each time the `while`'s condition is evaluated. – Dettorer Oct 30 '14 at 12:12
  • @specializt OK fine. According to my assumption, ch gets decremented for each iteration in my CODE. At 5th iteration, ch will be 0. while(ch=0) is false. So it should come out of while loop. But Dettorer said ch-- has no effect in while loop condition[i.e ch=ch--] . I don't understand this. – Gibbs Oct 30 '14 at 12:16
  • With `while(ch--)`, `ch` gets decremented at each iteration. With `while(ch=ch--)` you can't tell what will happen. – Dettorer Oct 30 '14 at 12:22
1

This should work for you:

#include <stdio.h>

int main() {

    signed char ch=5;

    while(ch--)
        printf("%d\n",ch);



    return 0;

}
Rizier123
  • 58,877
  • 16
  • 101
  • 156