How can C language receive a string?

In C language, you can use an array of char type to store a string. The specific steps are as follows:

  1. character
char str[100];
  1. obtain input from the user
  2. scan or read input
  3. %s: Can you rephrase this in English in your own words?
scanf("%s", str);

Be aware that the scanf function will stop reading characters when it encounters a space, tab, or newline, so it can only accept strings without spaces. If you need to receive strings with spaces, you can use the fgets function.

fgets(str, sizeof(str), stdin);
  1. “Could you please rephrase the following natively in English? Thanks.”
printf("输入的字符串是:%s\n", str);

It is important to note that both the scanf function and the fgets function will stop reading characters once they encounter a newline character, leaving the newline character in the input buffer. If you need to use scanf or another input function afterwards, you can use the getchar function to read and discard the newline character from the input buffer.

getchar();  // 读取并丢弃输入缓冲区中的换行符
bannerAds