Is it allowed to use scanf(" ")
without additional arguments to ignore initial whitespaces?
I'm using getchar()
to read the chars of a word, and I want to ignore the whitespaces before the word (whitespaces after are used to check the end of the word).
The code is the following, is it correct?
char *read_word() {
int size = 2;
int char_count = 0;
char *s;
char ch;
s = mem_alloc(size);
scanf(" ");
while ((ch = getchar()) != EOF) {
if (char_count >= size) {
s = mem_realloc(s, size++);
}
if (ch == ' ' || ch == '\n') {
s[char_count] = '\0';
break;
}
s[char_count++] = ch;
}
return s;
}