scanf를 이용한 다양한 출력 방법 (scan set, edit set)
scanf("%자리수[...]%*c, s);
- %[…] 은 edit set이라 불리며 scanf()를 통해 전달 받은 문자열이 […]안의 문자와 매치되는 경우에만 s에 저장
scanf("%자리수[^]%*c", s);
- %[]안의 ^는 scanf()를 통해 전달 받은 문자열이 ^ 뒤에 나오는 문자와 매치되지 않을 때 s에 저장됨.
- [^\n]는 \n을 제외한 문자라는 정규 표현식(RegEx)이다.
- 조건을 만족하지 못하는 곳과 그 뒷부분은 모두 무시
- scanf("%3[^\n]%*c", s);는 \n을 만날 때까지 입력받거나 세자리까지 입력 받는다.
scanf("%5[^:]%*c", s);는 :를 만날 때까지 입력 받거나 다섯자리까지 입력을 받는다.
%s와 %c의 차이
- %s는 white-space를 만나면 읽기를 중단함.
%c는 공백 여부에 관계없이 지정한 문자 수를 읽습니다.
%*c의 의미
scanf(“%5[^:-]%*c%d”, s, &d);
- :나 -를 만나거나 다섯자리까지 문자를 입력받는다.
- :나 -를 만나면 그 뒤에 있는 숫자를 가져옴
Input값에 따른 값의 차이
- 정해놓은 입력 받는 길이(5)보다 커졌을 때 값이 어떻게 바뀌는지 확인해봤다.
-는 음수 표현도 가능하기 때문에 좀 더 다양하게 값이 출력이 된다.
결론?
- scanf(“%5[^:-]%*c%d”, s, &d);와 같이 %c%d를 섞어 쓰는 경우 내가 원하는 값을 정확하게 출력하는게 어렵다고 생각이 든다. 자주 사용할 지도 의문이다.
같이 볼 내용
정규표현식 RegEx
# regex Regular Expression /{pattern}/{flag} # pattern ## Groups and rages | 또는 ( ) 그룹 [ ] 문자셋, 괄호안의 어떤 문자든 [^] 부정 문자셋, 괄호안의 어떤 문자가 아닐 때 (?:) 찾지만 기억하지는..
sondho.tistory.com
참고
C 언어 레퍼런스 - scanf 함수
modoocode.com
stackoverflow.com/questions/30065675/what-does-scanf-nc-mean
What does `scanf("%*[^\n]%*c")` mean?
I want to make a loop in C that, when the program asks for an integer and the user types a non-digit character, the program asks again for an integer. I just found the below code. but I don't unde...
stackoverflow.com
www.quora.com/What-does-scanf-n-c-name-mean
What does scanf ("% [^\n] %*c",name) mean?
Answer (1 of 5): In short, the statement scanf(“%[^\n]%*c”,name); means that all the characters entered as the input, including the spaces, until we hit the enter button are stored in the variable, name; provided we allocate sufficient memory for the v
www.quora.com