How to use [^\n] ?

%s is a format specifier for accepting the string in c language by scanf. But the problem of using this is that it will terminate when we enter a space character as input.So whenever we need to take input string with space character we can not just use %s.To resolve this problem we can use gets() and we can also use %[^\n]s.


The expression scanf("%[^\n]s", string) will take input from the keyboard until and unless we will not press Enter button because as we can see that we are using \n in the above scanf()  which is used for new line. 
if you want that your scanf terminates on entering any character or integer then use that at the place of \n.

Example-
  • scanf("%[^&]s", string)-using this will terminates the scanf on entering & character.
  • scanf("%[^#]s", string)-this will stop taking input when we press # character.
  • and so on......


In this way you can use this in your own way. 

Comments :

Post a Comment