How to input a string array in C programming language?

In C language, the scanf function can be used to input strings into an array. The specific steps are as follows:

  1. First, define a string array variable to store the input strings.
  2. Can you provide me with the translation for “scanf”?
  3. The given statement.

Here is a sample code:

#include <stdio.h>

#define MAX_LENGTH 100

int main() {
    char str[MAX_LENGTH];

    printf("请输入字符串:");
    scanf("%s", str);

    printf("输入的字符串为:%s\n", str);

    return 0;
}

In the code above, a string array str with a length of MAX_LENGTH is defined to store the input string. Then the scanf function is used to receive the input string and store it in str. Finally, the printf function is used to output the input string.

It is important to note that when using the scanf function to receive strings, the input string cannot contain spaces because scanf defaults to using spaces as a delimiter. If you need to input a string that contains spaces, you can use the fgets function instead of scanf to receive input.

广告
Closing in 10 seconds
bannerAds