Skip to main content

Command Palette

Search for a command to run...

The sizeof operator

Published
1 min readView as Markdown
      • Size of a data type = the number of bytes used to store a value of that data type.

  • C has a special operator (sizeof) that returns the size of a data type

    sizeof( dataType ) returns the size of a variable of type "dataType" or sizeof( varName ) returns the size of the variable "varName"

  • The sizeof operator is unusual in that: The sizeof operator is represented by an English-like word ("sizeof")Other operators in C are represented by symbols such as +, =, &&, ||, etc

int main()
{
   printf("sizeof(char)               = %lu\n", sizeof(char) );
   printf("sizeof(short)              = %lu\n", sizeof(short) );
   printf("sizeof(int)                = %lu\n", sizeof(int) );
   printf("sizeof(long)               = %lu\n", sizeof(long) );
   printf("sizeof(long long)          = %lu\n", sizeof(long long) );
   printf("\n");

   printf("sizeof(unsigned char)      = %lu\n", sizeof(unsigned char) );
   printf("sizeof(unsigned short)     = %lu\n", sizeof(unsigned short) );
   printf("sizeof(unsigned int)       = %lu\n", sizeof(unsigned int) );
   printf("sizeof(unsigned long)      = %lu\n", sizeof(unsigned long) );
   printf("sizeof(unsigned long long) = %lu\n", sizeof(unsigned long long) );      
   printf("\n");

   printf("sizeof(float)              = %lu\n", sizeof(float) );
   printf("sizeof(double)             = %lu\n", sizeof(double) );
   printf("sizeof(long double)        = %lu\n", sizeof(long double) );
   printf("\n");

   int x;

   printf("sizeof(x)                  = %lu\n", sizeof(x) );

}

3 views

More from this blog

P

Programming Tutorials

89 posts