[Linux] sizeof small note

Source: Internet
Author: User

1. SizeOf is a keyword, not a function, so don't look at it as a function.

The following are correct:

    int Ten ;    printf ("%d\n"sizeof  i);    printf ("%d\n"sizeof (int));

The following are the wrong:

printf ("%d\nint");  

2. sizeof is executed at compile time, but it can also be executed during the execution period in the compiler implementing ISO C99.

So the following definition is correct:

Char array[sizeof(int);

3. In a 32-bit system, sizeof has any type of pointer, which is 4 because the length of the pointer is 4byte.

4. When the object of sizeof is an array name, the actual length of the array is returned.

Example:

    CharA[] ="ABCDE"; intb[3]; Char(*C) [] = {"a","b","C" }; Char*d[] = {"AAA","BBB","CCC" }; printf ("%d\n",sizeofa);//Output 6 Because there is ' a 'printf"%d\n",sizeofb);//Output 3*4=12printf"%d\n",sizeofc);//output 8, which is an array pointer, it's essentially a pointer, so in 64bit, the sizeof pointer =8printf"%d\n",sizeofD);//Output 24, this is an array of pointers, its essence is an array, which holds 3 pointers, so =8*3=24printf"%d\n",sizeofAsizeof(Char));//output 6, which is how many elements in an array we have to find

5. When the array is passed as a parameter, the pointer is passed, and the sizeof object is actually a pointer:

void Fun (char  a[]) {    printf ("%d\n"sizeof//  output 8}

6. Each of the basic types of sizeof,64-bit system :

printf"%d\n",sizeof(Char));//Output 1printf"%d\n",sizeof( Short));//Output 2printf"%d\n",sizeof(int));//Output 4printf"%d\n",sizeof(Long));//Output 8printf"%d\n",sizeof(float));//Output 4printf"%d\n",sizeof(Double));//Output 8printf"%d\n",sizeof(unsignedChar));//Output 1printf"%d\n",sizeof(unsigned Short));//Output 2printf"%d\n",sizeof(unsignedint));//Output 4printf"%d\n",sizeof(unsignedLong));//Output 8

 

[Linux] sizeof small note

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.