Assign String Variable in C – strcpy() Tutorial

In C language, you can use the strcpy() function to assign a string to a variable. For example:

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

int main() {
    char str1[20];
    char str2[] = "Hello, World!";

    strcpy(str1, str2);

    printf("str1: %s\n", str1);

    return 0;
}

In the code above, we first define two character arrays, str1 and str2, then use the strcpy() function to assign the string in str2 to str1, and finally print the value of str1. The output of the run is:

str1: Hello, World!
bannerAds