[英]How to print two strings on the same line in c
I want to input a string with spaces and print that string with a another string on the same line. 我想输入一个带空格的字符串,并在同一行上打印另一个字符串。
int i = 4;
double d = 4.0;
char s[] = "Apple ";
int x;
double y;
char z[105];
scanf("%d",&x);
scanf("%lf",&y);
scanf("%[^105\n]s",z);
printf("%d\n",i+x);
printf("%0.1lf\n",d+y);
printf("%s %s",s,z);
return 0;
You scanf
format specifier "%[^105]s"
uses a character class [...]
which is a stand-alone specifier in and of itself and does not requires 's'
at the end. scanf
格式说明符 "%[^105]s"
使用一个字符类 [...]
,它本身就是一个独立的说明符,最后不需要's'
。 By placing 's'
at the end you are forcing scanf
to look for a literal 's'
following an unlimited number of characters NOT including 1, 0, 5
. 通过在末尾放置
's'
,您强制scanf
在无限数量的字符之后查找文字's'
,不包括1, 0, 5
。
It appears you intended to use the number to protect your arrays bounds -- which is a good thing, but the proper format in that case is "%104[^\\n]"
which will read up to 104
characters that do not include a '\\n'
(preserving space for the nul-character ). 您似乎打算使用该数字来保护您的数组边界 - 这是一件好事,但在这种情况下正确的格式是
"%104[^\\n]"
,它将读取多达104
不包含a的字符'\\n'
(保留空字符的空间)。
For example: 例如:
if (scanf("%104[^\n]",z) == 1)
printf("%s %s\n",s,z);
( note: ALWAYS validate ALL user-input by at minimum checking the return) ( 注意:始终通过至少检查返回来验证所有用户输入)
Also note: by NOT reading the '\\n'
above, it is left in your input buffer ( stdin
) unread, and if your next attempted input is "%c"
or "%[...]"
, you will take the '\\n'
as part of your input as nether "%c"
or `"%[...]" consume leading whitespace. 另请注意:通过NOT读取上面的
'\\n'
,它会在您的输入缓冲区( stdin
)中保留未读状态,如果您的下一次尝试输入是"%c"
或"%[...]"
,您将获取'\\n'
作为输入的一部分,因为nether "%c"
或“%[...]”消耗了前导空格。
Putting it together in an example you could do: 将它放在一个示例中,您可以这样做:
#include <stdio.h>
int main (void) {
char s[] = "Apple";
char z[105];
printf ("enter z: ");
if (scanf("%104[^\n]",z) == 1)
printf("%s %s\n",s,z);
else
fputs ("error: stream error or user canceled.\n", stderr);
return 0;
}
( note: instead of scanf
for reading lines, fgets()
is recommended, then simply trim the '\\n'
included in the filled buffer) ( 注意:建议使用
fgets()
而不是scanf
来读取行,然后只需修剪填充缓冲区中包含的'\\n'
)
Example Use/Output 示例使用/输出
$ ./bin/oneline
enter z: is a fruit
Apple is a fruit
Use fgets()
Instead 使用
fgets()
代替
Instead of using scanf
for line input, use a line-oriented input function like fgets()
which will consume an entire line (including the line ending). 不使用
scanf
作为行输入,而是使用面向行的输入函数,如fgets()
,这将消耗整行(包括行结尾)。 The ensures your input buffer is left in a consistent state that does not depend on the previous format specifier user, eg 确保您的输入缓冲区保持一致状态,不依赖于先前的格式说明符用户,例如
...
#include <string.h>
...
printf ("enter z: ");
if (fgets (z, sizeof z, stdin) != NULL) {
z[strcspn (z, "\n")] = 0; /* trim '\n' from end of z */
printf("%s %s\n",s,z);
}
Edit Per-Question in Comment 在评论中编辑每个问题
Your Problem With Your New Code Is scanf("%lf",&y);
您的新代码出现问题是
scanf("%lf",&y);
leaves the '\\n'
in stdin
unread , you then attempt to read scanf("%[^105\\n]",z);
将
stdin
的'\\n'
留下未读 ,然后尝试读取scanf("%[^105\\n]",z);
which reads nothing because you have excluded reading '\\n'
in the inverted character class and you then read stdin
as input where the first character is '\\n'
. 因为你已经排除了在倒置字符类中读取
'\\n'
而没有读取任何内容,然后你将stdin
作为输入读取,其中第一个字符是'\\n'
。 "%[^105\\n]"
means : read an unlimited number of characters and only stop the read if a 1, 0, 5
or '\\n'
character (or EOF
) is encountered. "%[^105\\n]"
是指:读取的字符的数量不受限制,并仅当一个停止读出的1, 0, 5
或'\\n'
字符(或EOF
遇到)。
Taking mixed input with scanf
is full of Pitfalls for new C programmers because of what is left in stdin
, and how leading whitespace is handled depends on the format-specifier used. 由于
stdin
剩下的内容,以及如何处理前导空格取决于所使用的格式说明 符 ,因此使用scanf
混合输入充满了新C程序员的陷阱 。 This is why fgets()
(or POSIX getline()
) are recommended for user input, and then parsing the needed information from the filled buffer with sscanf
. 这就是建议用户输入
fgets()
(或POSIX getline()
),然后使用sscanf
从填充缓冲区解析所需信息的原因。 With a line-oriented input function, the line is completely consumed on each input (given a sufficient buffer size -- don't skimp), eliminating the problems with scanf
. 使用面向行的输入函数,在每个输入上完全消耗该行(给定足够的缓冲区大小 - 不要吝啬),消除了
scanf
的问题。
To make your current code work, you could do: 要使当前代码正常工作,您可以:
#include <stdio.h>
/* simple function to empty remainder of line in stdin */
void empty_stdin (void)
{
int c = getchar();
while (c != '\n' && c != EOF)
c = getchar();
}
int main (void) {
int i = 4, x;
double d = 4.0, y;
char s[] = "Apple ", z[105];
scanf("%d",&x);
scanf("%lf",&y); /* leaves '\n' as next char in stdin */
empty_stdin(); /* empty extraneous characters */
scanf("%104[^\n]",z); /* read up to 104 chars, \n, or EOF */
printf("%d\n",i+x);
printf("%0.1lf\n",d+y);
printf("%s %s\n",s,z);
return 0;
}
( validate each call to scanf
-- that is left to you) ( 验证每次调用
scanf
- 留给你)
Let me know if you have further questions. 如果您有其他问题,请与我们联系。
我在scanf中使用\\ n解决了这个问题( “%lf \\ n ”,&y);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.