可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
This question already has an answer here:
-
How do you allow spaces to be entered using scanf?
12 answers
I\'m using Ubuntu and I\'m also using Geany and CodeBlock as my IDE.
What I\'m trying to do is reading a string (like \"Barack Obama\"
) and put it in a variable:
#include <stdio.h>
int main(void)
{
char name[100];
printf(\"Enter your name: \");
scanf(\"%s\", name);
printf(\"Your Name is: %s\", name);
return 0;
}
Output:
Enter your name: Barack Obama
Your Name is: Barack
How can I make the program read the whole name?
回答1:
Use:
fgets (name, 100, stdin);
100
is the max length of the buffer. You should adjust it as per your need.
Use:
scanf (\"%[^\\n]%*c\", name);
The []
is the scanset character. [^\\n]
tells that while the input is not a newline (\'\\n\'
) take input. Then with the %*c
it reads the newline character from the input buffer (which is not read), and the *
indicates that this read in input is discarded (assignment suppression), as you do not need it, and this newline in the buffer does not create any problem for next inputs that you might take.
Read here about the scanset and the assignment suppression operators.
Note you can also use gets
but ....
Never use gets()
. Because it is impossible to tell without knowing the data in advance how many characters gets() will read, and because gets()
will continue to store characters past the end of the buffer, it is extremely dangerous to use. It has been used to break computer security. Use fgets()
instead.
回答2:
Try this:
scanf(\"%[^\\n]s\",name);
\\n
just sets the delimiter for the scanned string.
回答3:
Here is an example of how you can get input containing spaces by using the fgets
function.
#include <stdio.h>
int main()
{
char name[100];
printf(\"Enter your name: \");
fgets(name, 100, stdin);
printf(\"Your Name is: %s\", name);
return 0;
}
回答4:
scanf(\" %[^\\t\\n]s\",&str);
str
is the variable in which you are getting the string from.
回答5:
NOTE: When using fgets(), the last character in the array will be \'\\n\' at times when you use fgets() for small inputs in CLI (command line interpreter) , as you end the string with \'Enter\'. So when you print the string the compiler will always go to the next line when printing the string. If you want the input string to have null terminated string like behavior, use this simple hack.
#include<stdio.h>
int main()
{
int i,size;
char a[100];
fgets(a,100,stdin);;
size = strlen(a);
a[size-1]=\'\\0\';
return 0;
}
Update: Updated with help from other users.
回答6:
#include<stdio.h>
int main()
{
char name[100];
printf(\"Enter your name: \");
scanf(\"%[^\\n]s\",name);
printf(\"Your Name is: %s\",name);
return 0;
}
回答7:
Using this code you can take input till pressing enter of your keyboard.
char ch[100];
int i;
for (i = 0; ch[i] != \'\\n\'; i++)
{
scanf(\"%c \", &ch[i]);
}
回答8:
#include <stdio.h>
// read a line into str, return length
int read_line(char str[]) {
int c, i=0;
c = getchar();
while (c != \'\\n\' && c != EOF) {
str[i] = c;
c = getchar();
i++;
}
str[i] = \'\\0\';
return i;
}
回答9:
If you need to read more than one line, need to clear buffer. Example:
int n;
scanf(\"%d\", &n);
char str[1001];
char temp;
scanf(\"%c\",&temp); // temp statement to clear buffer
scanf(\"%[^\\n]\",str);
回答10:
\"Barack Obama\" has a space between \'Barack\' and \'Obama\'. To accommodate that, use this code;
#include <stdio.h>
int main()
{
printf(\"Enter your name\\n\");
char a[80];
gets(a);
printf(\"Your name is %s\\n\", a);
return 0;
}
回答11:
While the above mentioned methods do work, but each one has it\'s own kind of problems.
You can use getline()
or getdelim()
, if you are using posix supported platform.
If you are using windows and minigw as your compiler, then it should be available.
getline()
is defined as :
ssize_t getline(char **lineptr, size_t *n, FILE *stream);
In order to take input, first you need to create a pointer to char type.
#include <stdio.h>
#include<stdlib.h>
// s is a pointer to char type.
char *s;
// size is of size_t type, this number varies based on your guess of
// how long the input is, even if the number is small, it isn\'t going
// to be a problem
size_t size = 10;
int main(){
// allocate s with the necessary memory needed, +1 is added
// as its input also contains, /n character at the end.
s = (char *)malloc(size+1);
getline(&s,&size,stdin);
printf(\"%s\",s);
return 0;
}
Sample Input:Hello world to the world!
Output:Hello world to the world!\\n
One thing to notice here is, even though allocated memory for s is 11 bytes,
where as input size is 26 bytes, getline reallocates s
using realloc()
.
So it doesn\'t matter how long your input is.
size
is updated with no.of bytes read, as per above sample input size
will be 27
.
getline()
also considers \\n
as input.So your \'s\' will hold \'\\n\' at the end.
There is also more generic version of getline()
, which is getdelim()
, which takes one more extra argument, that is delimiter
.
getdelim()
is defined as:
ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream);
Linux man page
回答12:
\"%s\"
will read the input until whitespace is reached.
gets might be a good place to start if you want to read a line (i.e. all characters including whitespace until a newline character is reached).
回答13:
The correct answer is this:
#include <stdio.h>
int main(void)
{
char name[100];
printf(\"Enter your name: \");
// pay attention to the space in front of the %
//that do all the trick
scanf(\" %[^\\n]s\", name);
printf(\"Your Name is: %s\", name);
return 0;
}
That space in front of % is very important, because if you have in your program another few scanf let\'s say you have 1 scanf of an integer value and another scanf with a double value... when you reach the scanf for your char (string name) that command will be skipped and you can\'t enter value for it... but if you put that space in front of % will be ok everything and not skip nothing.
回答14:
scanf(\"%s\",name);
use &
with scanf
input