From the scanf
manual:
s Matches a sequence of
non-white-space characters; the next
pointer
must be a pointer to char, and the array must be large enough to
accept all the sequence and the terminating NUL character. The
input string stops at white space or at the maximum field width,
whichever occurs first.
You are invoking UB. Try:
#define str(x) #x
#define xstr(s) str(x)
#define BUFSIZE 30
char buffer[ BUFSIZE + 1 ];
scanf("%" xstr(BUFSIZE) "s", buf);
To ignore anything beyond BUFSIZE
characters suppress assignment:
scanf("%" xstr(BUFSIZE) "s%*", buf);
You should also check if the user has entered return/newline and terminate scanf
if he has:
scanf("%" xstr(BUFSIZE) "[^\n]s%[^\n]*", buf);
and it is good practice to check for return values, so:
int rc = scanf("%" xstr(BUFSIZE) "[^\n]s%[^\n]*", buf);
and finally, check if the there's anything left (such as the newline, and consume it):
if (!feof(stdin))
getchar();