Three minutes to understand the C language sizeof

Source: Internet
Author: User

Three minutes to understand the C language sizeof
I. Concepts

Sizeof is a single object operator, which is the same as a ++ operator. Outputs the storage size of the operation object in bytes.

Ii. Usage a. operation data type

For example, sizeof (int), the length of the output int type in the memory, depending on the specific environment, the local output is 4.

B. Operation Variables

For example, char a [6]; printf ("% d \ n", sizeof (a). Because the char array length has been defined, the output value is 6.

Iii. FAQs

Note the following examples:

Example 1:

Int testSizeOf (char x []) {return sizeof (x);} int main (int argc, char * argv []) {char y [6]; int temp = testSizeOf (y); printf ("% d \ n", temp );}
The output result here is no longer 6, because in the testSizeOf function, a pointer is actually passed in, so here sizeof actually takes the number of bytes of the pointer, so the result is 4.

Example 2:

Int main (int argc, char * argv []) {printf ("% d \ n", sizeof ("0123456789 "));}
Here, the output result is 11, because in the language, Ten Characters and the ending character "\ 0" are considered here ".

Related Article

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.