Operands for sizeof operator

The sizeof operator is used to return the size of its operand, in bytes. This operator always precedes its operand. The operand either may be a data-type or an expression. Let’s look at both the operands through proper examples.

  1. type-name: The type-name must be specified in parentheses.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    sizeof(type - name)

    chevron_right

    
    

    Let’s look at the code:

    C

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <stdio.h> 
    int main() 
        printf("%lu\n", sizeof(char)); 
        printf("%lu\n", sizeof(int)); 
        printf("%lu\n", sizeof(float)); 
        printf("%lu", sizeof(double));
        return 0; 

    chevron_right

    
    

    C++

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <iostream> 
    using namespace std; 
      
    int main() 
        cout << sizeof(char)<<"\n"
        cout << sizeof(int)<<"\n"
        cout << sizeof(float)<<"\n"
        cout << sizeof(double)<<"\n"
        return 0; 

    chevron_right

    
    

    Output:

    1
    4
    4
    8
    
  2. expression: The expression can be specified with or without the parentheses.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // First type
    sizeof expression
      
    // Second type
    sizeof(expression)

    chevron_right

    
    

    The expression is used only for getting the type of operand and not evaluation. For example, below code prints value of i as 5 and the size of i a

    C

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <stdio.h>
      
    int main()
    {
        int i = 5;
        int int_size = sizeof(i++);
      
        // Displaying the size of the operand
        printf("\n size of i = %d", int_size);
      
        // Displaying the value of the operand
        printf("\n Value of i = %d", i);
      
        getchar();
        return 0;
    }

    chevron_right

    
    

    C++

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <iostream>
    using namespace std;
      
    int main()
    {
        int i = 5;
        int int_size = sizeof(i++);
      
        // Displaying the size of the operand
        cout << "\n size of i = " << int_size;
      
        // Displaying the value of the operand
        cout << "\n Value of i = " << i;
      
        return 0;
    }
      
    // This code is contributed by SHUBHAMSINGH10

    chevron_right

    
    

    Output:

    size of i = 4
     Value of i = 5
    

References:
http://www.gnu.org/software/gnu-c-manual/gnu-c-manual.html#The-sizeof-Operator

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :


10


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.