Differences between sizeof and strlen

Source: Internet
Author: User

(1) When the strlen function is used to calculate the length of a string, the strlen result value does not contain '\ 0. When sizeof calculates the length of a string, its result value includes '\ 0 '.

(2) If sizeof is used as an array, only the size of the static array can be measured, and the size of the dynamically allocated or external array cannot be detected.

(3) sizeof is a keyword rather than a function. sizeof occupies an hour of space for computing variables, and brackets can be omitted. The calculation type (model) cannot be omitted for an hour. Of course, it can also calculate the function size. When the calculation function is large, parentheses can be omitted. The size of the calculation function is the number of bytes of the type returned by the function. No return value function. The default size is 1 byte.

(4) the sizeof operation on a variable is the same as the sizeof operation on the address where the variable is located.

Int I = 0;

Sizeof (I) = 4, sizeof (& I) = 4

(5) sizeof () and initial initialization are irrelevant; strlen () is related to initialization.

(6) the sizeof operation results in the number of bytes in the memory space occupied by the operands. strlen returns the length of the string ending with the '\ 0' Terminator.

Char STR [20] = "0123456789 ";

Int A = sizeof (STR) // A = 20;

Int B = strlen (STR) // B = 10;

(7) sizeof is calculated at compilation, and strlen is operated only at runtime.



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.