Difference between Strlen () and Sizeof (); Difference between strlensizeof

Source: Internet
Author: User

Difference between Strlen () and Sizeof (); Difference between strlensizeof

The strlen () function is used to calculate the actual length of a string. It is obtained from the beginning to the First '\ 0'. If you only define that it is not assigned an initial value, this result is not fixed. It will be searched from the first address until it encounters '\ 0 '. The strlen results can be calculated only at runtime.

Sizeof () returns the amount of memory occupied after the variable declaration, not the actual length. In addition, sizeof is not a function, but an operator, and strlen is a function. Sizeof is calculated during compilation.

Example:

1. char xs = "123456789 ";

Sizeof (s) // 4, s is a character pointer to a String constant

Strlen (s) // 9, which can only be used to obtain the length of this string

2. char s [] = "123456789 ";

Sizeof (s) // 10, s is an array, calculated to the '\ 0' position, so 9 + 1

Strlen (s) // 9, strlen is a function internal implementation is to use a loop to calculate until '\ 0'

3. char s [100] = "123456789 ";

Sizeof (s) // 100, s indicates the memory size of 100 × 1

Strlen (s) // 9, strlen is a function internal implementation is to use a loop to calculate until '\ 0'

4. int s [100] = "0123456789 ";

Sizeof (s) // 400, s indicates the memory size of 100x4

Strlen (s) // error. The strlen parameter can only be char * and must end with '\ 0'

 

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.