-4

I read this in a strcpy funciton.

while (*dst++ = *src++)
    ;

I'm not really sure the execute order. who can help me?

protream
  • 29
  • 1
  • 4
  • Operator precedence says `postfix > unary > assignment`, and finally the `iteration-statement` will evaluate the `null-statement` every time its controlling expression evaluates as non-zero. – EOF Apr 01 '16 at 03:00

1 Answers1

1

The postfix ++ operator increments the value of a variable after the statement it's in has been executed. According to C's precedence rules, the expression will be evaluated something like this:

while (*(dst++) = *(src++));

Which will basically:

  1. Set the character dst points to to the character src points to.
  2. Increment both dst and src
  3. If the character was '\0', end the loop.
  4. Otherwise, repeat.
Functino
  • 1,939
  • 17
  • 25
  • So, '\0' is not be copied, right? – protream Apr 01 '16 at 03:13
  • Since the loop copes the character before it checks the value, yes, the `'\0'` will be copied. Also, if I answered your question, make sure to accept it. – Functino Apr 01 '16 at 03:15
  • @protream "right?" Obviously not right according to this description. Step 1 copies the char. Step 3 checks whether it is '\0'. – Jim Balter Apr 01 '16 at 05:57