printf 포맷스트링에 %n 이란??

Lipton의 이미지

printf("%n", variable);

%n 은 처음보는데

검색도 못하겠고(%는 검색엔진이 인식안해서-_-;; )

어디 물어볼데가 없네요 ㅋㅋ

가르쳐주세요~ 저게 뭔가요?

musiphil의 이미지

The argument shall be a pointer to signed integer into which is written the number of characters written to the output stream so far by this call to fprintf. No argument is converted, but one is consumed. If the conversion specification includes any flags, a field width, or a precision, the behavior is undefined. [7.19.6.1/8]

(예)
int d = 1234, n;
printf("%d%n\n", d, &n);
printf("%d digits\n", n);
(결과)
1234
4 digits

IDNed의 이미지

유일하게 printf 디렉티브 중 인자에 쓰기를 하는 녀석입니다.
%n 자리에 int 포인터를 넣어주면(unsigned int인지는 잘 모르겠습니다) 그자리에 지금까지 프린트 한 문자개수를 출력시켜줍니다.

이 디렉티브 때문에 FSB(Format-String Bug) 보안 구멍이 개발되었죠 ^^

해커스쿨(www.hackerschool.org) 도서관에 FSB(포맷스트링 어택) 문서를 보시면 이해가 될 겁니다.