String and string end flag

Source: Internet
Author: User

There is no special string variable in C language, and a character array is usually used to store a string. When we first introduced a String constant, it indicates that the string is always'/0' is the end of the string. Therefore, when a string is saved to an array, the terminator '/0' is also saved to the array as a flag for whether the string ends. With the '/0' sign, you do not need to use the length of the character array to determine the length of the string.

The C language allows the array to be initialized and assigned a value using a string.

For example:

Char C [] = {'C', '', 'P', 'R', 'O', 'G', 'R', 'A ', 'M '};

It can be written as follows:

Char C [] = {"C program "};

Or remove {} and write it:

Char C [] = "C program ";

The value assignment in string mode occupies one byte more than the value assignment in characters one by one. It is used to store the string ending mark.'/0 '. The actual storage of array C in the memory is as follows:

C

 

P

R

O

G

R

A

M

/0

'/0' is automatically added by the C compilation system. Because of'/0' flag. Therefore, when you use a string to assign an initial value, you do not need to specify the length of the array.

 

Measure the string length function.Strlen

Format:Strlen (character array name)

Function: test the actual length of a string (excluding the end mark of the string ).'/0') As the return value of the function.

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.