Answering a question about order of parameters it struck me that strcpy (and family) are the wrong way round. Copy should be src -> destination.

Is there a historical or architectural reason for the dest,src order in these 'C' functions? Something to do with optimization of the stack on the PDP-8 or something?

  • 1
    Why do you think it should be src->dst? You set regular variables dst = src, it's just doing this for multiple vars/memory locations at a time. – Kevin Nov 8 '11 at 21:01
  • @littleadv: Not necessarily a duplicate; this is about src vs. dest, not buffer vs. size. It's largely arbitrary in both cases, but if there's a rationale it's likely to be different. – Keith Thompson Nov 8 '11 at 21:09
  • 2
    Whatever the rationale, the fact that the order is the same as for the operands of an assignment operator is a good way to remember it. In any case, the C standard library evolved. It's not a model of consistency; don't expect it to be. – Keith Thompson Nov 8 '11 at 21:13
  • 1
    mov eax, 0xffffffffh; is your answer. – Coder Nov 8 '11 at 21:32
  • 1
    @Coder: movl 0xffffffff, %eax. I don't get your point. – FUZxxl Nov 8 '11 at 21:47

Think of it like an assignment operation.

A = B;  //copies the contents of B into A

Same order when using memcpy to copy an array.

memcpy(A, B, sizeof(B));  //copies the contents of B into A

Putting the target as the second argument would be inconsistent with other functions that write things to strings and have the target in the first argument. memset(3) and sprintf(3) come to mind.


I don't think anyone will know for certain the true answer, although many will speculate why, and now we can no longer ask Dennis Ritchie, the definitive answer is probably not possible. (Can anyone provide an evidence based answer?)

However, I do belive you assertion that the order is "wrong" is wrong. It is what it is. On the right in the UK: there is no right or wrong, it's just because it's that way - as long as everyone does it the same way around.

  • There are logical reasons for driving on the left, and historical/political ones for switching to the right, but it's a bit OT for programmers.so – Martin Beckett Jul 14 '12 at 14:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.