String Length function strlen ()

Source: Internet
Author: User
The following is my test file:
 
 
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. int main()
  5. {
  6. char str1[10] = "123456789";
  7. char str2[10] = "1234567890abcd";
  8. char str3[10] = "12345";
  9. char str4[10] = "12345 12";
  10. char str5[10] = {‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘\0‘, ‘5‘, ‘6‘};
  11. int len1, len2, len3, len4, len5;
  12. len1 = strlen(str1);
  13. len2 = strlen(str2);
  14. len3 = strlen(str3);
  15. len4 = strlen(str4);
  16. len5 = strlen(str5);
  17. printf("str1--> %s, len-->%d\n", str1, len1);
  18. printf("str2--> %s, len-->%d\n", str2, len2);
  19. printf("str3--> %s, len-->%d\n", str3, len3);
  20. printf("str4--> %s, len-->%d\n", str4, len4);
  21. printf("str5--> %s, len-->%d\n", str5, len5);
  22. printf("====== Game Over ======\n");
  23. printf("\n");
  24. return 0;
  25. }
The following is my test result:
 
 
  1. str1--> 123456789, len-->9
  2. str2--> 1234567890123456789, len-->19
  3. str3--> 12345, len-->5
  4. str4--> 12345 12, len-->8
  5. str5--> 1234, len-->4
  6. ====== Game Over ======

Result Analysis: based on the result of "Man strlen", strlen () calculates the length of string S, but does not include the ending character '\ 0 '. Therefore, the '\ 0' character indicates the end of a string. In my test:
Str1 The length is easy to know.
Str2 Why is the str2 length incorrect? This is because the "ABCD" character cannot be stored in a storage unit with a length of 10 in str2, and it cannot be allocated or stored out of bounds, therefore, "ABCD" is not allocated to storage units, and the end character '\ 0' of str2 is not automatically allocated, and str2 is allocated next to str1, when reading str2, the content in str1 is automatically read without the ending character '\ 0, in this way, str2 truncates "ABCD" and reads str1 content in succession. The result of strlen (str2) is the total length of str1 + str2.
Str3 The length is easy to understand.
Str4 Note that the NULL Character in str4 is between '5' and '1'. It is a null character rather than the ending character '\ 0', so the length of str4 is easy to understand.
Str5 Because I explicitly add an ending character '\ 0' to the string, it will end from the first' \ 0' when reading or judging the length of the string, then the first '\ 0' will mislead you when accessing str5.






From Weizhi note (wiz)

String Length function strlen ()

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.