I'm writing a program where I have to accept commands from the user like a shell where the user can set values for environment variables. The problem I'm having is if the user types set var var-value
I need to know the user typed a space instead of just set
and pressed enter which is a different command. How can I determine if the user pressed space or enter using scanf()
?

- 1,172
- 1
- 13
- 26
-
Your question isn't clear. The `scanf` function won't even return until the user presses enter. Are you saying the user can enter `set` on one line and the remainder of the command on another? – Carey Gregory Sep 24 '13 at 18:50
-
Doesn't `scanf()` return when it detects a space as well? – Slayter Sep 24 '13 at 18:51
-
So if the user inputs `set var var-value`, it only places `set` into the variable passed to `scanf()` but doesn't return until the end of `var-value` when the user presses enter? – Slayter Sep 24 '13 at 18:54
-
It would depend on the format string you're passing to `scanf`. In general, I don't think `scanf` is the function you want to use here. See [this question](http://stackoverflow.com/questions/421860/c-c-capture-characters-from-standard-input-without-waiting-for-enter-to-be-pr). – Carey Gregory Sep 24 '13 at 18:56
4 Answers
You'll know the user pressed enter because scanf()
won't return until the user does so. If you're trying to read characters in real time as the user types, scanf()
will not work for you. You will need to use the getchar()
, getch()
or getche()
functions, or a function provided by the OS for reading keyboard input. Read the characters one by one into an array, scanning for spaces and the enter key as you read.
See also this question.

- 1
- 1

- 6,836
- 2
- 26
- 47
-
`getchar()` interacts with `stdin` like `scanf()`. Recommend removing it from the list of alternatives. – chux - Reinstate Monica Aug 01 '18 at 15:34
You can check with isspace() function in ctype.h
Another way you can use strchr() to find whether input consists '\n' or space
if(strchr(input,'\n')==NULL && strchr(input,' ')==NULL)
{
//do something
}
EDIT
scanf() reads input till occurrence of any white space character. After white space ignores input.
Use fgets() Instead of scanf()
if you enter input of length less than MAXLENGTH , '\n'
stays before Null character
Replace it with null character
char input[MAXLENGTH+1];
fgets(input,sizeof(input),stdin);
if(input[strlen(input)-1]=='\n')
input[strlen(input)-1]='\0';

- 10,248
- 3
- 31
- 50
-
-
1But `scanf` will drop all whitespace in most cases. You would need to show how to prevent that from happening for your solution to work. – Carey Gregory Sep 24 '13 at 18:52
-
@ngneema and Carey Gregory. added some more info. yes, scanf won't read after space or return key. OP need to use fgets() instead of scanf(). – Gangadhar Sep 24 '13 at 19:20
How can I detect a space or newline at the end of scanf()?
Use %width[^\n]
to read input up to a '\n'
.
char buffer[256];
// v------- Read and discard leading whitespace
int retval = scanf(" %255[^\n]", buffer);
if (retval != 1) {
; // handle error or EOF
}
// Now scan buffer for whatever input format you desire
char ch;
retval = sscanf(buffer, "set%c", &ch);
if ((retval == 1) && (ch == '\n')) {
; // deal with "set"
}
else {
char var[256];
char value[256];
retval = sscanf(buffer, "set %s %s", var, value);
if (retval == 2) {
; // deal with "set var var-value"
}
else {
// other parsings
}
}

- 143,097
- 13
- 135
- 256