The difference between C language--strlen () and sizeof

Source: Internet
Author: User

The difference between strlen () and sizeof ():

Strlen ()-->c string library function that returns the true length of the string. It starts the scan from a location in memory until the terminator stops and returns the counter value.

The sizeof ()-to operator returns the memory size that is "occupied" after the variable declaration, which may not be the actual size.

int num;

sizeof (NUM); Returns num occupies memory size. sizeof num can also be written (without parentheses when the variable name is followed.) )

sizeof (int); Returns the size of an int data that occupies memory. (when followed by a type name, be sure to add parentheses.) )

The following example is good:

#import<Foundation/Foundation.h>intMainintargcConst Char*argv[]) {@autoreleasepool {CharArr1[] ="Wanger"; NSLog (@"Strlen:%lu___sizeof:%lu", strlen (ARR1),sizeof(arr1)); /*strlen (arr1) ———— > returns the actual length of the string "wanger" stored in arr1. sizeof (ARR1) ———— > Returns the amount of memory occupied by the arr1 declaration of an array. (although there are 6 characters, but the system will automatically add the Terminator ' ", so it is 7)*/                        Chararr2[Ten] ="Wanger"; NSLog (@"Strlen:%lu___sizeof:%lu", strlen (ARR2),sizeof(ARR2)); /*sizeof (ARR2) ———— > array ARR2 Declaration when the system allocates 10 bytes to it. */        Char*p ="Wanger"; NSLog (@"Strlen:%lu___sizeof:%lu", strlen (P),sizeof(p)); /*Note: sizeof (P) ———— > It means to return the length of the pointer variable p, we know that the length of the pointer variable is not different depending on the pointer variable type, it is only relevant to the system. */                }    return 0;}

The difference between C language--strlen () and sizeof

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.