sizeof ¿¬»êÀÚ

sizeof ¿¬»êÀÚ(sizeof operator)´Â °ýÈ£ ¾È¿¡ ±â¼úÇÑ ÀμöÀÇ Å©±â°¡ ¸î ¹ÙÀÌÆ®ÀÎÁö¸¦ ³ªÅ¸³»ÁÖ´Â ¿¬»êÀÚÀÔ´Ï´Ù.

 

¡¼Çü½Ä¡½

sizeof(Àμö) Àμö : ÀÚ·áÇü, »ó¼ö, º¯¼ö µî

 

´ÙÀ½ ÇÁ·Î±×·¥À» À©µµ¿ì10, 64Bit¿¡¼­ ½ÇÇà½ÃÄÑ º¾½Ã´Ù.

 

¡¼¼Ò½º ÇÁ·Î±×·¥¡½

// File Name : ex0037_sizeof.c

#include <stdio.h>

int main(void){

  char a;

  int varInt;

  float varFloat;

  char str[10];

  printf("Á¤¼öÀÇ Å©±â:%d   %d\n", sizeof(varInt), sizeof(25));

  printf("½Ç¼öÀÇ Å©±â:%d   %d\n", sizeof(varFloat), sizeof(double));

  printf("¹®ÀÚ(¿­)ÀÇ Å©±â:%d   %d\n", sizeof(a), sizeof(str));

  return 0;

}

 

¡¼½ÇÇà °á°ú¡½

Á¤¼öÀÇ Å©±â:4   4

½Ç¼öÀÇ Å©±â:4   8

¹®ÀÚ(¿­)ÀÇ Å©±â:1   10

 

 

¡¼¼Ò½º ÇÁ·Î±×·¥¡½

// File Name : ex0038_sizeof.c

#include <stdio.h>

int main(void){

  char ch;            unsigned char ch1;

  short sh1;          unsigned short sh2;

  int i1;             unsigned int i2;

  long long1;         unsigned long long2;

  long long long3;    unsigned long long long4;

  float f1;            double f2;

  long double f3;

 

  printf("char:%u \n",sizeof(ch));

  printf("unsigned char:%u \n",sizeof(ch1));

  printf("short:%u \n",sizeof(sh1));

  printf("unsigned short:%u \n",sizeof(sh2));

  printf("int:%u \n",sizeof(i1));

  printf("unsigned int:%u \n",sizeof(i2));

  printf("long:%u \n",sizeof(long1));

  printf("unsigned long:%u \n",sizeof(long2));

  printf("long long:%u \n",sizeof(long3));

  printf("unsigned long long:%u \n",sizeof(long4));

  printf("float:%u \n",sizeof(f1));

  printf("double:%u \n",sizeof(f2));

  printf("long double:%u \n",sizeof(f3));

  return 0;

}

 

 

¡¼½ÇÇà °á°ú¡½

char:1

unsigned char:1

short:2

unsigned short:2

int:4

unsigned int:4

long:4

unsigned long:4

long long:8

unsigned long long:8

float:4

double:8

long double:16

 

 

// File Name : ex0038_sizeof.c

#include <stdio.h>

int main(void){

  char ch;             unsigned char ch1;

  int i1;             unsigned int i2;

  short sh1;           unsigned short sh2;

  long long1;          unsigned long long2;

  long long long3;     unsigned long long long4;

  float f1;            double f2;

  long double f3;

 

  printf("char:%d \n",sizeof(ch));

  printf("unsigned char:%d \n",sizeof(ch1));

  printf("int:%d \n",sizeof(i1));

  printf("unsigned int:%d \n",sizeof(i2));

  printf("short:%d \n",sizeof(sh1));

  printf("unsigned short:%d \n",sizeof(sh2));

  printf("long:%d \n",sizeof(long1));

  printf("unsigned long:%d \n",sizeof(long2));

  printf("long long:%d \n",sizeof(long3));

  printf("unsigned long long:%d \n",sizeof(long4));

  printf("float:%d \n",sizeof(f1));

  printf("double:%d \n",sizeof(f2));

  printf("long double:%d \n",sizeof(f3));

  return 0;

}

 

 

´ÙÀ½ Á¤º¸¿¡¼­¿Í °°ÀÌ ÀÎÅÚCPU, ¿ìºÐÅõ ¸®´ª½º¿¡¼­ ½ÇÇàÇÏ¿© º¾½Ã´Ù.

 

ojk@linux:~$ lsb_release -a

No LSB modules are available.

Distributor ID: Ubuntu

Description:    Ubuntu 20.04.5 LTS

Release:        20.04

Codename:       focal

ojk@linux:~$

ojk@linux:~$ cat /proc/cpuinfo |grep "model name" |head -1

model name      : Intel(R) Core(TM) i7-6700K CPU @ 4.00GHz

ojk@linux:~$

 

 

 

ojk@linux:~/data$ gcc ex0038_sizeof.c

ex0038_sizeof.c: In function ¡®main¡¯:

ex0038_sizeof.c:12:16: warning: format ¡®%u¡¯ expects argument of type ¡®unsigned int¡¯, but argument 2 has type ¡®long unsigned int¡¯ [-Wformat=]

   12 |  printf("char:%u \n",sizeof(ch));

      |               ~^     ~~~~~~~~~~

      |                |     |

      |                |     long unsigned int

      |                unsigned int

      |               %lu

 

 

