I came across a piece of code that generates some interesting results when debugging another program.
I created a small program to illustrate this behavior:
#include <stdio.h> int main() { char* word = "foobar"; int i, iterator = 0; for (i = 0; i < 6; i++ && iterator++) printf("%c", word[iterator]); return 0; }
I know this is the wrong way to print a string. This is for demonstration purposes only.
Here I expected the output to be "foobar", obviously, but instead it is "ffooba". It basically reads the first character twice, as if the first time iterator++ executed, nothing happens.
Can anyone explain why this is happening?
source share