>Idiotic scanf() : Scanning whitespaces in a string

>In C, a few functions are real nasty and scanf() is one of them. It’s specially bad for inputting strings.
What will happen if you give a string:

-> scanf(“%s”, string)

-> Input: “C is stupid”.

Well, you will have only “C” in the “string”.

Reason: It happens because scanf() considers white-spaces as delimiter. So “C” being followed by a white space is considered as the only input. Simple, isn’t it?

Remedy: We have something like follows:

int main()
char arr[10];
scanf(“%*[ \t\n]%s”, arr);


Only way is this:

#define SIZE 128

int main() {
char arr[SIZE];
char ch;
int index = 0;

while(ch != ‘\n’ && index < SIZE)
ch = getchar();
arr[index] = ch;
arr[index] = ”;


Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: