The difference between sizeof and strlen in C language

Source: Internet
Author: User

sizeof Initial allocated space size, mid-term

sizeof (unsigned char) = 1;sizeof (signed char) = 1; sizeof (int) = 4;sizeof (unsigned int) = 4;sizeof (short int) = 2;sizeof (unsigned short) = 2;sizeof (long int) = 4;sizeof (unsi gned long) = 4;sizeof (float) = 4;sizeof (double) = 8;sizeof (long double) = 12;sizeof (char *) = 4; for example: Char a[5];int B[5];sizeo F (a) = 5;sizeof (b) = 20; strlen is the length of the array:

(1) char * pointer

strlen (pointer name)

If the argument is a pointer, it calculates the length of the character sequence that the pointer points to. (with ' + ' as the judging sign) for example:

Define char *p= "Hello World"; strlen (p) = 11, and sizeof (p) = 4. Can see strlen
Computes the length of the string pointed to by the pointer, and sizeof calculates that the memory used by the pointer itself is empty
The size of the room.

(2) array

strlen (array name)

If the argument is an array, the actual pass is a pointer, and the strlen will follow the module that handles the pointer above
Handle the array as you type.

We can look at the following example:

Char a[]= "HH";
Strlen (a);

It is clear that the result of strlen is 2. But what if the array is assigned such a value?

Char a[]={' h ', ' H '};
Strlen (a);

So what is the result of strlen (a) now? This number is not necessarily the reason why strlen will go
Calculates the length of the string at which a address begins, since the previous assignment would have HH in the form of a string
Assigning a value to an array assigns a value to the string Terminator ' \ Strlen ', at which point the end character is checked to stop
Calculation, and the second method of complex values is to assign a value with a single character without a Terminator ' \ s ', when we use
sizeof gets the result is normal, and with strlen because cannot find the Terminator, will continue the calculation until
Until the Terminator is found. So this number is not sure.

Example 2:

Char a[]= "Hello";

Char b[]={' h ', ' e ', ' l ', ' l ', ' o '};

strlen(a),What is the value of strlen (b) respectively?

Previously analyzed, strlen is the length of the string, the string has a default of Terminator/0, the Terminator is defined when the string is automatically added to the system, just like the definition of array a. Array a defines a string, and array B defines an array of characters. Therefore, strlen (a) = 5, while the length of strlen (b) is indeterminate, because strlen cannot find the terminator.

-------------------------

One example

#define PATH_TMP "12345"

static void Test_sizeof_strlen () {

Char *msg = "12345";
printf ("sizeof (MSG)---%d\n", sizeof (msg));
printf ("strlen (msg)---%d\n", strlen (msg));

Char array[] = "12345";
printf ("sizeof (array)---%d\n", sizeof (array));
printf ("strlen (array)---%d\n", strlen (array));

printf ("sizeof (PATH_TMP)---%d\n", sizeof (PATH_TMP));
printf ("strlen (path_tmp)---%d\n", strlen (path_tmp));

}

The output result:

sizeof (MSG)---4
strlen (MSG)---5
sizeof (array)---6
strlen (Array)---5
sizeof (PATH_TMP)---6
strlen (PATH_TMP)---5

Char *c= "abcdef";

Char d[]= "abcdef";

Char e[]={' A ', ' B ', ' C ', ' d ', ' e ', ' f '};

printf("%d%d/n", sizeof(c),strlen(c));

printf("%d%d/n", sizeof(d),strlen(d));

printf("%d%d/n", sizeof(e),strlen(e));

The result of the output is:

4 6

7 6

6 14

Analysis:

The first line defines C as a character pointer variable, pointing to a constant string, and C contains the first address of the string.

The second line defines D as a character array, assigning the character array as a string.

The third line defines a character array, which is assigned as a single element.

When assigning a value to a string, "abcdef" is automatically added with a "/0" at the end.

Strlen (c) encounters/0 will end, asking for the length of the string, which is 6.

sizeof (c) asks for the type space size, as previously stated, the size of the pointer type is 4 bytes, the system address bus length is 32 bits.

Strlen (d) is also the same, string assignment, Auto Add/0, the length of the string is of course 6.

sizeof (d) is the size of the space occupied by this array, that is, the number of bytes in the memory space of the array, which should be 7.

sizeof (e), array e is assigned a single element with no/0 terminator, so the size of the occupied space is 6 bytes.

Strlen (e), to find the length of the string ending with/0, because no/0 is found, the returned value is an indeterminate value.

The difference between sizeof and strlen in C language

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.