[완료] type별 size문제인데 이해가 가지 않습니다.

yundorri의 이미지

좀 더 쉽게 보시기 위해서 첨부를 올립니다.

 179     /** 타입 별 크기를 알아보기 위한 코드 2007.02.13 ******************/
 180     long long test = 0xFFFFFFFF;
 181     char      test_char     = (char)test;
 182     short     test_short    = (short)test;
 183     int       test_int      = (int)test;
 184     long      test_long     = (long)test;
 185     unsigned  test_unsigned = (unsigned)test;
 186     float     test_float    = (float)test;
 187     double    test_double   = (double)test;
 188     long long test_longlong = (long long)test;
 189     unsigned char      test_uchar     = (unsigned char)test;
 190     unsigned short     test_ushort    = (unsigned short)test;
 191     unsigned long      test_ulong     = (unsigned long)test;
 192     unsigned long long test_ulonglong = (unsigned long long)test;
 193 
 194     printf( "char                       = %d Bytes (MAX %X)\n", sizeof(char),              (char)test_char
 195     printf( "short                      = %d Bytes (MAX %X)\n", sizeof(short),             (short)test_short
 196     printf( "int                        = %d Bytes (MAX %X)\n", sizeof(int),               (int)test_int
 197     printf( "long                       = %d Bytes (MAX %X)\n", sizeof(long),              (long)test_long
 198     printf( "unsigned                   = %d Bytes (MAX %X)\n", sizeof(unsigned),          (unsigned)test_unsigned
 199     printf( "float                      = %d Bytes (MAX %X)\n", sizeof(float),             (float)test_float
 200     printf( "double                     = %d Bytes (MAX %X)\n", sizeof(double),            (double)test_double
 201     printf( "long long                  = %d Bytes (MAX %X)\n", sizeof(long long),         (long long)test_longlong
 202     printf( "unsigned char  (BYTE)      = %d Bytes (MAX %X)\n", sizeof(unsigned char),     (unsigned char)test_uchar
 203     printf( "unsigned short (WORD)      = %d Bytes (MAX %X)\n", sizeof(unsigned short),    (unsigned short)test_ushort
 204     printf( "unsigned long (DWORD)      = %d Bytes (MAX %X)\n", sizeof(unsigned long),     (unsigned long)test_ulong
 205     printf( "unsigned long long (QWORD) = %d Bytes (MAX %X)\n", sizeof(unsigned long long),(unsigned long long)test_ulon
 206     printf( "\n" );
 207 
 
:!./examples                                                                                               
char                       = 1 Bytes (MAX FFFFFFFF)
short                      = 2 Bytes (MAX FFFFFFFF)
int                        = 4 Bytes (MAX FFFFFFFF)
long                       = 4 Bytes (MAX FFFFFFFF)
unsigned                   = 4 Bytes (MAX FFFFFFFF)
float                      = 4 Bytes (MAX 0)
double                     = 8 Bytes (MAX FFE00000)
long long                  = 8 Bytes (MAX FFFFFFFF)
unsigned char  (BYTE)      = 1 Bytes (MAX FF)
unsigned short (WORD)      = 2 Bytes (MAX FFFF)
unsigned long (DWORD)      = 4 Bytes (MAX FFFFFFFF)
unsigned long long (QWORD) = 8 Bytes (MAX FFFFFFFF)

왜 이렇게 나올까요?
제 생각대로라면 char는 F가, short는 FF가, int는 FFFF가 나와야 하는데요...

워낙 기초가 없다보니 이런 문제도 모르겠습니다. ㅠ_ㅠ

제 시스템은
Linux swdev2 2.6.22.14-72.fc6 #1 SMP Wed Nov 21 13:44:07 EST 2007 i686 i686 i386 GNU/Linux
입니다. 즐거운 하루 되십시요!!

File attachments: 
첨부파일 크기
Image icon sizeof.jpg210.27 KB
ymir의 이미지

char, short 등을 unsigned 로 바꿔보세요.
char 변수에 0xFF 를 넣으면 값이 -1 이 됩니다.
%X 는 unsigned int 를 HEX 로 찍는 놈이니 입력값을 unsigned 로 판단할텐데..
-1 이 unsigned int 로 캐스팅 되면.. 0xFFFFFFFF 라는 값이 됩니다.

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

yundorri의 이미지

답글 감사합니다.
근데 unsigned int도 결국 4byte인데 0xFFFFFFFF (8byte)로
프린트를 할까요?

Fe.head의 이미지

0xFFFFFFFF 는 정수로 보면 -1 입니다.
이것을 char 로 바꾸면 0xFF(-1)겠고 이것을 %d형(int)으로 바꾸면 0xFFFFFFFF(-1)이겠죠

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

yundorri의 이미지

4byte가 0xFFFFFFFF 이죠?
그러면 8byte인 long long은

long long test = 0xFFFFFFFFFFFFFFFF;

이렇게 되어야 되는데 이게 컴파일이 안되네요.

g++ -o examples examples.cpp
examples.cpp:180: error: integer constant is too large for 'long' type
make: *** [all] 오류 1
(2 of 3): error: integer constant is too large for 'long' type

이건 어케 된걸까요?

yundorri의 이미지

long long test = 0xFFFFFFFF;
test |= (test << 32);

그래도 결과는 같군요.

:!./examples                                                                                                               
char                       = 1 Bytes (MAX FFFFFFFF)
short                      = 2 Bytes (MAX FFFFFFFF)
int                        = 4 Bytes (MAX FFFFFFFF)
long                       = 4 Bytes (MAX FFFFFFFF)
unsigned                   = 4 Bytes (MAX FFFFFFFF)
float                      = 4 Bytes (MAX 0)
double                     = 8 Bytes (MAX 0)
long long                  = 8 Bytes (MAX FFFFFFFF)
unsigned char  (BYTE)      = 1 Bytes (MAX FF)
unsigned short (WORD)      = 2 Bytes (MAX FFFF)
unsigned long (DWORD)      = 4 Bytes (MAX FFFFFFFF)
unsigned long long (QWORD) = 8 Bytes (MAX FFFFFFFF)
ymir의 이미지

일단 아래 글을 한번 살펴보신 후에, 다시 코드를 작성해 보심이 좋을 것 같습니다.
별도의 접미사가 없는 정수형 상수는 모두 int 로 취급됩니다.
그래서 0xFFFFFFFFFFFFFFFF 는 int 라기에는 너무 크다고 컴파일러가 경고해 주는 겁니다.
더불어 printf 의 man page 도 찬찬히 살펴보시고, format string 을 type 에 맞게 적절히 바꿔보세요.

http://ko.wikibooks.org/wiki/%EC%A0%95%EC%88%98%ED%98%95_%EB%8D%B0%EC%9D%B4%ED%84%B0

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

yundorri의 이미지

^_____^

두 분 도움으로 궁금증이 해결되었습니다.

코드를 수정했구요, 출력 인자를 적절히 지정해야되는 것이었군요.
상수 지정시에도요.

결과는 이렇습니다.

:!./examples                                                                                                               
char                       = 1 Bytes (MAX 0xff              )
short                      = 2 Bytes (MAX 0xffff            )
int                        = 4 Bytes (MAX 0xffffffff        )
long                       = 4 Bytes (MAX 0xffffffff        )
unsigned                   = 4 Bytes (MAX 0xffffffff        )
float                      = 4 Bytes (MAX   -1.000000       )
double                     = 8 Bytes (MAX   -1.000000       )
long long                  = 8 Bytes (MAX 0xffffffffffffffff)
unsigned char  (BYTE)      = 1 Bytes (MAX 0xff              )
unsigned short (WORD)      = 2 Bytes (MAX 0xffff            )
unsigned long (DWORD)      = 4 Bytes (MAX 0xffffffff        )
unsigned long long (QWORD) = 8 Bytes (MAX 0xffffffffffffffff)

코드는 이렇게 수정했구요.

 178 #ifdef _SIZEOF_TYPE_
 179     /** 타입 별 크기를 알아보기 위한 코드 2007.02.13 ******************/
 180     long long test = 0xFFFFFFFFFFFFFFFFLL;
 181     char      test_char     = (char)test;
 182     short     test_short    = (short)test;
 183     int       test_int      = (int)test;
 184     long      test_long     = (long)test;
 185     unsigned  test_unsigned = (unsigned)test;
 186     float     test_float    = (float)test;
 187     double    test_double   = (double)test;
 188     long long test_longlong = (long long)test;
 189     unsigned char      test_uchar     = (unsigned char)test;
 190     unsigned short     test_ushort    = (unsigned short)test;
 191     unsigned long      test_ulong     = (unsigned long)test;
 192     unsigned long long test_ulonglong = (unsigned long long)test;
 193 
 194     printf( "char                       = %d Bytes (MAX 0x%-16hhx)\n", sizeof(char),              (char)test_char                    );
 195     printf( "short                      = %d Bytes (MAX 0x%-16hx)\n",  sizeof(short),             (short)test_short                  );
 196     printf( "int                        = %d Bytes (MAX 0x%-16x)\n",   sizeof(int),               (int)test_int                      );
 197     printf( "long                       = %d Bytes (MAX 0x%-16lx)\n",  sizeof(long),              (long)test_long                    );
 198     printf( "unsigned                   = %d Bytes (MAX 0x%-16x)\n",   sizeof(unsigned),          (unsigned)test_unsigned            );
 199     printf( "float                      = %d Bytes (MAX   %-16f)\n",   sizeof(float),             (float)test_float                  );
 200     printf( "double                     = %d Bytes (MAX   %-16f)\n",   sizeof(double),            (double)test_double                );
 201     printf( "long long                  = %d Bytes (MAX 0x%-16llx)\n", sizeof(long long),         (long long)test_longlong           );
 202     printf( "unsigned char  (BYTE)      = %d Bytes (MAX 0x%-16hhx)\n", sizeof(unsigned char),     (unsigned char)test_uchar          );
 203     printf( "unsigned short (WORD)      = %d Bytes (MAX 0x%-16hx)\n",  sizeof(unsigned short),    (unsigned short)test_ushort        );
 204     printf( "unsigned long (DWORD)      = %d Bytes (MAX 0x%-16lx)\n",  sizeof(unsigned long),     (unsigned long)test_ulong          );
 205     printf( "unsigned long long (QWORD) = %d Bytes (MAX 0x%-16llx)\n", sizeof(unsigned long long),(unsigned long long)test_ulonglong );
 206     printf( "\n" );
 207 #endif

댓글 달기

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