Q: How can I detect a space or newline at the end of scanf()? 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()?

A: 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.

A: 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'; 


A: You would use fgets instead of scanf

A: 
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
  }
}

