The difference between character array and string and its relation

Source: Internet
Author: User
Tags strlen

Look at the following examples:

Char myname[] = {' L ', ' I ', ' s ', ' I '};       Character array <span style= "White-space:pre" >	</span> length is 4
char hename[] = {' L ', ' I ', ' s ', ' I ', '/'};//String c3/> length of 5
char yourname[10] = "Xiaoqiang";            String    length is 10 although only 9 characters

1 Definition: The string is an array of characters ending with '/'. Note, however, that ' strlen ' does not belong to the length of the string, it can be calculated using the length of the character array, and that sizeof is an operator, not a function. We can use sizeof (myName) to find the length of the character array, when ' s ' is counted.

2 Then, why does the normal array have no placeholders, and the character array has%s.

that we cannot

int num[] = {1, 2, 3};
printf ("%d", num);
<pre name= "code" class= "plain" > also cannot

printf ("%s", myName); After output, the content is:lisi\300\367\277_\377</strong></span></p>

But you can
printf ("%s,%s", YourName, Hename);

Because the string is an array of characters ending with '% ', printf encounters '/' will end the output


A summary is:

Strlen: The length of the string is not included '

sizeof: string lengths (including ' \ S ') and number of character array elements can be obtained

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.