C language strlen () function usage

Source: Internet
Author: User

C language strlen () function: Returns the length of a string

Header files: #include <string.h>

The strlen () function is used to calculate the length of a string, and its prototype is: unsigned int strlen (char *s); s for the specified string

eg

#include <stdio.h> #include <string.h>int main () {    char *str1 = "http://see.xidian.edu.cn/cpp/u/shipin/";    Char str2[100] = "http://see.xidian.edu.cn/cpp/u/shipin_liming/";    Char str3[5] = "12345";    printf ("strlen (str1) =%d, sizeof (STR1) =%d\n", strlen (str1), sizeof (STR1));    printf ("strlen (str2) =%d, sizeof (STR2) =%d\n", strlen (str2), sizeof (STR2));    printf ("strlen (STR3) =%d, sizeof (STR3) =%d\n", strlen (STR3), sizeof (STR3));    return 0;}
Operation Result:
Strlen (str1) =38, sizeof (STR1) =4
Strlen (str1) =45, sizeof (STR1) =100
Strlen (str1) =53, sizeof (STR1) =5

if the number of characters equals the size of the character array, then the return value of strlen () cannot be determined , for example
Char str[6] = "ABCXYZ";
The return value of strlen (str) will be indeterminate. Because the end of STR is not 0,strlen () will continue to retrieve backwards until it encounters ' \ ' and the contents of these areas are indeterminate.

C language strlen () function usage

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.