off_t 를 32bit, 64bit 시스템에 상관 없이 printf 하려면 어떻게 해야 하나요?

irdeal의 이미지

off_t 의 typedef가

32bit 시스템에서 컴파일 시에 -D_FILE_OFFSET_BITS=64 옵션을 주고 컴파일을 하면

long long int 고

64bit 시스템에서는 long int 로 알고 있습니다.

하나의 코드에서 printf 로 해당 값을 출력하고 싶은데 어떻게 포맷팅 해야 하나요?

현재는

#ifdef WORDSIZE == 32 등으로

32bit 일경우에는 %lld 로 64비트일 경우에는 %ld로 하고 있는데 한 번에 표현 할 수 있는 방법이 없나요?

검색을 해봐도 잘 나오지가 않네요.

익명 사용자의 이미지

#if _FILE_OFFSET_BITS == 64
어쩌고 저쩌고
#endif

라고 하면 되겠군요.

ymir의 이미지

#if _FILE_OFFSET_BITS == 64
#define fmt_off  "%lld"
#else
#define fmt_off  "%ld"
#endif
 
off_t value = 1234;
printf("value = "fmt_off"\n", value);

대충 이런식으로 써먹어도 되겠네요.

되면 한다! / feel no sorrow, feel no pain, feel no hurt, there's nothing gained.. only love will then remain.. 『 Mizz 』

irdeal의 이미지

감사합니다...

May The Force Be With You
irdeal

익명_사용자의 이미지

좀 더 읽기 쉽게 하실려면

그냥

if( sizeof(off_t) == sizeof(long int) )
   printf( "%ld", value );
else
   printf( "%lld", value );

이런식으로 하셔도 괜찮습니다.

sizeof(off_t) == sizeof(long int)같은 구문의 경우
컴파일러에 최적화에 걸려 제거될 가능성이 99.999999%이므로
실제 컴파일 하면

저 코드는 시스템에 맞는 적절한 하나의 printf()로 대체될것입니다.(컴파일후)
즉, if-statement도 제거될것이고, 해당 시스템에 안맞는 printf()도 제거될것이고..
매크로를 쓰는것과 동일한 효과를 얻는셈이죠.

방금 gcc 4.4.5 on x86로 단순한 코드를 만들어서
컴파일 하여 disassemble해봤는데

심지어 아무런 최적화옵션을 안주어도
if( .. )와 실행이 안될 printf() 모두 제거되네요.

irdeal의 이미지

답변 감사합니다.

제가 하고 싶은 것은 코딩시에..

그냥 한줄로..예를 들면

printf("size = %zd", (size_t)size);

머 이런식으로 끝 맺고 싶습니다..실제 size_t 변수는 %zd로 표현 할 수 있습니다.

구글링을 해보면 off_t는 %jd 로 찍고 (intmax_t)로 형변환해서 표현하면 된다고 나오는데...좀..잘 안맞는거 같아서요..

현재는 말씀해주신 것처럼 코딩을 해놓았는데..로그를 찍는 부분이 많아서 보기도 좀 그렇고 거슬려서요..ㅜㅜ

May The Force Be With You
irdeal

익명 사용자의 이미지

off_t 값 printf 출력
printf("%jd\n", (intmax_t)x);

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.