It depends on the compiler and the target architecture.
There may be very simple target architectures, such as microcontrollers, that do not have instructions for supporting copying memory blocks. There are probably very simple compilers for training that generate byte copy bytes even on architectures that support more efficient methods.
However, you can assume that compilers at the production level are a reasonable thing and create the fastest code for the most popular architectures in this case, and you do not need to worry about that.
However, the best way to check is to read the assembly by the compiler generates.
Take this test code (stack_array_init.c):
#include <stdio.h> int main() { char a[]="Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed\n" "do eiusmod tempor incididunt ut labore et dolore magna aliqua.\n"; printf("%s", a); return 0; }
And compile it into an assembly with gcc with optimization for size (to have less read), for example:
gcc -Os -S stack_array_init.c
Here is the result for x86-64:
.file "stack_array_init.c" .section .rodata.str1.1,"aMS",@progbits,1 .LC1: .string "%s" .LC0: .string "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed\ndo eiusmod tempor incididunt ut labore et dolore magna aliqua.\n" .section .text.startup,"ax",@progbits .globl main .type main, @function main: .LFB0: .cfi_startproc subq $136, %rsp .cfi_def_cfa_offset 144 movl $.LC0, %esi movl $126, %ecx leaq 2(%rsp), %rdi xorl %eax, %eax rep movsb leaq 2(%rsp), %rsi movl $.LC1, %edi call printf xorl %eax, %eax addq $136, %rsp .cfi_def_cfa_offset 8 ret .cfi_endproc .LFE0: .size main, .-main .ident "GCC: (Debian 4.7.2-5) 4.7.2" .section .note.GNU-stack,"",@progbits
Here "rep movsb" is a command that copies a line onto the stack.
Here is an excerpt from the ARMv4 build (which might be easier to read):
main: @ Function supports interworking. @ args = 0, pretend = 0, frame = 128 @ frame_needed = 0, uses_anonymous_args = 0 str lr, [sp, #-4]! sub sp, sp, #132 mov r2, #126 ldr r1, .L2 mov r0, sp bl memcpy mov r1, sp ldr r0, .L2+4 bl printf mov r0, #0 add sp, sp, #132 ldr lr, [sp], #4 bx lr .L3: .align 2 .L2: .word .LC0 .word .LC1 .size main, .-main .section .rodata.str1.4,"aMS",%progbits,1 .align 2 .LC1: .ascii "%s\000" .space 1 .LC0: .ascii "Lorem ipsum dolor sit amet, consectetur adipisicing" .ascii " elit, sed\012do eiusmod tempor incididunt ut labor" .ascii "e et dolore magna aliqua.\012\000" .ident "GCC: (Debian 4.6.3-14) 4.6.3" .section .note.GNU-stack,"",%progbits
In my understanding of the ARM assembly, it looks like a memcpy call code to copy a string into a stack array. Although this does not show the assembly for memcpy, I would expect it to use one of the fastest methods available.