%[^\n] 이게 뭔가요?

mero의 이미지

어떤 소스코드에서

char buf[100];
scanf("%[^\n]", buf);

이런 내용을 봤는데

%[^\n] 이게 도대체 뭘 의미하는지 모르겠네요 ㅠㅠ..

구글링 해봐도 안나오고 ; 이럴때 정말 답답합니다ㅠㅠ

haze11의 이미지

\n 으로 끝나지 않는 문자를 입력받아라?
scanf 도 약간의 정규식이 사용될 수 있더군요.

bluekyu의 이미지

manpage scanf 부분에서 conversions, [ 부분입니다.

Quote:
[

Matches a nonempty sequence of characters from the specified set of accepted characters; the next pointer must be a pointer to char, and there must be enough room for all the characters in the string, plus a terminating null byte. The usual skip of leading white space is suppressed. The string is to be made up of characters in (or not in) a particular set; the set is defined by the characters between the open bracket [ character and a close bracket ] character. The set excludes those characters if the first character after the open bracket is a circumflex (^). To include a close bracket in the set, make it the first character after the open bracket or the circumflex; any other position will end the set. The hyphen character - is also special; when placed between two other characters, it adds all intervening characters to the set. To include a hyphen, make it the last character before the final close bracket. For instance, [^]0-9-] means the set "everything except close bracket, zero through nine, and hyphen". The string ends with the appearance of a character not in the (or, with a circumflex, in) set or when the field width runs out.

/*** Signature ******************
* blog: http://blog.bluekyu.me/ *
********************************/