sizeof()ÀÇ ¹Ýȯ°ªÀÌ logn unsigned int¶ó°í ÇÏ¿© waringÀÌ ³ª¿É´Ï´Ù. ¹°·Ð waringÀ̹ǷΠÄÄÆÄÀÏÀº ¿Ï·áµÇ°í ½ÇÇ൵ µË´Ï´Ù. sizeof()´Â ÇÔ¼ö°¡ ¾Æ´Ï°í ¿¬»êÀÚÀÔ´Ï´Ù. ÀÌ ¿¬»êÀÚ´Â ÇÔ¼öó·³ ¹Ýȯ°ªÀÌ Àִµ¥ ±× ¹Ýȯ°ªÀº 'size_t'ÀÔ´Ï´Ù.

size_t ´Â unsigned int À̸ç, ¹®ÀÚ¿­À̳ª ¸Þ¸ð¸®ÀÇ »çÀÌÁ ³ªÅ¸³¾ ¶§ »ç¿ëÇÕ´Ï´Ù. "unsigned int"¸¦

 

typedef unsigned int size_t;

 

ÀÌ·¸°Ô size_t ¶ó´Â À̸§À¸·Î Á¤ÀÇÇØ ³õÀº °ÍÀÔ´Ï´Ù. size_t ´Â, 32ºñÆ® ¿î¿µÃ¼Á¦¿¡¼­´Â "ºÎÈ£¾ø´Â 32ºñÆ® Á¤¼ö"À̰í, 64ºñÆ® ¿î¿µÃ¼Á¦¿¡¼­´Â "ºÎÈ£¾ø´Â 64ºñÆ® Á¤¼ö"ÀÔ´Ï´Ù. ±×·¯³ª "unsigned int" ¶Ç´Â "int"´Â, 64ºñÆ® OS¶ó°í ÇØ¼­ ²À 64ºñÆ® Á¤¼ö´Â ¾Æ´Õ´Ï´Ù. ¿©ÀüÈ÷ 32ºñÆ®ÀÏ ¼öµµ ÀÖ½À´Ï´Ù. À̰ÍÀÌ size_tÇü°ú "unsigned int"ÇüÀÇ Â÷ÀÌÀÔ´Ï´Ù.

 

¸Þ¸ð¸®³ª ¹®ÀÚ¿­ µîÀÇ ±æÀ̸¦ ±¸ÇÒ ¶§¿¡´Â "unsigned int" ´ë½Å size_t ¶ó´Â ÇüÀ¸·Î ±æÀ̰¡ ¹ÝȯµË´Ï´Ù.

 

 

ojk@linux:~/data$ gcc ex0038_sizeof.c

ojk@linux:~/data$

ojk@linux:~/data$ ./a.out

char:1

unsigned char:1

int:4

unsigned int:4

short:2

unsigned short:2

long:8

unsigned long:8

long long:8

unsigned long long:8

float:4

double:8

long double:16

ojk@linux:~/data$

 

 

sizeof ¿¬»êÀÚ´Â »ç¿ëÇÏ´Â ÄÄÇ»ÅÍ ±âÁ¾¿¡ µû¶ó ÇÁ·Î±×·¡¹Ö ¾ð¾î¿¡ µû¶ó¼­ ´Ù¸¦ ¼öµµ ÀÖÀ¸¹Ç·Î ÁÖÀÇÇØ¾ß ÇÕ´Ï´Ù.

 

 

In C:

 

// File Name : ex0039_sizeof.c

#include <stdio.h>

int main() {

  char c = 'a';

  printf("Size of char : %d\n",sizeof(c));

  printf("Size of char : %d\n",sizeof('a'));

  return 0;

}

 

Output:

 

Size of char : 1

Size of char : 4

 

 

In C++:

 

// File Name : ex0040_sizeof.cpp

#include <iostream>

int main() {

  char c = 'a';

  std::cout<<"Size of char : "<<sizeof(c)<<"\n";

  std::cout<<"Size of char : "<<sizeof('a')<<"\n";

  return 0;

}

 

Output:

Size of char : 1

Size of char : 1

 

Why the sizeof('a') returns different values in C and C++?

 

 

In C, the type of a character constant like 'a' is actually an int, with size of 4 (or some other implementation-dependent value). In C++, the type is char, with size of 1. This is one of many small differences between the two languages.

 

¡¼ÇÑ °ÉÀ½ ´õ¡½

´ÙÀ½Àº ¸¶ÀÌÅ©·ÎÇÁ·Î¼¼¼­·Î ¸¹ÀÌ »ç¿ëÇÏ´Â ¾ÆµÎÀ̳뿡¼­ÀÇ º¯¼ö ¼±¾ð°ú ±× Å©±â¸¦ È®ÀÎÇÑ °ÍÀÔ´Ï´Ù. intÇüÀÌ 2¹ÙÀÌÆ®, longÇüÀÌ 4¹ÙÀÌÆ®·Î Ãâ·ÂÀÌ µË´Ï´Ù.