Reputation: 31
What is the difference between the following two scanf() functions:
scanf(" %79[^\n]\n",name);
and
scanf(" %79[^\n]s\n",name);
I am new to C so forgive me if someone thinks this is a rookie question. I did all the research I could and I can't tell the difference between the above two.
In what ways are the functioning of the above two different from one another? The first one will take all the characters (until 79 characters) from the stdin till it encounters a '\n' character. So wont the second one work the same way? I am having this doubt because in a program the first one was working properly but the second one failed to input the string properly.
Apart from the main question above just for clarification I think that the '\n' at the end of the scanf functions will remove the '\n' character from the buffer entered during the input of the string. Am I right or wrong?
Upvotes: 2
Views: 221
Reputation: 87441
scanf(" %79[^\n]\n",name);
This matches zero or more whitespace characters, at most 79 non-newline characters, and a newline, and saves the non-newline characters to name
.
scanf(" %79[^\n]s\n",name);
This matches zero or more whitespace characters, at most 79 non-newline characters, the character s
and a newline, and saves the non-newline characters to name
.
Upvotes: 3