A summary of the sizeof function in C language

Source: Internet
Author: User

The result of the sizeof function:

  1. Variable: The number of bytes that the variable occupies.
    int 0 ;p rintf ("%d\n"sizeof(i));    // 4
  2. Array: The number of bytes that the array occupies.
    intArr_int1[] = {1,2,3,4,5};intarr_int2[Ten] = {1,2,3,4,5};p rintf ("size_arr1=%d\n",sizeof(Arr_int1));//5*4=20printf"size_arr2=%d\n",sizeof(Arr_int2));//10*4=40
  3. String: Actually, it is an array of characters that have been added ' \ s '. The result is a string character length of +1.
    Char " Str " ;p rintf ("size_str=%d\n",sizeof(str));    // 3+1=4
  4. Pointer: fixed Length: 4 (32-bit address environment).
      • Special Description: When an array is an entry parameter to a function, the logarithm of the group sizeof in the function is fixed to 4: Because the passed-in parameter is a pointer.
         int  get_size (int   arr[]) { return  sizeof  Span style= "COLOR: #000000" (arr);}  int   main () { int  arr_int[10 ] = {1 , 2 , 3 , 4 , 5  }; printf (  " size_fun_arr=%d\n   ", Get_size (Arr_int)); // 4 } 
  5. Structural body
    1. Structure with variables only:
      1. The result is an integer multiple of the maximum number of bytes for the widest variable: [4 1 xx x]
        struct Test {    int  i;     Char ch;} test_t;printf ("size_test=%d\n"sizeof(test_t));    // 8
      2. several variables with a smaller width can be populated within a width range: [4 2 1 1]
         typedef struct   Test { int   I    ;     short   S;     char   ch1;  char   CH2;} test_t;printf (  " size_test=%d\n     Span style= "COLOR: #800000" > ", sizeof  (test_t)); // 8  
      3. address alignment: The offset of a struct member must be an integer multiple of its own width: [4 1 x 2 1 x x x]
         typedef struct   Test { int
                   I;     char   ch1;     short   S;  char   CH2;} test_t;printf (  " size_test=%d\n     Span style= "COLOR: #800000" > ", sizeof  (test_t)); // 12  
    2. struct with array: Contains the width of the entire array. The array width is detailed above. [4*10 2 1 1]
      struct Test {    int i[];      Short s;     Char ch1;     Char CH2;} test_t;printf ("size_test=%d\n"sizeof(test_t));    //  -
    3. Structure of nested structure body
      1. Contains the width of the entire internal structure (that is, the entire expanded internal structure): [4 4 4]
        struct son {    int  name;     int birthday;    } son_t;     struct Father {    son_t  s1;     int Wife;} father_t;printf ("size_struct=%d\n",sizeof(father_t));    //  A
      2. Address alignment: The internal structure of the expandedoffset for first member, it must be unfolded.the widest variable in the internal structureInteger multiples of the occupied bytes: [2x x2 x x 4 4 4]
        struct Son {    short age  ;     int name;     int birthday;    } son_t;     struct Father {    short age  ;    son_t  S1;     int Wife;} father_t;printf ("size_struct=%d\n",sizeof(father_t));    //  -

A summary of the sizeof function in C language

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.