如何使用 scanf() 保存包含多个单词的字符串
How to save a string with multiple words with scanf()
我刚开始用 C 编程,我想知道为什么我不能用 scanf() 存储包含多个单词的字符串。
例如,我输入:“That's an example”,它只存储第一个单词“That's”
我的代码:
int main(void) {
char string[100];
printf("Please enter something: ");
scanf("%s", &string);
printf("You entered: %s", string);
return (0);
}
我认为你有问题scanf();
我建议你从中删除&
。那么你的代码应该是这样的:
int main(void) {
char string[100];
printf("Please enter something: ");
scanf("%s", string);
printf("You entered: %s", string);
return (0);
}
您可以让 scanf()
读取多个带有字符 class 转换说明符的单词:%[^\n]
将在换行符处停止并将其保留在输入流中待处理。请注意,您必须告诉 scanf
要存储到目标数组中的最大字符数,以避免在长输入行上出现未定义的行为。将数组传递给 scanf()
时,不应将其地址作为 &string
传递,而应将 string
作为函数参数传递,因为数组会衰减为指向其第一个元素的指针。
这是修改后的版本:
#include <stdio.h>
int main(void) {
char string[100];
int c;
for (;;) {
printf("Please enter something: ");
/* initialize `string` in case the `scanf()` conversion fails on an empty line */
*string = '[=10=]';
if (scanf("%99[^\n]", string) == EOF)
break;
printf("You entered: %s\n", string);
/* read the next byte (should be the newline) */
c = getchar();
if (c == EOF) /* end of file */
break;
if (c != '\n')
ungetc(c, stdin); /* not a newline: push it back */
}
return 0;
}
但是请注意,使用 fgets()
来完成此任务要简单得多:
#include <stdio.h>
int main(void) {
char string[100];
for (;;) {
printf("Please enter something: ");
if (!fgets(string, sizeof string, stdin))
break;
/* strip the trailing newline, if any */
string[strcspn(string, "\n")] = '[=11=]';
printf("You entered: %s\n", string);
}
return 0;
}
在c语言中,没有字符串这样的数据类型。
字符串存储为字符数组。
而且,变量本身指向数组的第一个元素。因此,无需使用“&”运算符来传递地址。
因此,您只需执行以下操作:
int main(void) {
char string[100];
printf("Please enter something: ");
scanf("%s", string);
printf("You entered: %s", string);
return (0);
}
不要在 scanf 函数中使用“&”。
根据https://man7.org/linux/man-pages/man3/scanf.3.html,%s
将忽略白色-space 字符。要捕获 spaces,您必须使用 %c
和输入参数的额外大小,或使用 %[
格式。检查 scanf
是否会在末尾添加 [=14=]
个字节。
#include <stdio.h>
#define BUFF_SIZE 512
int main(void) {
char string[BUFF_SIZE];
printf("Enter something: ");
fgets(string, BUFF_SIZE, stdin);
printf("You entered: %s", string);
return (0);
}
fgets()
是最好的选择
int main()
{
char string[100];
printf("Please enter something: ");
scanf("%[^\n]%*c",string);
printf("You entered: %s", string);
return 0;
}
我刚开始用 C 编程,我想知道为什么我不能用 scanf() 存储包含多个单词的字符串。
例如,我输入:“That's an example”,它只存储第一个单词“That's”
我的代码:
int main(void) {
char string[100];
printf("Please enter something: ");
scanf("%s", &string);
printf("You entered: %s", string);
return (0);
}
我认为你有问题scanf();
我建议你从中删除&
。那么你的代码应该是这样的:
int main(void) {
char string[100];
printf("Please enter something: ");
scanf("%s", string);
printf("You entered: %s", string);
return (0);
}
您可以让 scanf()
读取多个带有字符 class 转换说明符的单词:%[^\n]
将在换行符处停止并将其保留在输入流中待处理。请注意,您必须告诉 scanf
要存储到目标数组中的最大字符数,以避免在长输入行上出现未定义的行为。将数组传递给 scanf()
时,不应将其地址作为 &string
传递,而应将 string
作为函数参数传递,因为数组会衰减为指向其第一个元素的指针。
这是修改后的版本:
#include <stdio.h>
int main(void) {
char string[100];
int c;
for (;;) {
printf("Please enter something: ");
/* initialize `string` in case the `scanf()` conversion fails on an empty line */
*string = '[=10=]';
if (scanf("%99[^\n]", string) == EOF)
break;
printf("You entered: %s\n", string);
/* read the next byte (should be the newline) */
c = getchar();
if (c == EOF) /* end of file */
break;
if (c != '\n')
ungetc(c, stdin); /* not a newline: push it back */
}
return 0;
}
但是请注意,使用 fgets()
来完成此任务要简单得多:
#include <stdio.h>
int main(void) {
char string[100];
for (;;) {
printf("Please enter something: ");
if (!fgets(string, sizeof string, stdin))
break;
/* strip the trailing newline, if any */
string[strcspn(string, "\n")] = '[=11=]';
printf("You entered: %s\n", string);
}
return 0;
}
在c语言中,没有字符串这样的数据类型。 字符串存储为字符数组。
而且,变量本身指向数组的第一个元素。因此,无需使用“&”运算符来传递地址。
因此,您只需执行以下操作:
int main(void) {
char string[100];
printf("Please enter something: ");
scanf("%s", string);
printf("You entered: %s", string);
return (0);
}
不要在 scanf 函数中使用“&”。
根据https://man7.org/linux/man-pages/man3/scanf.3.html,%s
将忽略白色-space 字符。要捕获 spaces,您必须使用 %c
和输入参数的额外大小,或使用 %[
格式。检查 scanf
是否会在末尾添加 [=14=]
个字节。
#include <stdio.h>
#define BUFF_SIZE 512
int main(void) {
char string[BUFF_SIZE];
printf("Enter something: ");
fgets(string, BUFF_SIZE, stdin);
printf("You entered: %s", string);
return (0);
}
fgets()
是最好的选择
int main()
{
char string[100];
printf("Please enter something: ");
scanf("%[^\n]%*c",string);
printf("You entered: %s", string);
return 0;
}