Library function strlen Source code reproduction and attention problems

Source: Internet
Author: User

First directly on the source:

size_t strlen (const char * str) {const char *eos = Str;while (*eos++); return (eos-str-1);}

You can see that the strlen is using a new char* pointer, which traverses backwards to find the next element in the end of the string. Then calculate the distance.

But notice here that a little size_t is actually unsigned int type. Therefore, you cannot use strlen (a)-strlen (b) when comparing the two string length size. Because two unsigned integer subtraction is not a negative number, the result is an integer regardless of how it is obtained. That is, strlen (a) is always greater than strlen (b).

You can use the expression strlen (a) > strlen (b) to determine the size of two strings, and you cannot use strlen (a)-strlen (b) > 0

Library function strlen Source code reproduction and attention problems

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.