简体   繁体   中英

Difference between scanf format specifiers

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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM