scanf (and friends) is designed to work with strict formatting. You can get around this with ugly hacks, but for the most part it's better to read an entire line with fgets and then parse it at your leisure. In this case, strtol gives you just what you need (it's less concise though ):
Code:
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
int main ( void )
{
long int a, b;
char op;
char buffer[BUFSIZ];
char *end;
if ( fgets ( buffer, sizeof buffer, stdin ) != NULL ) {
/* Split up the input */
a = strtol ( buffer, &end, 0 );
if ( end == buffer )
return EXIT_FAILURE;
while ( *end != '\0' && isspace ( *end ) )
++end;
op = *end++;
b = strtol ( end, &end, 0 );
if ( end == buffer )
return EXIT_FAILURE;
/* Perform the operation */
if ( op == '+' )
printf ( "%ld\n", a + b );
}
return EXIT_SUCCESS;
}