GNU C 에서의 printf 의 확장 및 locale 사용

sunyzero의 이미지

GCC에서는 printf에서 여러가지로 확장할 수 있는 부분이 많습니다. 특히 precision 이나 scale등을 가지고 장난을 치게되면 심히 보기 어려운 코드를 만들 수도 있습니다. 현재는 가끔 장난치기용으로 씁니다.

그리고 locale을 이용해서 출력포매팅을 바꿀 수 있습니다. 예를 들면 123456.123 을 -> 123.456,123 처럼 천단위로 끊어서 표현도 가능합니다.)

같이 포함하는 소스를 컴파일해서 실행시켜 보시면 압니다. 간단히 녀석을 살펴보면.

printf("\t(Weekday/MonthDay/Month) = %3$.*1$s / %6$0*5$d / %2$.*4$s\n", 3, month, weekday, 8, 2, mday);
printf("\t(Weekday/MonthDay/Month) = %3$.*1$s / %6$*5$d / %2$.*4$s\n", 6, month, weekday, 9, 2, mday);
printf("\t(Month/MonthDay/Weekday) = %2$.*4$s / %6$0*5$d / %3$.*1$s - %7$d:%8$d\n", 3, month, weekday, 9, 2, mday, hour, min);

/* it will be formatting thousand separator.. */
setlocale(LC_NUMERIC, "en_DK");
printf("\t(%'.2f)\n", 12345678.4321);

위와 같은 코드를 돌리면 아래와 같이 출력됩니다. 이해가 가시죠?

(Weekday/MonthDay/Month) = Sun / 03 / July(7월
(Weekday/MonthDay/Month) = Sunday / 3 / July(7월)
(Month/MonthDay/Weekday) = July(7월) / 03 / Sun - 10:2
(12.345.678,43)

즉 #$형태로 뒤에 오는 아규먼트를 직접 순서를 지정할 수 있다는 것이겠죠. 따라서 %의 개수와 아규먼트의 개수가 달라도 골라서 쓰게되면 조금 다른 결과가 나오게 됩니다.

#include <stdio.h>
#include <locale.h> /* for setlocale : ANSI C & POSIX.1 */

char *weekday = "Sunday(일요일)";
char *month = "July(7월)";
int mday = 3;
int hour = 10;
int min = 2;

int
main()
{
	/* Custom argument indication : 
	 * from SUS(Single UNIX Specification 
	 * it's not C99 Standard !!! 
	 */
	printf("\t(Weekday/MonthDay/Month) = %3$.*1$s / %6$0*5$d / %2$.*4$s\n",
			3, month, weekday, 8, 2, mday);
	printf("\t(Weekday/MonthDay/Month) = %3$.*1$s / %6$*5$d / %2$.*4$s\n",
			6, month, weekday, 9, 2, mday);
	printf("\t(Month/MonthDay/Weekday) = %2$.*4$s / %6$0*5$d / %3$.*1$s - %7$d:%8$d\n",
			3, month, weekday, 9, 2, mday, hour, min);

	/* it will be formatting thousand separator.. */
	setlocale(LC_NUMERIC, "en_DK");
	printf("\t(%'.2f)\n", 12345678.4321);

	exit(0);
}

Forums: 
keizie의 이미지

gettext로 .po에 번역된 텍스트를 관리하면
우리말과 원어의 어순이 달라서 주어진 인자를 그대로 쓰기가 곤란할 때가 있습니다.
그럴 때 순서를 바꿔서 넣기도 하죠.

Fe.head의 이미지

"%3$d %2$s"
이런거 윈도우에서만 쓸수 있게.. sprintf, vsprintf나 vsnprintf만 빼다가 쓸수 있는 방법없나요?

-----------------------
과거를 알고 싶거든 오늘의 네 모습을 보아라. 그것이 과거의 너니라.
그리고 내일을 알고 싶으냐?
그러면 오늘의 너를 보아라. 그것이 바로 미래의 너니라.

고작 블로킹 하나, 고작 25점 중에 1점, 고작 부활동
"만약 그 순간이 온다면 그때가 네가 배구에 빠지는 순간이야"

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.