How can multiple string variables be declared in the C language?

In C language, you can use character arrays to define multiple string variables. Each string variable is a character array, and you can define multiple string variables by defining different character arrays.

Here is an example code:

#include <stdio.h>

int main() {
    char str1[] = "Hello";
    char str2[] = "World";
    char str3[] = "C Programming";

    // 输出字符串变量的值
    printf("str1: %s\n", str1);
    printf("str2: %s\n", str2);
    printf("str3: %s\n", str3);

    return 0;
}

In the code above, three string variables, str1, str2, and str3, are defined to store different strings. These string variables’ values can be outputted using the printf function.

Note that the length of the character array will be automatically determined based on the length of the string, so there is no need to specify a specific length. If you need to modify the value of the string, you can directly assign values to the character array.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds