Application of C string array

Source: Internet
Author: User

1. special characters 'and' \ 0' in the character array

 

Char A [10] = {'I', '', 'A', 'M','', 'C', 'h', 'I ', 'N', 'A '};
Char B [] = "I am China ";
Printf ("% d \ n", strlen ());
Printf ("% d \ n", strlen (B ));
Printf ("% s \ n", );
Printf ("% s \ n", B );

Output result:

 

20

10

I am chinai am China

I am China

Why is the length of a 20? The reason is that the string ends with '\ 0', the length of array a is 10, and the length of the string is 10, therefore, the value of the next memory unit in the last element of array A is not '\ 0', but the first element in array B. It ends only when' \ 0' in array B is met.

Note: If the precedingCode

Char B [] = "I am China"; changed to Char B [10] = "I am China"; then B [11] is not necessarily '\ 0 '. therefore, the array size should be omitted when naming again.

 

 

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.