Differences and relationships between sizeof () and strlen ()

Source: Internet
Author: User

From the perspective of external features:

# Include <stdio. h> </P> <p> # include <string> </P> <p> int main () </P> <p >{< br/> char array [] = "12345"; </P> <p> int n = sizeof (array ); // n is 6 </P> <p> int M = strlen (array); // m is 5 </P> <p> array [3] = 0; </P> <p> N = sizeof (array); // n is 6 </P> <p> M = strlen (array ); // m is 3 </P> <p> return 0; </P> <p >}</P> <p>

 

The essential difference between the two is that sizeof is an operator, while strlen is a function.

Their definitions in msdn are as follows:

Sizeof Operator

Sizeof expression

 

The sizeof keyword gives the amount of storage, in bytes, associated with a variable or a type (including aggregate types). This keyword returns a value of Type size_t.

The expression is either an identifier or a type-cast expression (a type specifier enclosed in

Parentheses ).

When applied to a structure type or variable, sizeof returns the actual size, which may include Padding Bytes inserted for alignment. when applied to a statically dimensioned array, sizeof returns the size of the entire array. the sizeof operator cannot
Return the size of dynamically allocated arrays or external arrays.

Zookeeper -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Strlen

 

Get the length of a string.

 

Routine required header:

Strlen <string. h>

 

Size_t strlen (const char * string );

Parameter

String: null-terminated string

Libraries

All versions of the C run-time libraries.

 

Return Value

Each of these functions returns the number of characters in string, excluding the terminal

Null. No return value is reserved to indicate an error.

 

Remarks

Each of these functions returns the number of characters in string, not including

Terminating null character. wcslen is a wide-character version of strlen; the argument

Wcslen is a wide-character string. wcslen and strlen behave identically otherwise.

 

It can be seen that strlen is for string, and sizeof is only a keyword.

Most compilers calculate the sizeof value during compilation, while strlen isProgramCalculated during execution.

Sizeof calculates the memory size occupied by this type, while strlen calculates the actual length of the string.

 

Evaluating sizeof expr does not evaluate the expression.

The result of applying sizeof depends in part on the type involved:

L sizeof Char or an expression of type char is guaranteed to be 1

L sizeof a reference type returns the size of the memory necessary to contain an object of the referenced type

L sizeof a pointer returns the size needed hold a pointer; to obtain the size of the object to which the pointer pointers, the pointer must be dereferenced

L sizeof an array is equivalent to taking the sizeof the element type times the number of elements in the array

 

 

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.