3rd argument is ignored when I use memcpy in C

Viewed 21

I'm a beginner in C and try to use memcpy to send byte in order to transfer data to server. It seems like no matter what I choose for 3rd argument. It's always the same.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
    
    char myChar[100] = "Hello world! ABC is an online sandbox that makes it easy to try out";
    char myChar2[100];
    
    memcpy(myChar2, myChar, 3);
    
    printf("%s", myChar);
    
    return 0;
}

This is the result.

Hello world! ABC is an online sandbox that makes it easy to try out

1 Answers

memcpy is working correctly. The problem with your code is that you are printing myChar instead of myChar2.

printf("%s", myChar2); // Hel

Working example

Related