In theory, which is faster, the call to strcpy or the call to memcpy? #include #include int main(){ char msg[12] = "Hello World"; char buffer1[12]; char buffer2[12]; strcpy(buffer1, msg); memcpy(buffer2, msg, sizeof(msg)); return 0; }
-
strcpy
-
memcpy
-
both memcpy and strcpy
-
none
B
Correct answer
Explanation
memcpy is theoretically faster because it knows the exact byte count upfront and can optimize the copy accordingly. strcpy must scan for the null terminator to find the length, then copy character by character until it finds it. In the given code, msg is 12 bytes (including null terminator), so memcpy copies exactly 12 bytes directly. strcpy has to first find where the string ends, then copy. Modern compilers may optimize strcpy calls when the size is known, but theoretically memcpy is faster.